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 ?
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.
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).
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With