[netty foundation 4] netty and nio

1. Reactor

1. Blockage model

Blocking I/O is blocked when the InputStream.read() method is called , it will not return until the data arrives (or times out);

Similarly, when the ServerSocket.accept() method is called, it will be blocked until there is a client connection before returning. After each client connection is successful, the server will start a thread to process the client's request.

The communication model of blocking I/O is shown in the figure below.
insert image description here

Two disadvantages of the blocking I/O communication model.

  1. When there are many clients, a large number of processing threads will be created. And each thread takes up stack space and some CPU time. (Some requests without data will cause the stack space and cpu to be occupied all the time)
  2. Blocking may cause frequent context switching, and most context switching may be meaningless.

 
context switch

The CPU uses time slice polling to serve each task for a certain period of time, then saves the status of the current task and continues to serve the next task. The state saving and reloading of tasks is called context switching of threads.

 

2. How Java NIO works

  1. A dedicated thread handles all I/O events and is responsible for distribution.
  2. Event-driven mechanism: (constantly polling to determine whether the event arrives) triggers when the event arrives, instead of monitoring the event synchronously.
  3. Thread communication: (established because of the request) communicates between threads through wait, notify, etc. (how). Ensure that each context switch is meaningful and reduce unnecessary (time slice) thread switching.

insert image description here

Note: The processing flow of each thread is roughly reading data, decoding, computing processing, encoding and sending responses.

 

2. Netty and NIO

Netty is an asynchronous, event-driven framework for high-performance and high-reliability network applications. The following are its main advantages.

(1) The frame design is elegant, and the underlying model can be switched at will to adapt to different network protocol requirements.
(2) Provides many standard protocols, security, codec support.
(3) Solved many problems that NIO is not easy to use.
(4) The community is more active and used in many open source frameworks, such as Dubbo, RocketMQ, Spark, etc.

The functions and features supported by Netty are shown in the figure below.
insert image description here

  1. The underlying core includes: Zero-Copy-Capable Buffer, a very easy-to-use zero-copy Buffer (this content is very interesting, I will talk about it later), a unified API, and a standard and extensible event model.
  2. The support for transmission includes: pipeline communication; HTTP tunnel; TCP and UDP.
  3. Protocol support includes: protocols based on original text and binary; decompression; large file transfer; streaming media transfer; ProtoBuf codec; security authentication; HTTP and WebSocket.

 
Reasons for Netty to use NIO instead of AIO

  1. Netty does not value the use on Windows. On the Linux system, the underlying implementation of AIO still uses epoll, which does not implement AIO well, so there is no obvious advantage in performance, and it is encapsulated by JDK, so it is not easy to optimize deeply.
  2. The overall architecture of Netty adopts the Reactor model, while AIO adopts the Proactor model. If they are mixed together, it will be very confusing. If AIO is also transformed into a Reactor model, it seems that Epoll is turned around and back.
  3. Another disadvantage of AIO is that receiving data needs to be allocated in advance (you need to estimate the cache), while NIO allocates cache when it needs to receive, so for the case of a very large number of connections but small traffic, AIO wastes a lot of memory.
  4. AIO on Linux is not mature enough, and the speed of processing callback results cannot keep up with the processing demand. For example, there are too few delivery staff, too many customers, and supply exceeds demand, resulting in a bottleneck in processing speed.

 
 

Reference:
"Netty4 Core Principles and Handwritten RPC Framework"

Supongo que te gusta

Origin blog.csdn.net/hiliang521/article/details/131218282
Recomendado
Clasificación