Reactor mode of netty high performance architecture

Bobo roast duck 2020-11-13 05:00:29
reactor mode netty high performance


In the discussion Netty Before the architectural pattern of , Let's introduce Reactor Pattern , because Netty Based on this, the architecture pattern of the

Reactor Model is introduced

1. Basic introduction to thread model

Different threading patterns , It has a great impact on the performance of the program , In order to find out Netty Thread mode , Let's talk about it systematically Each thread mode , Finally, take a look at Netty What are the advantages of the threading model .

The existing thread models are : The traditional block I/O Service model Reactor Pattern
according to Reactor The number of threads processing the resource pool is different , Yes 3 A typical implementation

  1. single Reactor Single thread ;
  2. single Reactor Multithreading ;
  3. Master-slave Reactor Multithreading
    Netty Thread mode (Netty Based mainly on Master-slave Reactor Multithreading model Made some improvements , Among them, master and slave Reactor The multithreading model has multiple Reactor)

2. The traditional block I/O Service model

2.1 working principle

 Insert picture description here

explain :

The yellow box indicates the object , The blue box indicates the thread , The white box means (API)

2.2 Model features

Use blocking IO Mode to get the input data , Each connection needs an independent thread to complete the data input , Business processing , The data returned

2.3 Problem analysis

When the concurrency is large , It creates a lot of threads , It takes up a lot of system resources , After the connection is created , If the current thread has no data to read temporarily , The thread will block in read operation , This causes a waste of thread resources

3. Reactor Pattern

For traditional blocking I/O Of the service model 2 Disadvantages , Solution

  1. be based on I/O Reuse model : Multiple connections share a blocking object , The application just needs to wait on a blocking object , There is no need to block and wait for all connections . When a connection has new data to process , The operating system notifies the application , Thread returns... From blocked state , Start business processing Reactor The corresponding name is : 1. Reactor mode 2. Distributor mode (Dispatcher) 3. Notifier mode (notifier)

  2. Reuse thread resources based on thread pool : There's no need to create threads for each connection , After the connection is completed, the business processing task is assigned to the thread for processing , A thread can handle multiple connected businesses .

 Insert picture description here

I/O Reuse combined with thread pool , Namely Reactor Basic design idea of pattern , Pictured :
 Insert picture description here

explain :

  1. Reactor Pattern , The pattern of passing one or more inputs to the service processor at the same time ( Based on event driven )
  2. The server-side program processes multiple incoming requests , And synchronously dispatch them to the corresponding processing thread , therefore Reactor The pattern is also called Dispatcher Pattern
  3. Reactor Mode use IO Reuse listening Events , After receiving the incident , Distribute to a thread ( process ), This is the key to high concurrency of network servers

Reactor In the pattern The core of :

  1. Reactor:Reactor Run... In a separate thread , Responsible for monitoring and distributing Events , Distribute to the appropriate handler for IO Respond to the incident . It's like a company operator , It takes calls from customers and routes them to the right contacts ;

  2. Handlers: Handler execution I/O Event actual event to complete , It's similar to the actual officials in the company the customer wants to talk to .Reactor Respond by scheduling the appropriate handler I/O event , The handler performs non blocking operations .

3.1 single Reactor Single thread

3.1.1 Solutions that

 Insert picture description here

  1. Select It's the front I/O Standard network programming introduced by reuse model API, It can realize that the application program listens for multiple connection requests through a blocking object
  2. Reactor Object passing Select Monitoring client request events , Pass after receiving the event Dispatch distributed
  3. If the connection request event is established , By Acceptor adopt Accept Processing connection requests , And then create a Handler Object processing subsequent business processing after the connection is completed
  4. If the connection event is not established , be Reactor The corresponding Handler In response to a
  5. Handler Will complete Read→ Business processing →Send The complete business process of

Combined with examples : The server side uses a thread to do all the things through multiplexing IO operation ( Include connections , read 、 Writing, etc ), The coding is simple , clear , But if the number of client connections is large , Will not be able to support , Ahead NIO The case belongs to this model .

3.1.2 Analysis of advantages and disadvantages

advantage : Simple model , No multithreading 、 Process of communication 、 Competition issues , All in one thread
shortcoming : Performance issues , Only one thread , Unable to fully play multi-core CPU Performance of .Handler When processing business on a connection , The entire process cannot handle other connection events , Easy to cause performance bottlenecks
shortcoming : Reliability issues , Unexpected thread termination , Or into a dead cycle , It will make the whole system communication module unavailable , Cannot receive and process external messages , Cause node failure

Use scenarios : Limited number of clients , Very fast business processing , such as Redis Time complexity in business processing O(1) The situation of

3.2 single Reactor Multithreading

3.2.1 Schematic diagram

 Insert picture description here

  1. Reactor Object passing select Monitoring client request events , After receiving the incident , adopt dispatch distributed
  2. If a connection request is established , By Acceptor adopt accept Processing connection requests , And then create a Handler Object handles various events after connection
  3. If it's not a connection request , By reactor The distribution call connection corresponds to handler To deal with it
  4. handler Respond to events only , Do not do specific business processing , adopt read After reading data , It will be distributed to the following worker A thread in the thread pool handles business
  5. worker The thread pool allocates independent threads to complete the real business , And return the result to handler
  6. handler After receiving the response , adopt send Return the result to client

3.2.2 Analysis of advantages and disadvantages

advantage : Can make full use of multi-core cpu Handling capacity of
shortcoming : Multithreaded data sharing and access are complex , reactor Handle all event monitoring and response , Running on a single thread , In high concurrency scenarios, performance bottlenecks are easy to occur .

3.3 Master-slave Reactor Multithreading

For single Reactor In multithreaded model ,Reactor Running in a single thread , It is easy to become a performance bottleneck in high concurrency scenarios , It can make Reactor Running in multithreading

3.3.1 Principle analysis

 Insert picture description here

  1. Reactor The main thread MainReactor Object passing select Listen for connection Events , After receiving the incident , adopt Acceptor Handle connection Events
  2. When Acceptor After handling connection events ,MainReactor Assign connections to SubReactor
  3. subreactor Join the connection to the connection queue to listen , And create handler Handle all kinds of events
  4. When something new happens , subreactor Will call the corresponding handler Handle
  5. handler adopt read Reading data , Distribute it to the following worker threading
  6. worker Thread pool allocation is independent worker Thread for business processing , And return the result
  7. handler After receiving the result of the response , Re pass send Return the result to client
  8. Reactor The main thread can correspond to multiple Reactor Sub thread , namely MainRecator You can relate multiple SubReactor

 Insert picture description here

3.3.2 Advantages and disadvantages

advantage : Simple data interaction between parent thread and child thread with clear responsibilities , The parent thread only needs to receive new connections , Subthread completes subsequent business processing .
advantage : Simple data interaction between parent thread and child thread ,Reactor The main thread only needs to pass the new connection to the child thread , Child threads do not need to return data .
shortcoming : Programming complexity is high

Combined with examples : This model is widely used in many projects , Include Nginx Master-slave Reactor Multi process model , Memcached Master slave multithreading ,Netty Support of master-slave multithreading model

3.4 Reactor Summary

3 Patterns are understood by life cases

  1. single Reactor Single thread , The receptionist and the waiter are the same person , All the way to customer service
  2. single Reactor Multithreading ,1 A receptionist , Multiple waiters , The receptionist is only responsible for reception
  3. Master-slave Reactor Multithreading , Multiple receptionists , More than one waiter

Reactor Patterns have the following advantages :

  1. Fast response , It doesn't have to be blocked by a single synchronization time , although Reactor It's still synchronized
  2. It can avoid complicated multithreading and synchronization problems to the greatest extent , And avoid multithreading / Process switching overhead
  3. Good scalability , It's easy to add Reactor To make full use of CPU resources
  4. Good reusability ,Reactor The model itself has nothing to do with the specific event handling logic , It has high reusability
版权声明
本文为[Bobo roast duck]所创,转载请带上原文链接,感谢

  1. [front end -- JavaScript] knowledge point (IV) -- memory leakage in the project (I)
  2. This mechanism in JS
  3. Vue 3.0 source code learning 1 --- rendering process of components
  4. Learning the realization of canvas and simple drawing
  5. gin里获取http请求过来的参数
  6. vue3的新特性
  7. Get the parameters from HTTP request in gin
  8. New features of vue3
  9. vue-cli 引入腾讯地图(最新 api,rocketmq原理面试
  10. Vue 学习笔记(3,免费Java高级工程师学习资源
  11. Vue 学习笔记(2,Java编程视频教程
  12. Vue cli introduces Tencent maps (the latest API, rocketmq)
  13. Vue learning notes (3, free Java senior engineer learning resources)
  14. Vue learning notes (2, Java programming video tutorial)
  15. 【Vue】—props属性
  16. 【Vue】—创建组件
  17. [Vue] - props attribute
  18. [Vue] - create component
  19. 浅谈vue响应式原理及发布订阅模式和观察者模式
  20. On Vue responsive principle, publish subscribe mode and observer mode
  21. 浅谈vue响应式原理及发布订阅模式和观察者模式
  22. On Vue responsive principle, publish subscribe mode and observer mode
  23. Xiaobai can understand it. It only takes 4 steps to solve the problem of Vue keep alive cache component
  24. Publish, subscribe and observer of design patterns
  25. Summary of common content added in ES6 + (II)
  26. No.8 Vue element admin learning (III) vuex learning and login method analysis
  27. Write a mini webpack project construction tool
  28. Shopping cart (front-end static page preparation)
  29. Introduction to the fluent platform
  30. Webpack5 cache
  31. The difference between drop-down box select option and datalist
  32. CSS review (III)
  33. Node.js学习笔记【七】
  34. Node.js learning notes [VII]
  35. Vue Router根据后台数据加载不同的组件(思考->实现->不止于实现)
  36. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  37. 【JQuery框架,Java编程教程视频下载
  38. [jQuery framework, Java programming tutorial video download
  39. Vue Router根据后台数据加载不同的组件(思考->实现->不止于实现)
  40. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  41. 【Vue,阿里P8大佬亲自教你
  42. 【Vue基础知识总结 5,字节跳动算法工程师面试经验
  43. [Vue, Ali P8 teaches you personally
  44. [Vue basic knowledge summary 5. Interview experience of byte beating Algorithm Engineer
  45. 【问题记录】- 谷歌浏览器 Html生成PDF
  46. [problem record] - PDF generated by Google browser HTML
  47. 【问题记录】- 谷歌浏览器 Html生成PDF
  48. [problem record] - PDF generated by Google browser HTML
  49. 【JavaScript】查漏补缺 —数组中reduce()方法
  50. [JavaScript] leak checking and defect filling - reduce() method in array
  51. 【重识 HTML (3),350道Java面试真题分享
  52. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  53. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  54. [re recognize HTML (3) and share 350 real Java interview questions
  55. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  56. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  57. 【重识 HTML ,nginx面试题阿里
  58. 【重识 HTML (4),ELK原来这么简单
  59. [re recognize HTML, nginx interview questions]
  60. [re recognize HTML (4). Elk is so simple