当前位置:网站首页>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)
- technological process :
- 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
- solve the problem :
- 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
- Three functions :
4、 Signal driven and asynchronous IO
and redis Not much to do with , So I'm not explaining .
summary :
边栏推荐
- How to estimate the number of threads
- [KMP] template
- [cf gym101196-i] waif until dark network maximum flow
- Jerry's ad series MIDI function description [chapter]
- Three treasures of leeks and Chinese men's football team
- P3047 [usaco12feb]nearby cows g (tree DP)
- When the Jericho development board is powered on, you can open the NRF app with your mobile phone [article]
- MES, APS and ERP are essential to realize fine production
- Do you really think binary search is easy
- Relevant introduction of clip image
猜你喜欢
Leecode-c language implementation -15 Sum of three ----- ideas to be improved
leecode-C语言实现-15. 三数之和------思路待改进版
TS 类型体操 之 extends,Equal,Alike 使用场景和实现对比
Basics of reptile - Scratch reptile
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Opencv learning notes 8 -- answer sheet recognition
Esrally domestic installation and use pit avoidance Guide - the latest in the whole network
Ble of Jerry [chapter]
软件测试界的三无简历,企业拿什么来招聘你,石沉大海的简历
Sharing of source code anti disclosure scheme under burning scenario
随机推荐
成为优秀的TS体操高手 之 TS 类型体操前置知识储备
Ble of Jerry [chapter]
数据治理:微服务架构下的数据治理
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Cf1036c class numbers solution
Opencv learning notes 8 -- answer sheet recognition
超级浏览器是指纹浏览器吗?怎样选择一款好的超级浏览器?
Related operations of Excel
Methods for JS object to obtain attributes (. And [] methods)
Typescript void base type
TS 类型体操 之 extends,Equal,Alike 使用场景和实现对比
shu mei pai
合规、高效,加快药企数字化转型,全新打造药企文档资源中心
Typescript variable scope
Ali's redis interview question is too difficult, isn't it? I was pressed on the ground and rubbed
TypeScript接口与泛型的使用
TS类型体操 之 字符串的妙用
智能终端设备加密防护的意义和措施
Basics of reptile - Scratch reptile
octomap averageNodeColor函数说明