Where is redis generally used?
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

Using Redis to lock operations requires obtaining the lock through the SETNX command, and then using the EXPIRE command to set the expiration time. The specific steps are: (1) Use the SETNX command to try to set a key-value pair; (2) Use the EXPIRE command to set the expiration time for the lock; (3) Use the DEL command to delete the lock when the lock is no longer needed.

On CentOS systems, you can limit the execution time of Lua scripts by modifying Redis configuration files or using Redis commands to prevent malicious scripts from consuming too much resources. Method 1: Modify the Redis configuration file and locate the Redis configuration file: The Redis configuration file is usually located in /etc/redis/redis.conf. Edit configuration file: Open the configuration file using a text editor (such as vi or nano): sudovi/etc/redis/redis.conf Set the Lua script execution time limit: Add or modify the following lines in the configuration file to set the maximum execution time of the Lua script (unit: milliseconds)

Use the Redis command line tool (redis-cli) to manage and operate Redis through the following steps: Connect to the server, specify the address and port. Send commands to the server using the command name and parameters. Use the HELP command to view help information for a specific command. Use the QUIT command to exit the command line tool.

In Debian systems, readdir system calls are used to read directory contents. If its performance is not good, try the following optimization strategy: Simplify the number of directory files: Split large directories into multiple small directories as much as possible, reducing the number of items processed per readdir call. Enable directory content caching: build a cache mechanism, update the cache regularly or when directory content changes, and reduce frequent calls to readdir. Memory caches (such as Memcached or Redis) or local caches (such as files or databases) can be considered. Adopt efficient data structure: If you implement directory traversal by yourself, select more efficient data structures (such as hash tables instead of linear search) to store and access directory information
