← All writings // #0002

Is Redis the right call?

Redis

I've been using Redis for quite a while, and I've been treating it as magic. 

Database is slow? Add Redis.

API is slow? Add Redis.

Are sessions slow? Add Redis.

Is everything slow? Have you tried Redis?

It is like we're using Redis as a performance insurance, but that's not the case.

It is indeed that Redis is fast, but it is expensive since it stores data in memory. 

Well, let's talk about several cases where we shouldn't rely on Redis.

 

Your queries are not optimized

Instead of immediately implementing Redis, why don't we start by optimizing our database queries?

Redis can boost application performance, but it won't solve inefficient queries. It may only hide the underlying problem.

For example, you might discover that your application is suffering from the N+1 query problem. Before adding Redis, we should first ask: can we reduce the number of queries, add the right indexes, or optimize how we fetch the data?

Fix the query first. Then, if the database is still a bottleneck, consider Redis.

 

You have complex queries

An example of this is a listing page in e-commerce. You are building a page that requires you to query multiple tables, join them, and add where statements.  Then, suddenly you found out that the queries are so slow. You started to think about adding Redis as a cache. That might work, but you can quickly end up dealing with complicated cache keys, invalidation logic, and difficult debugging, especially when your listing page has many filters and facets.

In this scenario, the better solution might not be Redis. You may need to optimize your database queries or, if the read workload has genuinely outgrown your current database, consider a read-optimized database with CQRS. I want to tell you about this one in this section, but you can find a lot on the internet about the usage of OpenSearch, Algolia, or TypeSense.

 

You use Redis for heavy computation

Yes, Redis can support some computation using LUA scripts or Redis functions, but it doesn't mean you can put a heavy operation in Redis. Why is this bad? Redis is single-threaded when executing a command operation. Therefore, if you have a heavy operation, it will block other operations, causing a bottleneck. In fact, you should move that operation to another service that runs asynchronously, so it won't block your other operations.

 

I am writing this because I hate Redis. In fact, I love using Redis in my projects since Redis is a great tool and almost helps me with every performance problem. But sometimes we are not using the tool correctly.