Redis application scenarios

Source: Internet
Author: User

 

Source: http://blog.nosqlfan.com/html/2235.html

 

There is no doubt that redis has created a new way of data storage. Using redis, we don't have to focus on how to put elephants in the refrigerator in the face of monotonous databases, instead, redis uses flexible data structures and data operations to build different refrigerators for different elephants. Hope you like this metaphor.

The following is a new article by @ antirez, author of redis. He describes some applicable application scenarios of redis. nosqlfan is briefly listed here for your reference:

1. Operations for retrieving the latest n data

For example, for the latest articles on your website, we can place the latest 5000 comment IDs in the list set of apsaradb for redis, and retrieve the parts beyond the set from the database.

  • Use the lpush latest. Comments <ID> command to insert data to the list set.
  • Use the ltrim latest. Comments 0 5000 command to save only the last 5000 IDs.
  • Then we can use the following logic (Pseudo Code) to obtain a page of comments from 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_listEND

If you have different filtering dimensions, such as the newest n entries of a specific category, you can create another list based on this category and store only IDs. redis is very efficient.

2. Ranking application, Top N operations

The difference between this requirement and the above requirement is that the previous operation takes the time as the weight, and this is based on a certain condition as the weight, such as sorting by the top number of times, at this time, our sorted set will be out of service. Set the value you want to sort to the score of sorted set and set the specific data to the corresponding value, you only need to execute a zadd command each time.

3. Applications requiring precise expiration time setting

For example, you can set the score value of the sorted set as the timestamp of the expiration time, so you can simply sort the expiration time and regularly clear the expired data, in addition to clearing expired data in redis, You can regard the expiration time in redis as an index of the data in the database, and use redis to find out which data needs to be deleted after expiration, then, the corresponding records are precisely deleted from the database.

4. Counter application

Redis commands are atomic. You can easily use the incr and decr commands to build a counter system.

5. uniq operation to obtain the values of all data records in a certain period of time

The Set Data Structure of redis is the most suitable. You only need to constantly throw the data to the set. Set indicates the set, so it will automatically remove the weight.

6. Real-Time System and Anti-Spam system

With the set function mentioned above, you can know whether an end user has performed an operation, find a set of operations, and perform analysis, statistics, and comparison. You can't do it.

7. Build a Real-Time Message System Using pub/sub

Redis pub/sub systems can be used to build real-time messaging systems, for example, many examples of real-time chat systems built using pub/Sub.

8. Build a queue system

You can use list to build a queue system, and use sorted set to build a queue system.Priority.

9. Cache

Needless to say, the performance is better than memcached, and the data structure is more diversified.

Source: antirez.com

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.