Improve your nodejs performance with redis

Tram Ho

Introduction

  • The project I’m doing is related to redis, so by the way, I should note a little practice.
  • This article I will not present: What is Redis ?. Why should use redis.
  • This article only provides comparison results when switching to using redis as a cache or replacing traditional data types (SQL and NoSQL).

Redis as cache

  • Let’s look at the example nodejs app that returns book information via google api data.
    • Do not use Redis:

    • Use redis as cache:

  • Upon receiving the request from the client. The server with withoutRedisAsCache.js will call the third party api (here google) and return it to the user. Server with withRedisAsCache.js will check redis first without calling google api (and will be updated to redis for the next call).
  • To run the external server initialize 2 files as above. I installed a number of things:
    • npm init -y initialize project
    • Install the required modules: npm i express axios response-time redis
    • Launch server side with node withoutRedisAsCache.js node withRedisAsCache.js or node withRedisAsCache.js .
  • Now let’s look at the response time for each api. With the same query to the server. Here I want to check server response time. If you want to see the full response then remove the --head option:

  • When there is no redis server time to handle 748,695ms :

  • When redis caches 151.236ms processing time:

Redis as DB

  • As a second example, in some cases instead of saving to traditional SQL or noSQL databases we can use redis instead.
  • Use mongodb:

  • Here I create a User collection with a simple document {name: Asterisk, lastLogin: string} with lastLogin being time in milliseconds.
  • Install the mongoose package: npm i mongoose .
  • Run server: node mongoDB.js .
  • Check the response from the server:

  • Check Header to see response time:

  • So for a record, indexed, the time is about 2,821ms
  • If the same purpose saved lastLogin but this time I will save in redis:
  • Use redis:

    • Test response from server side:

    • Check header view response time 0.384ms :

Conclusion

  • In the above part, I demo 2 examples to change server response time to client:
    • When using redis as cache time decreased: 748,695ms -> 151,236ms.
    • When using redis as db time decreased: 2,821ms -> 0.384ms.
  • The above two changes in time are only relative because each time running has changed slightly. But nonetheless see the rate of change quite clearly in terms of response time.
  • When the results returned by the client do not change much, we can consider using Redis as a cache and updating when there are changes, for example, user information, or in the above example, from a book: author , year of issue …
  • When the API prioritizes real-time responsiveness as fast as in example 2 we can also consider saving some information in redis instead of the usual db. Although each type of DB still has its own strengths, we need to consider when choosing

Bonus Redis command exec on:

  • String:
    • GET : Get the value of the key (get value of key) returns nil if the key does not exist and error if the value stored in the key is not string. GET only handles value as string.
    • SET : assign a value (type of string) to a key. If the key is already valid, it will be overwritten (regardless of the previous type). Options:
      • EX seconds: the time expires in seconds.
      • PX milliseconds: time expires in milliseconds.
      • NX: Only considered when the key contains no value.
      • XX: Only consider if the key has no value.
    • Because SET has the above options, the following statements may be dropped in the future: SETEX, SETNX, PSETEX.
    • INCR : Increase the number stored in the key by one. If the key has no value, it is set to 0 and increments by 1 (ie = 1). Returns an error if the value stored in the key is not a string or a string cannot be converted to a number. Addition 1 is limited by 64 bit signed integers.
    • DECR : Same as INCR but this guy minus 1.
  • List:
    • To be a little girl: will update some more commands in redis.
Share the news now

Source : Viblo