当前位置:网站首页>Epoll and IO multiplexing of redis

Epoll and IO multiplexing of redis

2022-07-06 07:47:00 MG-net

One 、redsi Of IO Multiplexing

redis utilize epoll To achieve IO Multiplexing , Put connection information and events in the queue , Once in the file event dispatcher , The event dispatcher distributes events to the event handler .

IO Multiplexing , It uses linux Kernel functions , It can be used in three ways select、poll、epoll,redis What you use is epoll To implement .

stay redis in , Multiple clients connect at the same time ,redis Use epoll, Put the events of each client in the queue , Then the event dispatcher finds the corresponding work unit .IO The solution of multiplexing is to prevent one read and write from blocking the whole process .

Redis Service use Reactor To implement the file event handler , It consists of 4 Parts of :

  • Multiple sockets
  • IO Multiplexing program
  • File event dispatcher
  • Event handler

Because the consumption of the file event dispatcher queue is single threaded , therefore redis It's called the single thread model .

IO Just the Internet IO, Multiplexing is multiple connections 、 Reuse is handled by one or a group of threads TCP Connect , To sum up, a service process can process multiple socket descriptors at the same time (FD).

Two 、 Sync \ Asynchronous and blocking \ Non blocking

1、 Sync \ asynchronous

  • Sync : The caller needs to wait for the result , To perform subsequent operations
  • asynchronous : It refers to the response result returned by the callee , Let the caller go back first , Then process the results , Notify the caller after completion

Sync 、 The asynchronous discussion object is the callee , The key point is to get the message notification of the call result .

2、 Blocking \ Non blocking

  • Blocking : The caller has been waiting to do nothing else , The current thread will be suspended , Nothing to deal with
  • Non blocking : After the call is issued , The caller is free to do other things , Does not block the current thread , And return immediately

Blocking 、 The object of the non blocking discussion is the caller , The focus is on the behavior when waiting for a message , Whether the caller can do other things

3、 Four models

Take the example of a customer going to the bottom of the sea to fish for dinner :

  • Synchronous blocking : The waiter said it was coming , Need to wait , The customer doesn't do anything while waiting
  • Synchronous nonblocking : The waiter said it was coming , Need to wait , Customers play mobile phones and wait
  • Asynchronous blocking : The waiter said , If you need to wait, you can go shopping , But the customer is waiting for nothing
  • Asynchronous non-blocking : The waiter said , If you need to wait, you can go shopping , The customer plays with his mobile phone and waits for the waiter to inform him

In fact, it is a discussion about what is going on between the caller and the callee .

3、 ... and 、Linux The five kinds of IO Model

1、Blocking IO(BIO)

BIO Characteristics , There are two stages in the implementation process , All be block 了 . That is, the caller ( Socket )、 Callees ( system kernel ) It's all blocked .

Blocking position :

  • Server side :accpet() function
  • The server reads the client :read() function

shortcoming :

  • Using multithreading , This will cause the client to start a thread every time it comes
  • Context switching of threads consumes performance

2、NoneBlocking IO(NIO)

NIO Everything is non blocking .

accpet() It's non blocking , Return without client connection error.

read() It's non blocking , Can't read data , Just go back to error, It is blocked when processing the read data .

stay NIO In mode , There's only one thread :

When the client and server are connected , This socket Will be added to the array , Every once in a while , To see this socket Of read() Whether the method can read data , In this way, one thread can handle the connection and reading of multiple requests .

When the user process initiates read During operation , If the data in the kernel is not ready , It doesn't blokcing User process , It's back to a error, So user processes don't need to wait , But the user process needs to constantly take the initiative to ask the kernel whether the data is ready . 

shortcoming :

  • When there are many connections , The array capacity is very large , If there is 100 individual , among 99 There are no data , But we have to traverse
  • There is a switch between user mode and kernel mode in each traversal , Costly

3、IO multipexing(IO Multiplexing )

IO Multiplexing is often said select、poll、epoll, Some places are also called event driven IO, Through the consistency mechanism , A process listens to multiple descriptors , Once a descriptor is ready ( Ready state ), It can inform the user program to read and write ( Loop monitoring is carried out in the kernel ). 

IO Multiplexing , Reuse refers to tracking each... Through records in a single thread scoket To manage IO flow , The purpose is to improve the throughput of the server as much as possible . 

Reactor Pattern , It refers to the time driven processing mode of service requests that are simultaneously passed to the service processor through one or more inputs . And assign them to the corresponding application processor . namely IO More reuse unified monitoring events , Distribute the event to the handler after receiving it .

Linux Three implementations of the kernel :

  • select:
    • technological process :
      • select It's a blocking function
      • When there's data , The corresponding position will be set to 1
      • select return , Not listening
      • Traversal file descriptors fd, Determine which position is set
      • Return the data
    • advantage :
      • Put the traversal into the kernel
      • Use similar bitmap Record which fd There is readable data
    • shortcoming :
      • bitmap Only 1024
      • rset Reset to... Each time 0
      • There are still small cards in which users copy kernel state
      • When there is readable data , You still need to traverse fd, Time complexity n(O)
  • poll:
    • solve the problem :
      • It's solved bitmap1024 problem
      • It's solved rset Non reusable problems
    • shortcoming :
      • through select The last two points are the same
  • epoll:
    • Three functions :
      • epoll_create: Create a handle
      • epoll_ctl: Add file descriptor to listen
      • epoll_wait: Initiate similar select Method call
    • technological process : Put what you need to read and write fd Put it in the ready list , Directly traverse what needs to be handled directly fd

4、 Signal driven and asynchronous IO

and redis Not much to do with , So I'm not explaining .

summary :

原网站

版权声明
本文为[MG-net]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131859379586.html