Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the general application scenarios of redis?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Today, the editor shares with you what are the general application scenarios of redis. I believe many people do not know much about it. In order to let you know more about redis, I have summarized the following contents for you. Let's look down together. I'm sure you'll get something.

1. Cache of hot spot data

Because redis accesses speed blocks and supports a wide range of data types, redis is very suitable for storing hot data. In addition, combined with expire, we can set the expiration time and then update the cache. This function is the most common, and almost all of our projects have been used.

2. The application of time-limited business

In redis, you can use the expire command to set the lifetime of a key, and redis will delete it when the time expires. This feature can be used in time-limited preferential activity information, mobile phone CAPTCHA and other business scenarios.

3. Counter related problems

Redis incrby command can be used to increase atomicity, so it can be used in highly concurrent flash sale activity, distributed serial number generation, and specific business, such as limiting the number of SMS messages sent by a mobile phone number, the number of requests per minute, the number of calls per day, and so on.

4. Issues related to the ranking

The query speed of relational database is generally slow in terms of ranking, so we can sort hot data with the help of redis's SortedSet.

In the milk tea activity, we need to show the like list of each department, so I made a SortedSet for each department, then took the user's openid as the above username, and the user's like number as the above score, and then made a hash for each user. Through zrangebyscore, we can get the ranking according to the number of likes, and then get the user's hash information according to the username. The performance experience was also quite good in practical application at that time.

5. Distributed lock

This is mainly carried out by using the setnx command of redis. Setnx: "set if not exists" means that if it does not exist, successfully set the cache and return 1, otherwise return 0. This feature is used in the background where you go far away, because our server is clustered, and scheduled tasks may run on both machines, so set a lock through setnx in the scheduled task first, and execute it if it is set successfully. If it is not set successfully, the scheduled task has been executed. Of course, combined with the specific business, we can add an expiration time to the lock, such as a scheduled task executed every 30 minutes, then the expiration time can be set to a time less than 30 minutes, which is related to the cycle of the scheduled task and the time consumed by the scheduled task execution.

Of course, we can apply this feature to other scenarios that require distributed locks, and the main purpose of combining expiration time is to prevent deadlocks.

6. Delay operation

I have tested this so far, but it has not been applied to our actual project, so let me give you an application scenario of this feature. For example, after the production of the order, we occupy the inventory, 10 minutes later to check that the user is enough to buy, if there is no purchase, the document will be set invalid, and restore the inventory at the same time. Since redis has provided Keyspace Notifications since 2.8.0, it allows customers to subscribe to Pub/Sub channels to receive events that affect the Redis dataset in some way. So we can use the following solution for the above requirements. When we order production, we set a key and set it to expire 10 minutes later. We implement a listener in the background to listen for the effectiveness of the key, and add the follow-up logic when the key fails. Of course, we can also use the delay queue service of message middleware such as rabbitmq and activemq to achieve this requirement.

7. Paging and fuzzy search

A zrangebylex method is provided in the set collection of redis, with the following syntax:

ZRANGEBYLEX key min max [LIMIT offset count]

Paging data can be queried through ZRANGEBYLEX zset-+ LIMIT 010, where-+ means to get all the data

Zrangebylex key min max can return the data in the dictionary range, and this feature can be used for fuzzy query. This is the only feature I found in redis that supports fuzzy query of stored content.

A few days ago, I used this feature to simulate and test the school data. The school data is about 600000, and the response time is about 700ms, which is a little faster than mysql's like query, but because it can avoid a large number of database io operations, it is still more conducive to system performance than direct mysql query.

8. Storage of interrelationships such as likes and friends

Redis set external function is similar to list is a list function, the special feature is that set can automatically arrange weight, when you need to store a list of data, but do not want to duplicate data, set is a good choice, and set provides an important interface to determine whether a member is in a set collection, which list can not provide. Or in the Weibo application, where there is a collection of people that each user follows, it is easy to find a common friend of two people.

This is used in milk tea activities, that is, set is used to store likes between users. In addition, the sismember method is used to determine whether a like has been given before giving a like. At that time, the response time of this API was controlled within 10 milliseconds, which is very efficient.

9. Queue

Because redis has commands such as list push and list pop, it is easy to perform queue operations.

On the general use of redis scenarios which are shared here, I hope that the above content can have a certain reference value for you, you can learn to apply. If you like this article, you might as well share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report