8 Use Cases of Redis Beyond Key Value Store
Redis is not just a key-value store; it is far beyond that. In this post we are going to discuss the same. Before discovering these use cases, I was using Redis just for simple key-value storage and as a database for my open-source rate limiter.
1. API Caching Layer
We can use Redis as a caching layer for your API to make them fast. If there is an API that return lot of static data that does not get updated regularly, we can introduce Redis caching layer in it.
Suppose there is an API that returns a list of all filters, and then it is displayed on the frontend. Why is there a need to fetch it from the database if it rarely gets updated? Instead, store it in Redis.
Or you can also use the Read Through Cache strategy. Get all filters from cache if they are present, and if they are not present, get them from the primary data source and store them in cache.
2. Real-Time Counter Analytics
Imagine you have a scenario where you have to store simple metrics like page views, video views, hit counters or event counts. Redis can be a good solution here if you don’t need too much of an advanced analytical system.
Simply storing them in Redis will prevent unnecessary writing in SQL, or if I say HDD or SDD. It will also be near real-time, as Redis will store them in memory instead of HDD or SDD.
3. Ranking Systems (Leaderboard)
Redis has dedicated solutions for ranking systems such as Game Leaderboard, e-commerce product popularity, Reddit-style content voting systems, and crypto price leaderboards.
In Redis we get a data structure called Redis Sorted Set. It is a collection of unique strings that are ordered in ascending or descending order. It contains a member (string) and score. Members are sorted automatically based on score.
Member Score
John 2312
Sushant 2199
Kevin 874
Steven 855
Ben 450
Above is the structure of a Redis Sorted Set. With this structuring you can easily get leaderboard status such as top 10 players.
By the way, members are stored in ascending order by default. I reversed them just to show the example of the leaderboard.
4. Rate Limiter
Redis can act as a fast database for rate limiter. My open-source rate limiter is completely built using Redis. I’ve implemented a token bucket and sliding window rate limiting algorithm using Redis.
For Token Bucket I used the ReJSON module, as I wanted to store the key in JSON format in Redis, and for the Sliding Window limiting algorithm, I used Redis Sorted Set.
5. Session Store
If you are using session-based authentication in your application and if you are using any SQL or NoSQL database to store user sessions, then you can replace it with Redis. It will be faster than SQL or MySQL. It also provides TTL functionality that will automatically clean data after a predefined delay.
Create a user session and store it in Redis with 24 hours of TTL. The user will remain logged in for 24 hours, and after this time, Redis will automatically delete this record, causing the user session to log out.
6. Geospatial Data
Geospatial data is the information that includes location data, typically in the form of latitude and longitude coordinates, allowing for the mapping of objects, events, and phenomena on Earth's surface.
Redis comes with great support for storing and querying location data. It will be useful for location services in scenarios such as finding the nearest drivers or shops.
7. Pub/Sub Systems
If your application needs to send real-time updates to multiple users or services, Redis Pub/Sub can be a good choice. Redis allows one service to publish a message, and other services or users who are subscribed to that channel will receive it instantly.
You can use this for real-time notifications, chat systems, or microservices communication. For example, if one microservice wants to notify another about a new order or an event, it can just publish a message to a Redis channel, and the subscribed service will receive it immediately.
8. Task Queue
Redis can be used to build task queues in your application. If you are running background jobs like sending emails, processing images, or doing long-running tasks, you can push these jobs into a Redis queue and process them in the background.
You can use Redis lists to push a task (LPUSH
) and pop a task (RPOP
) for processing. Many popular libraries like Bull (Node.js) and RQ (Python) use Redis under the hood to manage job queues.
For example, when a user signs up, you can push an email task into the Redis queue. A background worker will later pick it up and send the email without blocking the main application.
Other Posts
Thanks!
Thanks for reading this post. You can subscribe to my newsletter. It is completely free and I never spam.