Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node-Cache vs Redis for Simple Caching

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 ?

like image 799
Ligo George Avatar asked Sep 05 '17 07:09

Ligo George


People also ask

Should I use Redis for caching?

Redis is extremely fast. It delivers sub-millisecond response times that enable millions of requests per second to power demanding real-time applications. Typically, you'll want to store frequently accessed data in Redis so that whenever the data is requested, it can come from the cache instead of your database.

Is Redis faster than in-memory cache?

Redis is a remote data structure server. It is certainly slower than just storing the data in local memory (since it involves socket roundtrips to fetch/store the data).

What is difference between Redis and cache?

Memcached and RedisMemcached is a distributed memory caching system designed for ease of use and simplicity and is well-suited as a cache or a session store. Redis is an in-memory data structure store that offers a rich set of features. It is useful as a cache, database, message broker, and queue.

Is Redis just a cache?

Redis began as a caching database, but it has since evolved into a primary database. Many applications built today use Redis as a primary database. However, most Redis service providers support Redis as a cache but not as a primary database.


1 Answers

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.

like image 56
Alexandru Olaru Avatar answered Sep 24 '22 12:09

Alexandru Olaru