Netty's threading model

Reactor single-threaded model: all IO operations all in a complete NIO thread above, NIO thread is responsible for:

As the NIO server, the client receives a TCP connection

As a NIO client initiates a TCP request to the server

Read request or a communication peer response message

Sending a request or a response message to the communication peer

Since Reactor pattern using asynchronous non-blocking group I / O, all I / O operations will not cause blocking, in theory, a separate thread may handle all I / O operations associated. From an architectural perspective, a really NIO thread can complete its duties assumed. For example, TCP connection receiving client via Acceptor class request message, when the link is successfully established, by Dispatch corresponding ByteBuffer distributed to the specified Handler, a message decoding, the user thread message encoding to send messages to the customer by NIO thread end.

In some small-capacity scenarios, you can use single-threaded model. But it is for high load, high concurrency scenarios was inappropriate, the main reasons are as follows:

1: a thread to handle hundreds of NIO link can not support the performance, even if the CPU load NIO thread 100%, can not meet the massive message encoding, decoding, reading and transmission.

2: When the NIO thread overloaded, processing speed will slow down, which causes a large number of client connection timeout, timeout after tend to be retransmitted, which is even more heavy load NIO thread will eventually lead to a large number of messages and processing backlog overtime, became the system performance bottleneck

3: Reliability issues: Once NIO thread unexpectedly running out, or into the loop, can cause the entire system communication modules are not available to receive and process external message, causing node failure

 

To address these issues, the evolution of the Reactor multi-threaded model

The biggest difference Reactor multi-threaded and single-threaded model is a model with a set of threads to handle the IO operation NIO

Reactor features multi-process model are as follows:

1: There is a special NIO thread TCP connection request --Acceptor thread used to monitor the server, the client receives

2: Network I / O operations - read, write, etc. responsible NIO a thread pool, thread pool may be employed to achieve the standard JDK thread pool comprising N and a task queue of available threads, these threads are responsible for message NIO reading, decoding, encoding and transmitting

3: a thread NIO N links can be processed simultaneously, but only a link corresponding to a NIO thread from concurrent problems

  

   In most scenarios, Reactor multi-threading model to meet the performance requirements, but in a special scene, a NIO thread is responsible for monitoring and handling all client connections may Aberdeen in performance problems, such as one million concurrent client connections, or the server requires the client security authentication handshake, but the certification itself is loss performance. In such scenarios, a single thread can be problematic Acceptor performance of a foot, in order to solve performance problems, resulting in a third Reactor threading models - from the Reactor master multi-threaded model.

 

Reactor master-slave multi-threaded model

Reactor thread features from the master model is: the service terminal for receiving a client connection is no longer a separate thread NIO, but an independent NIO thread pool. Acceptor receives TCP connection requests to the client and ends the process after completion of an I (may include an access authentication), the newly created SocketChannel register to I / O thread pool (sub reactor thread pool) / 0 thread by its responsible for reading and writing and codecs work of SocketChannel. Acceptor thread pool is only used to log the client, handshaking and safety certification, once the link is established, the link will be registered on the IO thread pool thread backend subReactor, responsible for follow-up by the IO IO operations.

NIO thread using the master-slave model, you can solve a server monitor thread can not effectively deal with the problem of inadequate performance of all client connections, because Netty's official website Demo, it is recommended to use the threading model

Published 50 original articles · won praise 2 · Views 2316

Guess you like

Origin blog.csdn.net/eafun_888/article/details/96567707