What is Redis capable of? Look at 11 web scenarios [go]

Source: Internet
Author: User

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.

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.leaderboard Application, Top n operation

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 number of times by the top, then we need our sorted set to go, set the value you want to sort into the score of sorted set, Set the specific data 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.Counter Application

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-spam system

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. BuildQueuing System

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

51838190

What is Redis capable of? Look at 11 web scenarios [go]

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.