Summary of redis application scenarios. We usually use redis in many places. Here is a summary of the application scenarios we understand:
1. Cache of hotspot data
Because redis has fast access speed and supports rich data types, redis is very suitable for storing hotspot data. In addition, combined with expire, we can set the expiration time and then proceed. Cache update operation, this function is the most common and is used in almost all our projects.
2. Application of limited-time business (Recommended learning: Redis video tutorial)
You can use the expire command to set the survival time of a key in redis , redis will delete it after the time is up. This feature can be used in business scenarios such as limited-time promotional information and mobile phone verification codes.
3. Issues related to counters
Since the incrby command can achieve atomic increment, redis can be used for high-concurrency flash sales activities and distributed sequence number generation. , Specific services are also reflected in, for example, limiting the number of text messages sent by a mobile phone number, limiting the number of requests per minute for an interface, limiting the number of calls per day for an interface, etc.
4. Issues related to rankings
The query speed of relational databases in rankings is generally slow, so you can use the SortedSet of redis to sort hot data.
In the milk tea activity, we need to display the like ranking list of each department, so I made a SortedSet for each department, and then used the user's openid as the username above, and the user's number of likes as For the score above, a hash is made for each user. Through zrangebyscore, the ranking can be obtained according to the number of likes, and then the user's hash information is obtained according to the username. The performance experience of this was quite good in actual application at that time.
5. Distributed lock
This is mainly performed using the setnx command of redis. setnx: "set if not exists" means that if it does not exist, the cache will be successfully set and returned at the same time. 1, otherwise it returns 0. This feature is used in the background of Yunibenyuanfang. Because our server is clustered, the scheduled task may run on both machines, so in the scheduled task, first set a lock through setnx. If the setting is successful, it will be executed. If it is not set successfully, it means that the scheduled task has been executed. Of course, based on the specific business, we can add an expiration time to this lock. For example, if a scheduled task is executed every 30 minutes, then the expiration time can be set to a time less than 30 minutes. This is related to the period of the scheduled task and the execution of the scheduled task. Time consumption related.
Of course we can apply this feature to other scenarios that require distributed locks. Combined with the expiration time, it is mainly to prevent the occurrence of deadlock.
6. Delay operation
I have done relevant tests on this so far, but it has not been applied to our actual projects. Here I will give an application of this feature. Scenes. For example, after the order is produced, we occupy the inventory. After 10 minutes, we will check whether the user has actually purchased it. If there is no purchase, the document will be invalid and the inventory will be restored at the same time. Since redis has provided the Keyspace Notifications function since version 2.8.0, customers are allowed to subscribe to Pub/Sub channels in order to receive events that affect the Redis data set in some way. So we can use the following solution for the above needs. When we order production, we set a key and set it to expire after 10 minutes. We implement a listener in the background to monitor the effectiveness of the key. When the key expires, we will follow up. Logic added. Of course, we can also use the delay queue service of message middleware such as rabbitmq and activemq to achieve this requirement.
7. Paging, fuzzy search
The set collection of redis provides a zrangebylex method, the syntax is as follows:
ZRANGEBYLEX key min max [LIMIT offset count]
Page data query can be performed through ZRANGEBYLEX zset - LIMIT 0 10, where - means to obtain all data
zrangebylex key min max This can return the data in the dictionary interval, use this feature Fuzzy query function can be performed. This is also the only feature I have found in redis that supports fuzzy query on stored content.
A few days ago, I used this feature to conduct a simulation test on school data. The school data is about 600,000, and the response time is about 700ms, which is slightly faster than mysql's like query, but because it can avoid a large number of Database IO operation, so overall it is more conducive to system performance guarantee than direct mysql query.
8. Storage of mutual relationships such as likes and friends
The external functions provided by Redis set are similar to those of list. The special thing is that set can automatically eliminate duplicates. When you need to store a list of data and do not want duplicate data to appear, set is a very useful tool. A good choice, and set provides an important interface for determining whether a member is in a set collection, which list cannot provide. Or in the Weibo application, the people that each user follows exist in a collection, so it is easy to implement the function of finding common friends of two people.
This is used in the milk tea activity, which uses set to store the like relationship between users. In addition, the sismember method is used to determine whether the like has been received before the like. At that time, the response time of this interface was controlled. Within 10 milliseconds, very efficient.
9. Queue
Since redis has commands such as list push and list pop, it can easily perform queue operations.
For more Redis-related technical articles, please visit the Introduction to Using Redis Database Tutorial column to learn!
The above is the detailed content of Where is redis generally used?. For more information, please follow other related articles on the PHP Chinese website!