5 usage Scenarios for Redis
Redis is a powerful storage of memory data structures, including databases, caches, and message brokers for a variety of purposes. Most people often think of it as just a simple key-value store, but in fact it has more power. I'll summarize some real examples of what Redis can do.
1. Full Page Caching
The first is the full page cache. If you are using server-side rendered content, you do not need to re-render each page for each individual request. With caches such as Redis, you can cache frequently requested content, greatly reducing the latency of the most requested pages, and most frameworks have hooks for Redis cache pages.
Simple command
/Set The page that would last 1 minute
SET key "
Get the page
GET Key
2. Leaderboard
One of the most dazzling areas of Redis is the leaderboard. Because Redis is in memory, it can handle increments and decrements very quickly and efficiently. Comparing this with each request to run a SQL query, performance gains are significant. This combination with the Redis sort set means that you can crawl the highest-scoring items in the list in milliseconds and are easy to implement.
Simple command
Add an item to the sorted set
Zadd SortedSet 1 "one"
Get all items from the sorted set
Zrange SortedSet 0-1
Get all items from the sorted set with their score
Zrange SortedSet 0-1 withscores
3. Sessions Session storage
The most common use of Redis I have seen is session storage. Unlike other session stores (such as Memcache), Redis can retain data so that when the cache is stopped, all data still exists at reboot. This feature can save your users a lot of hassle, even if they don't require a rigorous, continuous task. No one would be happy to see their conversation randomly erased for no apparent reason.
Simple command
Set session that would last 1 minute
SET randomhash "{userId}" EX 60
Get userId
GET Randomhash
4. Queues
A less common but very useful thing to do with Redis is to queue up. Whether it's an e-mail queue or data used by other applications, you can create an efficient queue in Redis. Any developer familiar with the stack and push and pop projects can easily and naturally use this feature.
Simple command
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>
5.pub/sub
The final usage of Redis in the real world is the pub/sub I will present in this article. This is one of the most powerful features built into Redis, and it may be infinite. You can create a live chat system that triggers notification of a friend request on a social network, and so on. This feature is one of the most underrated features offered by Redis, but it is very powerful and simple to use.
Simple command
ADD a message to a channelpublish channel message
Recieve messages from a channelsubscribe channel
Conclusion
I hope you will like the use of these redis in the real world. Although this article captures only the surface of what Redis can do for you, I hope you can get a good idea of how to make the most of Redis.
Link: http://www.codeceo.com/article/5-real-world-uses-redis.html
English Original: https://ryanmccue.ca/5- real-world-uses-for-redis/(5 Real World uses for Redis)
Translator: Code farm – Small peak