Redis authors talk about Redis application scenarios

Source: Internet
Author: User

Without a doubt,Redis pioneered a new way of storing data, using Redis, instead of focusing on how to put an elephant into a refrigerator when faced with a monotonous database, we use Redis's flexible data structure and data manipulation. Build different refrigerators for different elephants. I hope you like this metaphor.

Here is a fresh article, the author of the Redis author @antirez, describing some of the applications that Redis is more suitable for, Nosqlfan simply listed here for you to list:

1. Take the latest N data operation

For example, the most recent article to get your site, by the following way, we can put the latest 5,000 reviews of the ID in the Redis list collection, and will be out of the collection part from the database

    • Use the Lpush latest.comments<id> command to insert data into the list collection

    • Insert complete and then use the LTrim latest.comments 0 5000 command to keep the last 5,000 IDs forever

    • 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_listend

If you have a different filter dimension, such as the newest n for a category, you can build a list that is categorized by this category, and Redis is very efficient if you save the ID.

2.LeaderboardApply, take top N action

This requirement differs from the above requirements in that the preceding operation takes the time as the weight, this is the weight of a certain condition, such as the order of the top number of times, then we need our sorted set to go, set the value you want to sort to sorted set Score, the specific data is set to the corresponding value, each time only need to execute a zadd command.

3. Applications that require precise setting of expiration time

For example, you can set the score value of the sorted set to the timestamp of the expiration time, then you can simply sort through the expiration time, and periodically purge out-of-date data, not only to clear the expired data in Redis, You can think of this expiration time in Redis as an index to the data in the database, use Redis to find out what data needs to be deleted, and then delete the corresponding records from the database exactly.

4.counterApplication

Redis commands are atomic, and you can easily use the INCR,DECR command to build a counter system.

5.Uniq operation to get all data rows for a certain period of time

This is the most appropriate set data structure to use Redis, just to constantly throw it into set, set means set, so it automatically takes weight.

6. Real-time system,anti-spamSystem

With the Set function mentioned above, you can know whether an end user has performed an operation, can find a combination of its operations to analyze statistical comparisons, and so on. There is nothing to be done, only unexpected.

7.pub/sub Building a real-time messaging system

Redis's pub/sub system can build real-time messaging systems, such as many examples of real-time chat systems built with Pub/sub.

8. BuildQueueSystem

Using list, you can build a queue system, and you can even build a prioritized queue system using sorted set.

9.Cache

This needless to say, performance is better than memcacheD, and the data structure is more diverse.

Source: antirez.com


Redis authors talk about Redis application scenarios

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.