当前位置:网站首页>The redis keys command should be used with caution in the production environment. It is best to shield it
The redis keys command should be used with caution in the production environment. It is best to shield it
2022-06-23 13:09:00 【Dream ~】
1. First look at the official right keys The description of this command :
Returns all matching key. keys The time complexity of is O(N),N To execute the database under this command key The number of , constant . redis scanning key It's very fast , The entry notebook is about 40 millisecond 100w individual .
Warning ️:keys Used in production environments can only be performed at extremely low frequencies . Catastrophic performance occurs when large databases are executed . If you need to query some key, Consider using SCAN perhaps sets.
2. keys Why is the command so slow ?
Another way to ask , Why? redis Need to traverse all of key To find what we need key Well ?
- Redis yes NoSQL Type database , With hash Data structure storage , Therefore, efficient data query can be realized . and hash Structure is very fast for exact lookup , For fuzzy queries , There's nothing we can do .
- Redis Command execution is single threaded , Only a single command can be executed at one time . A single long command will block subsequent .( Can pass debug sleep 0.1100ms Simulate the execution of long-time commands )
The above two points have caused KEYS Conduct key The query needs to traverse the current db All data for , And when the command execution is completed, subsequent commands will be blocked . So in redis Command executed in , Try to avoid blocking the command for a long time .
3. It is recommended to use Scan Instead of keys
For usage, please refer to :Redis use scan Fuzzy matching key, Avoid blocking _ dream ~'-CSDN Blog
use SCAN cursor [MATCH pattern] [COUNT count] Commands are iterated key Traverse ( Limit the... Of a single query key Number ). This count Not to limit the number of returned results , Instead, it limits the number of dictionary slots that the server can traverse at a single time ( About equal to ). shortcoming :
- The same element may be returned more than once . The work of handling duplicate elements is left to the application , for instance , Consider using the elements returned by the iteration only for operations that can be safely repeated multiple times .
- If an element is added to the dataset during iteration , Or it can be deleted from the dataset during the iteration , So this element might be returned , Maybe not ,redis scan There is only a limited guarantee of the returned result set .
- If the element is deleted during the iteration , May not be returned .
4. Will want to prohibit the command , Add to the configuration file in the following format :
It is recommended to use... Directly in the configuration file redis Of rename Block these high-risk commands :
rename-command FLUSHALL ""
rename-command FLUSHDB ""
rename-command KEYS ""
5. SCAN and KEYS The difference between :
When KEYS When the command is used to process a large database , Or, SMEMBERS The command is used for When dealing with a large set key , They lock redis library , May block the server for several seconds . High concurrency can cause a large number of requests to pile up and cause service avalanche . Some companies directly disable it in the production environment kyes * command . But in redis The server key When the number is small , Use keys There's no problem .
SCAN Orders and related SSCAN command 、 HSCAN Command and ZSCAN Commands are used to iterate incrementally , They return only a few elements each time they execute , It doesn't block the server , So these commands can be used in the production environment , But not like KEYS command 、 SMEMBERS Problems with orders .
SCAN It has its own problems :
- Because it's segmentation key, So it will ask for redis The server , This is bound to take the same key,scan Takes longer .
- In the process of incrementally iterating on the bond , The key may be modified , So the incremental iteration command can only provide limited guarantee for the returned elements .
SCAN cursor [MATCH pattern] [COUNT count]
Integrate the following boss articles :
边栏推荐
- 简历的项目经历,测试人员书写要注意的几个问题
- MySQL使用ReplicationConnection導致的連接失效分析與解决
- Can cold plate, submerged and spray liquid cooling lead the development of high-performance computing?
- State machine framework
- 交换两个数的三种方法原理解析
- 2-optical-2-electric cascaded optical fiber transceiver Gigabit 2-optical-2-electric optical fiber transceiver Mini embedded industrial mine intrinsic safety optical fiber transceiver
- Unity learning day14 -- collaboration and WWW
- Network foundation and framework
- 全新项目,如何保证测试的覆盖率?
- C#学习(高级课程)Day15——异常处理和命名空间
猜你喜欢

Qunhui 10 Gigabit network configuration and test

Unity learning day14 -- collaboration and WWW

那些技术实战中的架构设计方法

Based on your work experience, talk about the quality system construction in software testing

Ablebits Ultimate Suite for Excel

PHP handwriting a perfect daemon

Lm05 former VIX (second generation product)

"Four highs" of data midrange stability | startdt Tech Lab 18
![[website architecture] the unique skill of 10-year database design, practical design steps and specifications](/img/f2/061fa6dd42e57a121401e4f0cf1865.png)
[website architecture] the unique skill of 10-year database design, practical design steps and specifications

解决:Argument type ‘String‘ expected to be an instance of a class or class-constrained type
随机推荐
跟循泰国国内游宣传曲MV,像本地人一样游曼谷
When did the redo log under InnoDB in mysql start to perform check point disk dropping?
How about stock online account opening and account opening process? Is it safe to open a mobile account?
测试人员跳槽到新公司,工作怎样快速上手?
MySQL使用ReplicationConnection導致的連接失效分析與解决
Hanyuan high tech USB2.0 optical transceiver USB2.0 optical fiber extender USB2.0 optical fiber transmitter USB2.0 interface to optical fiber
华三交换机配置SSH远程登录
C#学习(高级课程)Day15——异常处理和命名空间
Hanyuan hi tech 1-channel gigabit optical port to 4-channel Gigabit Ethernet electrical port Gigabit 1-optical 4-electric optical fiber transceiver
16 channel HD-SDI optical transceiver multi channel HD-SDI HD video optical transceiver 16 channel 3g-sdi HD audio video optical transceiver
What if the test time is not enough?
LM05丨曾经的VIX(二代产品)
Follow the promotional music MV of domestic tour in Thailand and travel to Bangkok like local people
RestCloud ETL解决shell脚本参数化
夏日炎炎玩转新加坡:盘点室内景点和夜游好去处
R language uses matchit package for propensity matching analysis (set the matching method as nearest, match the control group and case group with the closest propensity score, 1:1 ratio), and use matc
手机开户有什么风险吗?开户安全吗?
2 万字 + 30 张图 |MySQL 日志:undo log、redo log、binlog 有什么用?
服务稳定性治理
R语言使用MASS包的polr函数构建有序多分类logistic回归模型、使用exp函数和coef函数获取模型中每个变量(自变量改变一个单位)对应的优势比(odds ratio)