Node-Cache vs Redis for Simple Caching

12,826

Solution 1

If you plan on scaling your application in the future, you choose redis

If this is just a small project that you need good performance you go with in-memory.

As a starter I recommend to go with in-memory caching but use a inversion of control mechanism to be able to refactor easily this stuff after into using redis. Try something like this:

// cache service

module.exports = (provider) => {
   // provider here is an abstraction of redis or in memory node
   // implement your logic
   
   const save = (key, item) => {
     provider.save(key, item);
     // return promise callback etc
   }    

   const getItem = (key) => {
      provider.get(key);
   } 

   return {
     save,
     getItem,
   }
}

And then you just use the needed provider and you can easily refactor the code after, no need to change all the files in your app.

Solution 2

It depends on the object size you want to cache, you can use redis alone, or local cache alone, or a combination of both. Redis would give you many advantages like centralized cache and thus a higher cache hit rate ( as one web server sets the cache, all other web servers can use the value), whereas in node cache the cache is set per server, and hence has a lower cache hit rate. The performance advantage of local cache is avoiding the network call, which is a miniscule overhead if your object is not very large, and redis would give you good performance in both cases if you host it locally on the web server or a dedicated seperate server. If your objects are large, say few MBs, then network hops are not advisable, you shall get the object from database or redis cache and cache it locally too.

Solution 3

Redis is great if you want to build a full-blown caching system. For example, you want to keep the session in cache and want to scale in the future. But there are cases that node-cache would be much more useful even in the case that you have to scale your application in the future. For example, in the case that you have pretty static information that is saved in a database and you just don't want that the server will always do a network call. node-cache is much faster, you can upload the information to node-cache or any other internal caching with a ttl and like this have the information in a faster way.

Share:
12,826
Ligo George
Author by

Ligo George

Updated on June 06, 2022

Comments

  • Ligo George
    Ligo George almost 2 years

    I am planning to use a cache in my Node.js application just to avoid a database read operation. It is only some small amount of data (instead of reading same data from database every time) . I am planning to have a local cache in each server. Should I go for Node Cache (https://www.npmjs.com/package/node-cache) or Redis ?

    Which will be faster and efficient ?

  • Sanjay Kumar N S
    Sanjay Kumar N S about 6 years
    Cache it locally means browser or the server itself?