One of the great benefits of Redis is that you can use databases such as MySQL instead of the entire database, but only in some specific scenarios to improve efficiency through Redis's features. This article lists 11 such web scenarios, such as displaying the latest list of items, deleting and filtering, leaderboards, and other related requirements.
"51CTO featured Translations" in the "How to make Redis work in your system" article, Salvatore ' Antirez ' Sanfilippo tells us how to use Redis's unique data structure processing power to solve some common problems. Some Redis primitive commands, such as Lpush, LTrim, and Lrem, can be used to help developers accomplish the tasks they need--tasks that are difficult or slow in traditional database storage. This is a very useful and practical article. So how do you accomplish these tasks in your framework?
Here are 11 web scenarios where you can take advantage of Redis's features to improve efficiency.
1. Displays the most recent list of items in the home page.
Redis uses a cache of resident memory, which is very fast. Lpush is used to insert a content ID that is stored as a keyword in the list header. LTrim is used to limit the maximum number of items in a list to 5000. If the amount of data that the user needs to retrieve exceeds this cache capacity, then the request needs to be sent to the database.
2. Delete and filter.
If an article is deleted, you can use Lrem to completely erase it from the cache.
3. Leaderboards and related issues.
Leaderboards (leader board) are sorted by score. The Zadd command can implement this function directly, and the Zrevrange command can be used to get the top 100 users according to the score, Zrank can be used to get the user rank, very straightforward and easy to operate.
4. Sort by user vote and time.
This is like Reddit's leaderboard, where scores change over time. The Lpush and LTrim commands are combined to add the article to a list. A background task is used to get the list and recalculate the sorting of the list, and the Zadd command is used to populate the build list in the new order. Lists can be retrieved very quickly, even on heavily loaded sites.
5. Expired item processing.
Use Unix time as a keyword to keep the list sorted by time. Search for Current_time and time_to_live to complete the arduous task of finding overdue items. Another background task uses the Zrange ... Withscores the query to delete the expired entries.
6. Count.
The use of various data statistics is very extensive, such as to know when to block an IP address. The Incrby command makes it easy to keep count by atomic increment, Getset to reset the counter, and the Expiration property to confirm when a keyword should be deleted.
7. Specific items within a specific time period.
This is a problem for a particular visitor and can be resolved by using the Sadd command for each page view. Sadd does not add a member that already exists to a collection.
8. Real-time analysis of what is happening, for data statistics and prevention of spam.
Using the Redis Primitives command makes it easier to implement spam filtering systems or other real-time tracking systems.
9. Pub/sub.
Maintaining user mapping of data in an update is a common task in the system. Redis's pub/sub feature uses the subscribe, unsubscribe, and publish commands to make this easier.
10. Queue.
Queues are ubiquitous in the current programming. In addition to the push and pop type commands, Redis also has commands to block queues, allowing one program to be added to the queue by another program at execution time. You can also do something more interesting, such as an RSS feed queue that spins updates.
11. Cache.
The Redis cache is used in the same way as memcache.
Web applications cannot endlessly model wars, look at these redis primitive commands, even though they are simple but powerful and combine them to make it even more unimaginable. Of course, you can write code specifically to do all of this, but Redis is obviously easier to implement.
Original: Common Web use Cases Solved in Redis