【Netty】 thread model

tags: Netty  java  Thread model  


Single -threaded model (single Reactor single thread)
Multi -threaded model (single Reactor multi -threaded)
Mainly and secondary multi -threaded model (multi -Reactor multi -thread)

1. Single Reactor single thread


All operations are processed in the same NIO thread. In this single thread, it is necessary to take responsibility for receiving requests, processing IO, and encoding all operations, which is equivalent to only one person in a restaurant. At the same time, it is responsible for the front desk and background services.

  1. A NIO thread processing hundreds of thousands of connections at the same time, the performance cannot be supported. Even if the CPU load of the NIO thread reaches 100%, it cannot meet the encoding, decoding, reading and sending of massive information.
    2. When the NIO thread load is too heavy, the processing speed will slow down, which will cause a large number of clients to connect the timeout. After the overtime, it will often be re -issued. Timeout becomes the performance bottleneck of the system.
    3. Reliability issues: Once the NIO thread runs unexpectedly, or enter the dead cycle, it will cause the entire system communication module to be unavailable and cannot receive and process external messages, causing node failures.

Use scenario: The number of connections on the client is limited, and the business thread processing time can be used to maintain multiple different connections with a single thread, which means that there are Redis.

Netty related code

EventLoopGroup bossGroup = new NioEventLoopGroup(1);
ServerBootstrap bootstrap = new ServerBootstrap();

advantage:
No context switching, no thread security problem

shortcoming:
1. There is only one thread processing, and the efficiency of the CPU multi -core cannot be used
2. If there are more requests, it is easy to encounter bottlenecks
3. Some accidents are terminated, resulting in the entire system module cannot be used.

There are certain restrictions on the number of client connections, and the business processing time is very fast.

2. Single Reactor Multi -thread


It is equivalent to a restaurant with a front desk responsible for reception, and many waiters to do the latter work, so that the efficiency is much higher than the single -threaded model.

1. The Reactor thread-ACCEPTOR thread is used to monitor the server, receiving the client connection request;
2. Network I/O operation reading, writing, etc. are handled by the Reactor thread pool;
3. One Reactor thread can handle multiple links at the same time, but a link can only correspond to a reactor thread, which can avoid concurrent operation problems;
4. In most scenarios, the Reactor multi -threaded model can meet performance needs. However, in extremely special special scenarios, a Reactor thread is responsible for monitoring and processing all client connections may have performance problems. For example, a million client connection, or the server requires a security certification of the client handshake, but the certification itself is very losing to performance. Therefore, the third thread model was born;

Netty settings related code

EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup(2);
ServerBootstrap bootstrap = new ServerBootstrap();

Create 1 thread Bossground thread group. This thread is responsible for handling the client's connection request, and WorkerGroup uses the number of threads of the processor to handle the number of threads to handle I/O operations by default. This is equivalent to the multi -threaded model of Reactor.

advantage
You can make full use of multi -threaded multi -core processing capabilities, and the processing efficiency is faster than single -threaded

shortcoming:
Access data between threads may encounter thread security issues.

3. Reactor Main Strike Model

The disadvantage of the multi -threaded model is that when the amount of concurrency is high, there is only one Reactor single thread to deal with it. It is not enough to have only one front desk reception at the restaurant. To this end, the main and slab model needs to be used.
Mainly -consisting model: A set of thread pools receive requests, and a set of thread pools handle IO.

1. The server uses an independent Reactor thread pool to handle the client connection. When the server receives a connection request, a Reactor thread is randomly selected from the main thread pool as the accessor thread processing connection;
2. After the link was successfully established, register the newly created SocketChannel to a reactor thread at the Sub Reactor thread pool, and it will process the subsequent I/O operation.

1. MainReactor is responsible for listening to the Server Socket to handle the establishment of new connections. The SocketChannel is specified to the SocketChannel to SubremeActor.
2.SubReactor maintains its selector, and the SocketChannel multi -road separation IO reading and writing event is separated based on the SocketChannel registered, read and write network data, and the functions of business processing are thrown to the worker thread pool to complete.

It is easy to understand as: MainReactor is the boss, only responsible for the interface, and the SubremeActor is responsible for the front desk reception, that is, multiple front desk receptions.

Compared to multi -threaded single RECTOR models, it is divided into two parts. MainReactor is responsible for monitoring and accepting new connections, and then the established socket is assigned to Subjector through the ACCEPTOR. SUBREACTOR is responsible for multi -way separation socket, read and write network data; business processing function, it is handed over to the Worker thread pool to complete. Usually, the number of Subreactor can be equal to the number of CPUs.

Intelligent Recommendation

Netty features and thread model

Zero copy hard driver - kernel buffer - protocol engine only DMA copy avoids cpu copy There was actually a cpu copy of kernel buffer - socket buffer, but the copied information can rarely be ignored; ...

013. NETTY thread model

Introduction to Netty Netty is a high-performance, high-scalable asynchronous event-driven network application framework, which greatly simplifies network programming such as TCP and UDP clients and s...

Netty thread model and basics

Why use Netty Netty is an asynchronous event-driven web application framework for rapid development of maintainable high-performance and high-profile servers and clients. Netty has the advantages of h...

Netty thread model and gameplay

Event cycle group   All I / O operations in Netty are asynchronous, and the asynchronous execution results are obtained by channelfuture. Asynchronously executes a thread pool EventLoopGroup, it ...

Netty - Thread Model Reactor

table of Contents Thread model 1, traditional IO service model 2, Reactor mode reactor Three modes: to sum up Netty model Excommissum Thread model 1, traditional IO service model Blocked IO mode Get i...

More Recommendation

Netty thread model [next]

Hey everyone, I amJava small white 2021。 The programmer of the halfway is in the development of aircraft, and the opportunity to find a more interesting thing under the development of a surveying cour...

Netty entry thread model

Single-threaded model: the boss thread is responsible for connection and data reading and writing Hybrid model: the boss thread is responsible for connection and data reading and writing, and the work...

Redis source code parsing: 09redis database implementations (key operation, key timeout function key space notification)

This chapter of the Redis database server implementations are introduced, indicating achieve Redis database-related operations, including key-value pairs in the database to add, delete, view, update a...

PAT B1041-B1045 Question

1、b1041 2、b1042 3、b1043 4、b1044 5、b1045...

Sorting Algorithm - Overview

Seven commonly used sorting algorithm: 1w integers sorted tested Explain the principles of: 1, direct insertion sort be a sorted array A, an empty array B, once the elements A B into a specific positi...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top