当前位置:网站首页>[re understand the communication model] the application of reactor mode in redis and Kafka
[re understand the communication model] the application of reactor mode in redis and Kafka
2022-07-07 17:37:00 【Lin like】
Re understand the communication model
Each framework has its own communication model , Used to handle network events . It's just that different frameworks depend on their own priorities , The requirements and implementation of network communication are different .
The development of our network communication mode :
- Single thread : Only one request can be processed at a time , Other requests are blocked , Low processing efficiency ;
- Multithreading : One request, one connection , Create a large number of threads , It brings thread switching and maintenance problems , High system complexity ;
- Thread pool : Thread reuse , Thread management , But thread pool resources are limited , There are also thread waiting problems .
- Reactor: Modern high-performance Networks IO Pattern , Use the event driven mechanism .
0,Reactor
Reactor Pattern , Reactor mode , It is an event driven network request processing mode , It is implemented in many high-performance network communication frameworks .
stay Reactor In the pattern , Divide the network processing requests into one event , Specifically, it includes reading events 、 Write events 、 Connection event .
In this mode , There are three key components :reactor、acceptor、handler, Respectively correspond to listening and distributing requests , Connect requests and process requests .
The basic flow : Client requests are first reactor receive ,reactor Distribute the request according to the request type , The connection request will be distributed to acceptor Components , He always monitors the network status , For read requests and write requests, it is given to handler Handle .
meanwhile , According to the different ways of handling ,reactor Modes can be divided into many kinds :
single reactror Single thread : One reactor, One handler;
shortcoming : Single thread , The processing capacity is slow ( Compared with these three ),redis6.0 This method was used before , such as keys * It is easy to block
single reactror Multithreading : One reactor,handler Drop the request to worker Thread pool , Read / write events are handled by the thread pool
Take full advantage of multi-core capabilities ,redis6.0 The multi threading mechanism introduced is this mode , however redis The logic of specific command execution is still single thread .
many reactor Multithreading :mainReactor be responsible for acceptor Handle ,subReactors be responsible for handler Handle
You can see that the performance is very strong ,kafka,netty Have used this mode .
1,Redis Medium Reactor
redis in reactor The mode is embodied in IO Based on multiplexing , say concretely redis For different os Of IO Multiplexing mechanism realizes different reactor Model , such as mac os Of kqueue; Let's mainly talk about Redis be based on Linux Of epoll Functionally implemented reactor Pattern . Be careful : Only epoll It adopts the idea of event driven .
that redis How to reuse epoll What about ?
First through a while Loop constantly listening for network events , Implement different processing strategies according to different events . There are three main types of events , Empty event , Continue polling ; Time event , Call the time handler ; Read and write events , You need to call os Of io The multiplexing mechanism performs event capture . Here is a brief mention of multiplexing functions epoll workflow :
epoll First, through epoll_ctl Function to register a file descriptor , And listen for other file descriptor States , If a fd be ready , The callback mechanism will be adopted callback, Activate this fd And put it in the ready list , Then it is handled by specific threads .
2,Kafka Medium Reactor
kafaka It is also based on Reactor Pattern designed its own network communication framework , Bottom use NIO Medium selector function .
First ,broker There was a stockerserver Will listen and dispatch requests ,acceptor Processing connection requests , Rotate and select one processor Handle this connection . This network thread is handled by parameters num.network.threads=3 control . And then in IO Thread by work Thread specific execution ,num.io.threads=8. After execution , Will reponse Put it in the response queue , And then back to the client . among , Each network thread has its own response queue .
besides ,kafka Added a cache layer ,RequestChannel, Used to cache those delayed requests , Used to handle settings ack=all Of produce request
Some details in the source code :
#SocketServer, Managed two sets acceptor,processor,RequestChannel; Including data requests data-plane, Control request control-plane;
#KafkaRequestHandlerPool,IO Thread pool , The thread that actually processes the request ,KafkaRequestHandler Match and process different requests .
kafka The request process of is the process of network communication , Let's talk about it again , To handle different requests ,kafka Priority the request . It is divided into
Data request : Such as produce request , Write messages to disk ;fetch request , Pull messages from disk or cache pages
Control request : Such as updating the copy and ISR Of leaderAndlsr request , And ordering copies offline stopReplica Request etc.
The benefit of request priority is , Some requests can be processed first , Prevent invalid request processing . such as , When deleting a topic , We will use stopreplica request , So even if it's ack=all Under the circumstances , There is no need to wait ack Complete , You can execute the deletion request , Speed up the process , Avoid invalid operations . The implementation mechanism is , Start the two request processing models mentioned above , And configure different listeners Listen for the request , Then the specified .
Reference link :
select、poll、epoll Detailed analysis of
边栏推荐
- 本周小贴士#134:make_unique与私有构造函数
- Flash build API Service - generate API documents
- L1-027 出租(Lua)
- How to choose the appropriate automated testing tools?
- Functions and usage of viewflipper
- Establishment of solid development environment
- Smart logistics platform: make overseas warehouses smarter
- 专精特新软件开发类企业实力指数发布,麒麟信安荣誉登榜
- Enum + Validation 的个人最佳实践 demo 分享
- 麒麟信安云平台全新升级!
猜你喜欢

Sator推出Web3游戏“Satorspace” ,并上线Huobi

Functions and usage of viewflipper

【网络攻防原理与技术】第6章:特洛伊木马

Dateticket and timeticket, functions and usage of date and time selectors

Sator推出Web3遊戲“Satorspace” ,並上線Huobi

Is AI more fair than people in the distribution of wealth? Research on multiplayer game from deepmind

PLC: automatically correct the data set noise, wash the data set | ICLR 2021 spotlight

textSwitch文本切换器的功能和用法

99%的人都不知道|私有化部署还永久免费的即时通讯软件!

datepicket和timepicket,日期、时间选择器的功能和用法
随机推荐
【TPM2.0原理及应用指南】 9、10、11章
鲲鹏开发者峰会2022 | 麒麟信安携手鲲鹏共筑计算产业新生态
Functions and usage of viewflipper
Mysql 索引命中级别分析
【网络攻防原理与技术】第7章:口令攻击技术 第8章:网络监听技术
alertDialog創建对话框
本周小贴士#134:make_unique与私有构造函数
The computer cannot add a domain, and the Ping domain name is displayed as the public IP. What is the problem? How to solve it?
第3章业务功能开发(用户登录)
MySQL usage notes 1
Sator推出Web3遊戲“Satorspace” ,並上線Huobi
actionBar 导航栏学习
[video / audio data processing] Shanghai daoning brings you elecard download, trial and tutorial
With the latest Alibaba P7 technology system, mom doesn't have to worry about me looking for a job anymore
Lex & yacc of Pisa proxy SQL parsing
无法链接远程redis服务器(解决办法百分百)
The server is completely broken and cannot be repaired. How to use backup to restore it into a virtual machine without damage?
【网络攻防原理与技术】第1章:绪论
从DevOps到MLOps:IT工具怎样向AI工具进化?
专精特新软件开发类企业实力指数发布,麒麟信安荣誉登榜