当前位置:网站首页>Want to do something in production? Then try these redis commands
Want to do something in production? Then try these redis commands
2022-07-04 03:14:00 【Java geek Technology】
Ah , Recently, ah fan once again has committed a crime .
Here's the thing , Some time ago, a powder company occasionally reported errors in production transactions , After a lot of investigation, the final reason is that Redis Command execution timeout .
But what is puzzling is , Production transactions only use Redis set This simple command , This command is reasonable. It can't be executed so slowly .
What is the cause of this problem ?
To find out the problem , We looked at and analyzed Redis Recent slow logs , Most of them are time-consuming keys XX*
See the prefix of the key operated by this command , A fan just found that this is his own responsible application . But a fan checks , Although their own code did not take the initiative to use keys command , But the underlying framework is used indirectly , So we have the question of today .
Question why
The application that a fan is responsible for is a management background application , Permission management uses Shiro frame , Because there are multiple nodes , You need to use distributed Session, So here we use Redis Storage Session Information .
*
Voice over : I don't know about distributed Session , You can see what ah fan wrote before Say in one breath 4 Distributed consistency Session Realization way , It's a tough interview ~
*
because Shiro It's not directly provided Redis Storage Session Components , A fan has to use Github An open source component shiro-redis.
because Shiro The framework needs to be validated periodically Session Whether it works , therefore Shiro The underlying layer will call SessionDAO#getActiveSessions Get all Session Information .
and shiro-redis Just inherit SessionDAO This interface , For bottom use keys Command find Redis All stored Session key.
public Set < byte[] > keys(byte[] pattern){
checkAndInit();
Set < byte[] > keys = null;
Jedis jedis = jedisPool.getResource();
try{
keys = jedis.keys(pattern);
}finally{
jedis.close();
}
return keys;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
Find the cause of the problem , The solution is simpler ,github Find solutions on , Upgrade shiro-redis To the latest version .
In this version ,shiro-redis use scan Command instead of keys, To fix the problem .
public Set < byte[] > keys(byte[] pattern) {
Set < byte[] > keys = null;
Jedis jedis = jedisPool.getResource();
try{
keys = new HashSet < byte[] >();
ScanParams params = new ScanParams();
params.count(count);
params.match(pattern);
byte[] cursor = ScanParams.SCAN_POINTER_START_BINARY;
ScanResult < byte[] > scanResult;
do{
scanResult = jedis.scan(cursor,params);
keys.addAll(scanResult.getResult());
cursor = scanResult.getCursorAsBytes();
}while(scanResult.getStringCursor().compareTo(ScanParams.SCAN_POINTER_START) > 0);
}finally{
jedis.close();
}
return keys;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
Although the problem has been solved successfully , But ah fan was still a little confused .
Why? keys Instructions can slow down the execution of other commands ?
Why? Keys Instruction queries can be so slow ?
Why? Scan There's no problem with instructions ?
Redis The principle of executing commands
First let's look at the first question , Why? keys Instructions can slow down the execution of other commands ?
Answer the question , Let's take a look at Redis The client executes a command :

From the perspective of the client , There are three steps to executing a command :
- dispatch orders
- Carry out orders
- Return results
But this is just what the client thinks it is , But actually at the same time , There may be many clients sending commands to Redis, and Redis We all know it's a single threaded model .
In order to handle all client requests at the same time ,Redis The internal use of queues , Queued execution .

So it takes four steps for the client to execute a command :
- dispatch orders
- Order to line up
- Carry out orders
- Return results
because Redis Single thread command execution , You can only take the task out of the queue in order to start execution .
as long as 3 This process is too slow to execute commands , Other tasks in the queue have to wait , This looks to external clients ,Redis It's like it's blocked , There has been no response .
So use Redis The process must not execute instructions that need to run for a long time , This may lead to Redis Blocking , Affect the execution of other instructions .
KEYS principle
Now I'm going to answer the second question , Why? Keys Instruction queries can be so slow ?
Before I answer that question , Please think about it Redis The underlying storage structure .
It doesn't matter if you don't know your friend's , You can go back to a fan's previous article 「 Interview officer Ali :HashMap Be familiar with it. ? well , Let's talk about it Redis Dictionary !」.
Here a fan copies the content of the previous article ,Redis The bottom layer uses the dictionary structure , This structure and Java HashMap The bottom layer is similar to .

keys The command needs to return all that conform to the given pattern pattern Of Redis In the key , To achieve this goal ,Redis I have to go through the dictionary ht[0] Hash table underlying array , This time complexity is 「O(N)」(N by Redis in key All the numbers ).
If Redis in key The quantity is very small , Then the execution speed will be very fast . wait until Redis key The number of them is gradually increasing , Up to a million 、 Ten million 、 Even hundreds of millions of levels , Then the execution speed will be very slow .
The following is an experiment done by ah fan , Use lua Script to Redis add 10W individual key, And then use keys Query all keys , This query will be blocked for about ten seconds .
eval "for i=1,100000 do redis.call('set',i,i+1) end" 0
- 1.
*
Here a fan uses Docker Deploy Redis, The performance may be slightly worse .
*
SCAN principle
Finally, let's look at the third question , Why? scan There's no problem with instructions ?
This is because scan Order a black Technology -「 Cursor based iterators 」.
Every time you call scan command ,Redis A new cursor and a certain number of key. Next time you want to continue to get the rest of key, You need to pass this cursor into scan command , To continue the previous iteration process .
simply ,scan The command uses pagination to query redis .
Here's a scan Example of an iterative process for a command :
scan Commands use cursors , Split a full query into many times , Reduce query complexity .
although scan Command time complexity and keys equally , All are 「O(N)」, But because of scan The command only needs to return a small number of key, So the execution speed will be very fast .
Last , although scan Order to solve keys Insufficient , But it also introduces other defects :
- The same element may be returned more than once , This requires our application to increase the ability to handle duplicate elements .
- If an element is added to redis, Or deleted in the iteration process , That element will be returned , Maybe not .
The above defects , We need to consider this situation in our development .
except scan outside ,redis There are several other commands for incremental iteration :
-
sscan: Used to iterate the database key in the current database , For resolution smembers There may be blocking problems -
hscan The command is used to iterate over key value pairs in hash keys , For resolution hgetall There may be blocking problems . -
zscan: The command is used to iterate over elements in an ordered set ( Include element members and element scores ), Used to produce zrange There may be blocking problems .
summary
Redis Use a single thread to execute operation commands , All clients send commands ,Redis Will now be put in the queue , And then take them out of the queue and execute the corresponding commands .
If any task is too slow , It will affect other tasks in the queue , In the external client's view , It's too late to get Redis Response , It looks like it's blocked .
So don't execute in production keys、smembers、hgetall、zrange This kind of instruction may cause blocking , If you really need to execute , You can use the corresponding scan Command progressive traversal , Can effectively prevent blocking problems .
< END >
If you like our article , Welcome to forward , Click "watch" to let more people see .

边栏推荐
- How to use websocket to realize simple chat function in C #
- PID of sunflower classic
- Record a problem that soft deletion fails due to warehouse level error
- Johnson–Lindenstrauss Lemma
- Is online futures account opening safe and reliable? Which domestic futures company is better?
- 基於.NetCore開發博客項目 StarBlog - (14) 實現主題切換功能
- Zblog collection plug-in does not need authorization to stay away from the cracked version of zblog
- WP collection plug-in free WordPress collection hang up plug-in
- Explain AI accelerator in detail: why is this the golden age of AI accelerator?
- Redis transaction
猜你喜欢

Package and download 10 sets of Apple CMS templates / download the source code of Apple CMS video and film website

Practical multifunctional toolbox wechat applet source code / support traffic master

Unity knapsack system (code to center and exchange items)

Contest3145 - the 37th game of 2021 freshman individual training match_ 1: Origami

What are the virtual machine software? What are their respective functions?

The difference between MCU serial communication and parallel communication and the understanding of UART

Solve the problem that the tabbar navigation at the bottom of vantui does not correspond to the page (window.loading.hash)

Node write API

Record a problem that soft deletion fails due to warehouse level error

Jenkins continuous integration environment construction V (Jenkins common construction triggers)
随机推荐
Baijia forum the founding of the Eastern Han Dynasty
2022 Guangxi provincial safety officer a certificate examination materials and Guangxi provincial safety officer a certificate simulation test questions
Bugku Zhi, you have to stop him
MySQL data query optimization -- data structure of index
Li Chuang EDA learning notes IX: layers
Constantly changing harmonyos custom JS components during the Spring Festival - Smart Koi
Contest3145 - the 37th game of 2021 freshman individual training match_ 1: Origami
Libcblas appears when installing opencv import CV2 so. 3:cannot open shared object file:NO such file or directory
New year's first race, submit bug reward more!
Database concept and installation
機器學習基礎:用 Lasso 做特征選擇
Optimization theory: definition of convex function + generalized convex function
Recent learning fragmentation (14)
[Yugong series] February 2022 attack and defense world advanced question misc-84 (MySQL)
[latex] production of complex tables: excel2latex and detail adjustment
1day vulnerability pushback skills practice (3)
POSTECH | option compatible reward reverse reinforcement learning
(practice C language every day) pointer sorting problem
Backpropagation formula derivation [Li Hongyi deep learning version]
JS object definition