当前位置:网站首页>Why can redis be so fast?
Why can redis be so fast?
2022-06-11 14:55:00 【Unfortunately, no if o】
Let's make a conclusion first , Why? Redis stand-alone QPS Can achieve 10W?
- Redis Most requests are memory based ;
- Redis Have a simple and efficient data structure ;
- Redis It is based on single thread IO Multiplexed event mechanism ;
For the above Three reasons Conduct Analyze... Item by item :
Redis Most requests are based on memory operations :
- That's true. Memory and disk Of Read/write speed It's not an order of magnitude at all .Redis When started, it will Persistent files Data in Load into memory , and Traditional relational databases about New query Need experience disk IO Of Time consuming .
Redis Have a simple and efficient data structure :
- Redis Have SDS, Linked list , Dictionaries , Skip list , Set of integers , Compressed list etc. Simple data structure , This makes CPU It is very fast to handle the data types encapsulated upward by these data structures ;
Redis It is based on single thread IO Multiplexed event mechanism : ( a key )
- Single thread 、IO Multiplexed event mechanism
Single thread :
Redis Of Single thread refer to The Internet IO Handle and Key-Value Data information processing Sharing a thread ;
Why? Redis Use single thread :
We all know Multithreading Can improve Program throughput , Improve Program Of Degree of concurrency ;
Usually , After we adopt multithreading , without Good system design , The actual result , In fact, it is as shown in the figure on the right . We At first When increasing the number of threads , System throughput will increase , however , When adding more threads , The system throughput grows slowly , Sometimes even falling The situation of .
Why does this happen ? A key bottleneck is , There is usually a problem in the program that is Multithreading At the same time Shared resources , For example, a Shared data variables . When there is When multiple threads want to modify this shared variable , In order to ensure Correctness of shared variables , There needs to be Additional mechanisms to ensure ( such as : Lock ), And this extra mechanism , It will bring Additional overhead .
On the whole Redis Read data based on memory , Process data based on simple data structure , These are all Very fast operation . And introduce Multi process May result in Extra time + Time for multithreading to process simple data > Time for a single thread to process simple data , Obviously Do more harm than good Of . thus Redis Design concept of It is necessary to Single core CPU To the extreme , This is also stand-alone Redis example QPS performance Only in 10W about Root cause of , Because it can't be like Multithreaded program Pass like that Heaped up CPU nucleus Realization High concurrency . However , about Ordinary scenes Come on , The stand-alone performance is also enough With .
IO Multiplexed event mechanism :
It says ,Redis Single thread Need to be used to handle The Internet IO and Key-value Data and information ; (Redis6.0 Version network IO Change to multithreading )
however Tradition Of Network programming model yes Blocking . If according to The design of this blocking model , that Redis Of The main thread Accept to Connection request and waiting for data input , The main thread yes blocked , Can't Handle KV Data and information . Solve this problem , Can be introduced Multithreading , Start a special New Threads Responsible for handling The Internet IO, The main thread only does KV Data processing (Redis6.0 design scheme ),6.0 Before it was IO Multiplexing mechanism , namely One thread Responsible for handling The Internet IO and KV data ;
accept(listenfd) # Received request , Waiting for data input
IO Multiplexing interpretation :
Create a thread for each client , Server side thread resources It's easy to run out .
Of course, there's a smart way , We can be every receive One client Connect after ( Only receive connection requests , The requested data information has not been received ), Put this File descriptor (connfd) Put it in an array .
fdlist.add(connfd);
Then get a new thread to continuously traverse the array , Call the non blocking of each element read Method (read Will not block threads ,accept Blocking threads )
while(1) {
for(fd <-- fdlist) {
if(read(fd) != -1) {
doSomeThing();
}
}
}such , We successfully used a single thread to process multiple client connections .
Do you think it's a little Multiplexing means ?
And we will The traversal is delegated to the operating system kernel , It gives us a function with this effect . We will A batch of file descriptors adopt A system call To the kernel , It's traversed by the kernel layer , real Efficient Solve this problem .
Redis in IO Explanation of multiplexing event mechanism :
Redis Lieutenant general IO Multiplexing And Event mechanism Conduct Integrate Use . stay Linux In the operating system epoll (IO A form of multiplexing ) + Event mechanism ;
The figure below is based on epoll Mechanism Of Redis IO Model . Multiple in the diagram FD It's a connection socket . adopt epoll Mechanism , Give Way The kernel listens to these sockets . here ,Redis Threads do not block in A particular listener or Connected to socket , in other words , It doesn't block the processing of a specific client request . Because of this ,Redis Sure Connect to multiple clients and process requests at the same time , To improve concurrency .
In order to inform when the request arrives Redis Threads ,epoll Provides the basis for Event callback mechanism , That is, for the occurrence of different events , Would call Different handlers .
that , How does the callback mechanism work ?epoll Once it detects FD There are When the data arrives , will Trigger the corresponding event . these Triggered events Will be put into a Event queue And inform Redis Thread to the The event queue is continuously processed . thus ,Redis No need to poll all the time Whether a request actually occurs , This can avoid causing CPU Waste of resources . meanwhile ,Redis When processing events in the event queue , The corresponding handler function will be called , This implements an event based callback .
explain :Redis Received Connection request , hold Connection request hand Operating system kernel , kernel Receive the Request data after Return events to the queue And notify The main thread . The main thread Traverse Event queue , Transfer the corresponding data from Kernel buffer copy to User buffer , And then in User space Yes Request data (get/set) To deal with .
Reference material :
边栏推荐
- B站高管解读财报:疫情对公司长期发展无影响 视频化趋势不可阻挡
- [SystemVerilog interface] ~ interface
- Flutter 3.0 was officially released: it stably supports 6 platforms, and byte jitter is the main user
- Repository Manager之Nexus配置yum仓库
- 思科瑞递交科创板注册:拟募资6亿 年营收2.22亿
- 2022年湖南省安全员-C证考试练习题及在线模拟考试
- Current situation and future development trend of scaffold market in the world and China from 2022 to 2028
- 树莓派知识大扫盲
- Leetcode daily question - Search insertion position
- Turning "passive" into "active", how to build security compliant intelligent products | Q recommendation
猜你喜欢

B站高管解读财报:疫情对公司长期发展无影响 视频化趋势不可阻挡

In depth analysis of "circle group" relationship system design | series of articles on "circle group" technology

Task manager based on Qt development

C language simple webserver

Seven parameters of thread pool and reject policy

Qualcomm WLAN framework learning (29) -- 6GHz overview

Individual income tax rate table

树莓派知识大扫盲

How to manually package your own projects

Knowledge of affairs
随机推荐
简单的C语言版本通讯录
Hashicopy之nomad应用编排方案01
[SystemVerilog interface] ~ interface
架构概念探索:以开发纸牌游戏为例
Lippon instrument software intern interview
Sum of two leetcode numbers
B站高管解读财报:疫情对公司长期发展无影响 视频化趋势不可阻挡
China's technology goes to sea, tidb database's overseas exploration road | interview with excellent technical team
Cisco Rui submitted the registration of sci tech Innovation Board: proposed to raise 600million yuan, with annual revenue of 222million yuan
Vscode virtual environment running file reported an error importerror: DLL load failed: the specified module could not be found
化“被动”为“主动”,如何构建安全合规的智能产品 | Q推荐
Hashicopy之nomad应用编排方案02
What is excess product power? Find the secret key of the second generation cs75plus in the year of the tiger
2022-2028 global and Chinese near field scanning optical microscope (NSOM) market status and future development trend
Backtracking / activity scheduling maximum compatible activities
2022年湖南省安全员-C证考试练习题及在线模拟考试
Social software soul withdraws its IPO application: Tencent is a major shareholder
以 Log4j 为例,如何评估和划分安全风险
Determine whether a string contains the specified string (verified)
2022 Hunan Provincial Safety officer-c certificate examination practice questions and online simulation examination