redis

Caching in NodeJS with Redis

programming Mar 14, 2024

Redis is an open-source (BSD licensed), in-memory data structure store.

Redis has multiple use cases, like an in-memory database for caching, persistent general-purpose database (contrary to many believes Redis has data persistence). For the purpose of this article, we will concentrate on the in-memory database part.

Q. Why is Redis's in-memory database faster than other mainstream databases like Postgres, and MongoDB?
Ans. An In-Memory Database(IMDB) relies on the main memory of the computer (also known as the RAM) for data storage while others use the Disk Storage, which is quite a bit slower than the RAM.

Q. When should you use Redis?
Ans. One would want to use Redis, when they want to do caching server-side, and want full control over the cache, i.e conduct CRUD operations on the cache.

A few numbers:

Here is a postman request to my Node+Express API, which reads data from firebase firestore.

Without Redis

With Redis

We can observe a drastic reduction in the response times, between Redis and not Redis here is how it's implemented.

Revalidation

A good caching system would always account for the validity of the data it holds i.e does it need to get updated/revalidated. There are two key things one needs to take care of, stale data and invalid data. Now stale data in itself just implies that the data that we have in our cache is just simply old data and it would be prudent to delete it and cache it again in case there were some changes. Invalid data means that the data we have in cache is no longer equal to the data stored in our main/persistent database, and it needs to be updated, to put it simply we are unaware of the state of the data when its stale, but in case of invalid we are aware that the data has changed in one way or another.

here is how one would delete data from Redis.

After you delete the cached data, you would ideally wait for someone to request the data again if someone does it, as that would save you the memory that is needed to store the data, and also it would in a way provide the latest possible data at the time of request and caching.

Conclusion

Should you use Redis?

Well it's hard to give a one size fits all response, but here are the things you should keep in mind when deciding to use redis.

Q. Are you gonna store static media like images, videos?
Ans. If your answer is “yes”, then Redis will not work for you, you should look into my article covering Cloudflare page rules, which can cache both JSON data and static media.

Q. Are you only gonna store JSON data or other data that can be stored in a manageable string format?
Ans. If your answer is “yes” then Redis will get your job done.

Q. Do you want full control over how the cache is created, read, updated, deleted (CRUD)?
Ans. If your answer is “yes” you would want to use Redis.

Q. Do you want to take the load off of your server?
Ans. If your answer is "yes" you should look into Cloudflare page rules, to fetch data from Redis, you would still get clients sending requests to your server.

Thank you for reading.

====================================

Here is my Twitter in case you want to get in contact with me.

Tags

Agamjot Jashan Singh

Your average local philanthropist