5 Real-World Uses of Redis

Redis is a powerful in-memory data structure store that encompasses a variety of uses including database, cache and message broker. Most people often think of it as nothing more than a simple key-value store, but it actually has much more capability. Below I’ll summarize some real-world examples of what Redis can do.

Full page caching

The first is whole-page caching. If you are using server-side rendered content, there is no need to re-render each page for each individual request. With a cache such as Redis, you can cache frequently requested content, which greatly reduces latency for the most requested pages, and most frameworks have hooks for Redis cached pages.

Simple commands

/ Set the page that will last 1 minuteSET key "<html>...</html>" EX 60// Get the pageGET key  

Leaderboard

One of the things that makes Redis shine is the leaderboard. Because Redis is in memory, it can handle incrementing and decrementing very quickly and efficiently. Compare this to running SQL queries per request and the performance gains are huge! This combined with Redis’ sorting set means you can grab the highest rated items in the list in milliseconds, and it’s very easy to implement.

Simple commands

// Add an item to the sorted setZADD sortedSet 1 "one"// Get all items from the sorted setZRANGE sortedSet 0 -1// Get all items from the sorted set with their score  
ZRANGE sortedSet 0 -1 WITHSCORES  

Session Storage

The most common use of Redis that I have seen is session storage. Unlike other session stores such as Memcache, Redis can retain data so that in the event of a cache stop, all data is still there when it is restarted. Even if it’s not a task that requires strict persistence, this feature can still save your users a lot of headaches. No one will be happy to have their sessions randomly deleted for no reason.

Simple commands

// Set session that will last 1 minuteSET randomHash "{userId}" EX 60// Get userIdGET randomHash  

Queue

One of the less common, but very useful, things you can do with Redis is queueing. Whether it’s an email queue or other data used by your application, you can create an efficient queue in Redis. Any developer who is familiar with stacks and can push and pop items can use this feature easily and naturally.

Simple commands

// Add a Message  
HSET messages <id> <message>ZADD due <due_timestamp> <id>// Recieving Message  
ZRANGEBYSCORE due -inf <current_timestamp> LIMIT 0 1  
HGET messages <message_id>// Delete  Message  
ZREM due <message_id>HDEL messages <message_id>  

pub/sub

The ultimate real-world use of Redis is the pub/sub that I will present in this article. this is one of the most powerful features built into Redis; the possibilities are endless. You can create a live chat system, trigger notifications for friend requests on social networks, etc. This feature is one of the most underrated features Redis offers, but is very powerful and easy to use.

// Add a message to a channelPUBLISH channel message// Recieve messages from a channelSUBSCRIBE channel  

Conclusion

I hope you’ll enjoy these real-world uses of Redis. While this article only scratches the surface of what Redis can do for you, I hope you’ll take away inspiration on how you should make the most of Redis.

Leave a Reply