Redis author talks about Redis application scenarios
1. The operation of taking the latest N data
For example, typically take the latest articles on your website, in the following ways,
We can put the ID of the latest 5000 comments in the List collection of Redis, and get the part beyond the collection from the database
Use the LPUSH latest.comments<ID> command to insert data into the list collection
After the insertion is completed, use the LTRIM latest.comments 0 5000 command to make it only save forever
Last 5000 IDs
Then we can use the following logic (pseudo code) when we get a page comment on the client
FUNCTION get_latest_comments(start,num_items):
id_list = redis.lrange("latest.comments",start,start+num_items-1)
IF id_list.length < num_items
id_list=SQL_DB("SELECT ... ORDER BY time LIMIT ...")
END
RETURN id_list
END
If you have different filter dimensions, such as the latest N items of a certain category,
Then you can build another List according to this classification. If only ID is stored, Redis is very efficient.
2. Leaderboard application, take TOP N operation
The difference between this requirement and the above requirement is that the previous operation is weighted by time, and this is weighted by a certain condition.
For example, sorting by the number of tops, then we need our sorted set to go out and set the value you want to sort as the score of the sorted set,
To set the specific data to the corresponding value, only one ZADD command needs to be executed each time.
3. Applications that need to accurately set the expiration time
For example, you can set the score value of the sorted set mentioned above to the timestamp of the expiration time,
Then you can simply sort by expiration time and clear expired data regularly, not only clearing expired data in Redis,
You can completely regard the expiration time in Redis as an index to the data in the database, and use Redis to find out which data needs to be expired and deleted.
Then precisely delete the corresponding record from the database.
4. Counter application
Redis commands are atomic, you can easily use INCR, DECR commands to build a counter system.
5. Uniq operation, get all the data ranking values for a certain period of time
This set data structure using Redis is the most suitable. You only need to keep throwing data into the set. Set means set, so it will automatically arrange weights.
6. Real-time system, anti-spam system
Through the set function mentioned above, you can know whether an end user has performed a certain operation, and you can find the set of its operations and perform analysis and statistical comparisons. Not impossible, only unexpected.
7. Pub/Sub builds a real-time messaging system
Redis' Pub/Sub system can build real-time messaging systems, such as many examples of real-time chat systems built with Pub/Sub.
8. Build a queue system
Queue systems can be built using lists, and even prioritized queue systems can be built using sorted sets.
9. Cache
Needless to say, the performance is better than Memcached, and the data structure is more diverse.