One of the great advantages of Redis is the ability to use databases such as MySQL, rather than the entire database, to improve efficiency only in certain scenarios 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 of the Redis primitive commands, such as Lpush, LTrim, and Lrem, can be used to help developers complete the tasks they need--tasks that are difficult or slow in traditional database storage. This is a very practical and practical article. So how do you finish these tasks in your frame?
Here are 11 web scenarios in which you can take advantage of Redis's features to make it much more efficient.
1. Displays the most recent list of items in the home page.
Redis uses a cache of resident memory, which is fast. Lpush is used to insert a content ID that is stored as keyword in the list header. LTrim is used to limit the maximum number of items in a list to 5000. It is assumed that the amount of data that the user needs to retrieve exceeds this cache capacity before the request is sent to the database.
2. Delete and filter.
Suppose an article is deleted and can be wiped out completely from the cache using Lrem.
3. Leaderboards and related issues.
The leaderboards (leader board) are sorted according to the score. The Zadd command is able to 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. According to the user vote and time sorting.
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 once again the list is sorted, and the Zadd command is used to populate the build list in the new order. The list enables very high-speed retrieval, even for sites with very heavy loads.
5. Expired item processing.
Use Unix time as keyword to keep the list sorted by time. Search for Current_time and time_to_live and complete the arduous task of finding overdue items. There is also a background task using the Zrange ... Withscores the query to delete the expired entries.
6. Count.
The use of various data statistics is very extensive, for example to know when to block an IP address. The Incrby command makes these very easy to keep count by atomic increment, Getset is used to reset the counter, and the Expiration property is used to confirm when a keyword should be deleted.
7. Specific items within a specific time period.
This is a question for specific visitors, and can be resolved by using the Sadd command for each page view. Sadd does not add members that already exist to a collection.
8. Real-time analysis of what is happening, for data statistics and prevention of spam.
Use the Redis primitives command to make 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 a command to block the queue, allowing a program to be added to the queue at run time by a program. You can also do something more interesting, such as a rotation of the updated RSS feed queue.
11. Cache.
The Redis cache is used in the same way as memcache.
Web applications cannot endlessly model war, look at these Redis primitive commands, although simple but powerful, combine them, can be finished more unimaginable. Of course, you can write code to complete all of these operations, but Redis is obviously easier to implement.
Original: Common Web use Cases Solved in Redis
REDIS11 Web Application Scenarios