Home > Common Problem > What to do if redis blocks

What to do if redis blocks

(*-*)浩
Release: 2019-06-04 17:42:02
Original
2965 people have browsed it

Redis' event loop is processed in a thread. As a single-threaded program, it is important to ensure that the event processing delay is short, so that subsequent tasks in the event loop will not be blocked; when the amount of redis data reaches After a certain level (such as 20G), blocking operations have a particularly serious impact on performance;

What to do if redis blocks

Let’s summarize the time-consuming scenarios in redis and how to deal with them Method;

Long-time-consuming commands cause blocking

keys, sort and other commands

keys command is used to find all files that match the given pattern The time complexity of pattern key is O(N), and N is the number of keys in the database. When the number of items in the database reaches tens of millions, this command will cause the read and write threads to block for several seconds; similar commands include operations such as sunion sort; what if operations such as keys and sort must be used in business requirements?

Solution:

In the architecture design, there is a trick of "diversion", which means to separate the requests that are processed quickly and the requests that are processed slowly. Otherwise, the slow one will affect the fast one, making it impossible for the fast one to get up; this is very obvious in the design of redis. The pure memory operation of redis and epoll non-blocking IO event processing can be done in one thread. , and time-consuming operations such as persistence, AOF rewriting, and Master-slave synchronization data should be handled by a separate process, so as not to affect the speed of the slow process; similarly, since time-consuming operations such as keys need to be used, then we can Strip them out, such as opening a separate redis slave node, specifically for time-consuming operations such as keys and sorting. These queries are generally not online real-time businesses. The slower the query, the slower it is. The main thing is to complete the task. , but has no impact on online and fast-consuming tasks;

smembers command

smembers command is used to obtain the complete set, and the time complexity is O(N) , N is the number in the collection; if tens of millions of data are stored in a collection, retrieval will also cause the event processing thread to be blocked for a long time;

Solution:

Different from commands such as sort and keys, smembers may be a command that is used very frequently in online real-time application scenarios. Diversion is not suitable here. We need to consider it more from the design level. ; During design, we can control the number of sets and generally keep the number of sets within 500; For example, one key is used to store records for one year, and the amount of data is large. We can use 12 keys to store records for 12 months. records, or 365 keys to save records for each day, and control the size of the collection within an acceptable range;

If it is not easy to divide the collection into multiple sub-collections, insist on using one large collection to store , then you can consider using SRANDMEMBER key [count] when fetching the set; randomly returns the specified number in the set. Of course, if you want to traverse all elements in the set, this command is not suitable;

The above is the detailed content of What to do if redis blocks. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template