当前位置:网站首页>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

  1. Redis Most requests are memory based ;
  2. Redis Have a simple and efficient data structure ;
  3. 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 .

chart 1

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 .

chart 2

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 .

chart 3

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 .

chart 4

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 :

原网站

版权声明
本文为[Unfortunately, no if o]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203012040060579.html