ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Data Caching with Redis
    Framework/Node.js 2020. 2. 22. 14:48

    1. Overview

    Redis (Remote Dictionary Server) is an in-memory data structure project implementing a distributed, in-memory key-value database with optional durability. Redis supports different kinds of abstract data structures, such as strings, lists, maps, sets, sorted sets, HyperLogLogs, bitmaps, streams, and spatial indexes.

    2. Intuition

    2.1 Index search case

    We have this index over here for specifically the id property that means that if we ever ask Mongo to give us a blog with a particular ID this index can very easily go and find the correct blog post.

    2.2 Full Collection Scan

    A full collection scan a collection scan means that we have to look at every single record inside of a given collection which is an extremely expensive operation relatively

    2.3 Solutions

    2.3.1 Adding indices

    Drawbacks:

    1. Whenever we add indices to a collection that has an impact on our ability to write to that collection performance. In other words for every additional collection we add, every additional index that we add to a collection takes longer to write records into that collection.
    2. Any time we add in more indices that consumes more disk space and more memory as well.
    3. We might be making queries inside of an application where we can't really figure out ahead of time what indices we need for it.

    3. Query Caching Layer

    It's going to maintain a record between queries that are issued and responses that come back. Now any time that that same exact query is issued again Mongoose is going to send the same query over to the cache server but this time if the cache server sees that that query has already been issued once before it's not going to send the query onto MongoDB.

    4. Redis

    4.1 Redis in Node

    4.2 Redis Outline

    In this case whenever we are trying to retrieve a value out of red S that is an asynchronous operation. So we have to pass in a callback function as a second argument.

    4.3 Redis Hashes

    Whenever we store data inside of redis we only have the option of storing numbers and letters. In other words, we cannot store a plain javascript object directly inside of Redis.

    4.4 Cache Keys

    4.5 Caching Solution

    The entire process right there is like formulating some query and getting close to setting off to Mongo sounds like it's incredibly related to the lifecycle of the caching system.

    // result of below using stringify below as key
    query.getOptions();

    4.6 Difference JSON and Mongoose Model

    4.7 Cache Expiration

    5. Reference

    https://en.wikipedia.org/wiki/Redis

    https://www.npmjs.com/package/redis

    https://redis.io/documentation

    https://github.com/microsoftarchive/redis

    'Framework > Node.js' 카테고리의 다른 글

    Enhancing Node Performance  (0) 2020.02.21
    Single Thread, Event Loop, and Blocking Code  (0) 2020.02.21
    The Internals of Node.js  (0) 2020.02.21

    댓글

Designed by Tistory.