preface

Netty is a high-performance network framework and tool based on asynchronous events. Common distributed middleware is involved in Netty.

To learn the Netty threading model, understand the three IO models first. This will make it easier to understand the Netty threading model and IO model.

BIO

Synchronous blocking IO model

  • One thread is responsible for the connection, and multiple threads open one thread for each access
  • One request, one reply
  • The client will block until the request is answered

NIO

Synchronize non-blocking I/ OS

IO model of “non-blocking synchronization” based on IO multiplexing technology. In simple terms, the kernel notifies the application of readable and writable events, and the application actively initiates read and write operations.

AIO

Asynchronous non-blocking IO, AIO introduces the concept of asynchronous channel, using Proactor mode, simplify the program writing, effective request to start the thread, its characteristics is completed by the operating system before informing the server program to start the thread to deal with, generally applicable to the number of connections and long connection time application

NIO differs from AIO in that the application does actual read and write operations.

Netty’s threading model is based on the REACTOR model, so let’s first understand the three threading models of reactor

Reactor has three threading models

Reactor single-threaded model

All I/O operations are performed on the same NIO thread. The NIO thread has the following responsibilities:

  • As the NIO server, it receives TCP connections from clients
  • As the NIO client, initiate TCP connections to the server
  • Read the request or reply message of the communication peer
  • Sends a request or reply message to the peer

The Reactor is a single thread, as shown in the figure above: it not only processes accept connection requests from clients, but also dispatches read and write requests to the processor.

The disadvantages of this are also obvious

  1. Performance cannot support a SINGLE NIO thread handling hundreds or thousands of links simultaneously
  2. Once the NIO thread dies, the entire IO model becomes unusable and goes down

One thread is responsible for everything

Reactor multithreaded model

The biggest difference between the Reactor multithreaded model and the single-threaded model is that a group of NIO threads handle I/O operations and a single NIO thread handles Accept. A NIO thread can handle multiple connection events, and a connection event can only belong to one NIO thread.

The reactor thread takes care of THE I/O events by delegating non-IO events to the thread pool

The Reactor multithreaded model meets performance requirements in most scenarios.

However, if client connection requests are overwhelmed within a short period of time, a single Rector must not only process registration events, but also dispatch tasks to the worker thread pool. Dispatch is a time-consuming operation, and a single Acceptor thread may suffer from poor performance and may cause blocking.

Reactor master-slave multithreaded model

Instead of a single NIO thread for the server to receive client connections, there is a separate NIO thread pool.

The primary reactor accepts requests to the secondary reactor, and all of them go through the thread pool, so that hundreds of thousands of requests can be supported simultaneously.

The primary REACTOR receives links, and the secondary REACTOR passes IO events to the thread pool

Netty threading model

The first thing to be clear about is this

“Netty’s threading model is not rigid and depends on how the Reactor’s startup parameters are configured. Netty supports both the single-threaded and multithreaded models by setting different startup parameters.”

Netty’s default threading model is the master-slave NIO model above, which is based on the Reactor model. Usually, one master with multiple slave is used. However, you can set the startup parameters to Multiple master with multiple slave as required.

We can intuitively understand the Netty threading model through the code

Single-threaded model:

private EventLoopGroup group = new NioEventLoopGroup();
ServerBootstrap bootstrap = new ServerBootstrap()
                .group(group)
                .childHandler(new HeartbeatInitializer());
Copy the code

Multithreaded model:

private EventLoopGroup boss = new NioEventLoopGroup(1);
private EventLoopGroup work = new NioEventLoopGroup();
ServerBootstrap bootstrap = new ServerBootstrap()
                .group(boss,work)
                .childHandler(new HeartbeatInitializer());
Copy the code

Master and slave multithreading:

private EventLoopGroup boss = new NioEventLoopGroup();
private EventLoopGroup work = new NioEventLoopGroup();
ServerBootstrap bootstrap = new ServerBootstrap()
                .group(boss,work)
                .childHandler(new HeartbeatInitializer());
Copy the code

We can make a judgment based on the specific business scenario and deal with it flexibly.

My wechat official number: Java Architect Advanced programming focus on sharing Java technology dry products, including JVM, SpringBoot, SpringCloud, database, architecture design, interview questions, e-books, etc., looking forward to your attention!