Redis is a powerful in-memory storage of data structures, including databases, caches, and message brokers. Most people often think of it as a simple key-value store, but there’s more to it than that. Below I’ll summarize some real world examples of what Redis can do.

1. Full page caching

The first is full page caching. If you’re using server-side rendering, you don’t need to re-render every page for each individual request. With a cache like Redis, you can cache frequently requested content, greatly reducing latency for the most requested pages, and most frameworks have hooks for Redis cached pages.

A simple command

/ Set the page that will last 1 minuteSET key … EX 60// Get the pageGET key

2. List

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

A simple command

// 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

3. Session Session storage

The most common use I’ve seen for Redis 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 on a restart. Even if it’s not a strictly ongoing task, this feature can still save your users a lot of trouble. No one likes to see their conversations randomly deleted for no reason.

A simple command

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

4. The queue

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

A simple command

// Add a Message

HSET messages ZADD due // Recieving Message

ZRANGEBYSCORE due -inf LIMIT 0 1

HGET messages // Delete Message

ZREM due HDEL messages

5.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; It could be infinite. You can create a live chat system, trigger notifications of friend requests on social networks, and so on. This feature is one of the most underrated Redis offers, but it’s powerful and easy to use.

A simple command

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

conclusion

I hope you enjoy using these Redis in the real world. While this article only scratches the surface of what Redis can do for you, I hope it gives you some insight into how you can make the most of Redis.