Some understanding of Netty's

NettyIt is a high-performance, asynchronous event-driven NIOframework that provides TCP, UDPsupport and file transfer. As the most popular NIOframework Nettyin the field of Internet, big data distributed computing, game industry, communications industry, such as access to a wide range of applications, some of the industry's leading open source components is also based on Nettythe NIOframework to build.

NettyTake advantage of Javathe ability of advanced network, hiding the complexity behind it to provide an easy to use APIto build a client / server, which has a high concurrency, transmission faster, better packaging and so on.

High concurrencyNetty is based on a NIO( Nonblocking I/Onon-blocking IO) network communications framework for the development, in contrast to the BIO( Blocking I/Oblocking IO), its concurrent performance has been greatly improved.

Fast transmissionNetty of fast transmission is actually dependent on the NIOone characteristic - zero copy .

Good packaging Netty NIO encapsulated many of the details of the operation, to provide easy to use API, as well as characteristics of heartbeat reconnection mechanism, stick package unpacking programs, enable developers to build a robust high concurrent applications quickly and efficiently.

Why Netty?

JDKNative issues programNIO

JDKNative also have a web application API, but a series of problems, notably the following:

  • NIOLibraries and APIcomplicated, cumbersome to use. You need to master Selector, ServerSocketChannel, SocketChannel, ByteBufferand so on.
  • Other need to have additional skills to pave the way. For example familiar with Javamulti-threaded programming, because the NIOprogram involves the Reactormode, you have to multithreading and network programming is very familiar with, in order to write a high-quality NIOprogram.
  • Reliability capacity filled, the development workload and difficulty are very large. For example a client face reconnect disconnected, network glitches, half a pack reader, a cache failure, abnormal processing network congestion and so on stream. NIOProgramming is characterized by functional development is relatively easy, but the reliability of the capacity filled workload and difficulty are very large.
  • JDK NIOA Bug. For example infamous Epoll Bug, it will lead to Selectorempty polling, eventually leading CPU 100%. Officials say the JDK 1.6version update 18fixes the problem, but until JDK 1.7version of the problem still exists, but the Bugprobability of reducing some of it, it has not been fundamentally resolved occur.

Netty specialty

NettyTo JDKown NIOthe APIencapsulated solve the above problems, the main features are:

  • Elegantly designed for the harmonization of transport types of APIblocking and non-blocking Socket; based on a flexible and extensible event model, clearly separating concerns; highly customizable threading model - a single thread, one or more thread pool; true connectionless datagram socket support (from 3.1starting).
  • Easy to use, a detailed record Javadoc, user guide and examples; no other dependencies, JDK 5( ) Netty 3.xor 6( ) Netty 4.xis sufficient.
  • Performance, higher throughput, lower delay; reduce resource consumption; minimize unnecessary memory copying.
    The safe, complete SSL/TLSand StartTLSsupport.
  • Community activists, constantly updated, community activists, short version iterations, found Bugcan be repaired in time, at the same time, more new features will be added.

Netty internal execution flow


  • Creating ServerBootStrapinstances
  • Set and bind Reactorthe thread pool: EventLoopGroup, EventLoopis registered to handle all of this thread SelectoraboveChannel
  • Set and bind the service sideChannel
  • Create a process network events ChannelPipelineand handler, in the form of network time in which the flow of the stream, handlerto complete the majority of the custom function: such as codec SSlsafety certification
  • Binding and start listening port
  • When the rotational training to ready channelafter by Reactora thread: NioEventLoopthe implementation piplineof the method, the final scheduling and executionchannelHandler

Client:Netty client creates a timing diagram .pngimage.png

Netty architecture design

FIG main features are as follows:image.png

Netty Features are as follows:

  • Transport service, support BIOand NIO.
  • Container integration, support OSGI, JBossMC, Spring, Guicecontainer.
  • Protocol HTTPsupport , Protobufbinary, text, WebSocketand a series of common protocols are supported. Also supports custom protocol implemented by executing coding and decoding logic.
  • CoreCore, scalable event model, universal communication API, support for zero-copy ByteBufbuffer object.

Module assembly


BootstrapThe guide means is an Nettyapplication usually consists of a Bootstrapstart, the main role is to configure the entire Nettyprogram, the various components in series, Nettythe Bootstrapclasses are Class boot client program, ServerBootstrapa boot server class.


As previously described, in Nettyall the IOoperations are asynchronous, not immediately know whether the message is processed correctly.

But after a while and so it can be completed or executed directly register a listener, specific implementation is through Futureand ChannelFuturesthey can register a listener, when an operation success or failure of the monitoring will be triggered automatically registered to listen for events.


NettyComponents of the network communication can be used to perform network I/Ooperations. ChannelTo provide users with:

  • The state of the current channel network connection (for example, whether to open? Are you connected?)
  • Network configuration parameters for the connection (e.g., receive buffer size)
  • It provides asynchronous network I/Ooperations (such as establishing a connection, read and write, to bind to port), an asynchronous call means that any I/Ocall will return immediately, and does not guarantee the end of the call to the requested I/Ooperation has been completed. Immediately return a call ChannelFutureinstance, by registering listeners to ChannelFutureon, you can I/Ocallback notification caller successful operation, cancel or fail.
  • Supporting the association I/Ooperation with a corresponding processing program.

Different protocols, different type of blocking has different connection Channeltypes corresponding thereto. Here are some common Channeltypes:

  • NioSocketChannelAsynchronous client TCP Socketconnections.
  • NioServerSocketChannel, Asynchronous server TCP Socketconnection.
  • NioDatagramChannel, Asynchronous UDPconnection.
  • NioSctpChannelAsynchronous client Sctpconnections.
  • NioSctpServerChannel, Asynchronous Sctpserver-side connection, which covers the channels UDPand TCPthe network IOand file IO.


NettyBased on Selectorthe object to achieve I/Omultiplexing through Selectormultiple connections you can listen to a thread Channelevent.

As to a Selectorregistration Channel, the Selectorinternal mechanism can automatically continue to query ( Select) of these registered Channelwhether there has been ready I/Oevents (such as read, write, network connection is completed, etc.), so that the program can simply use a efficient management of multiple threads Channel.


NioEventLoopMaintained a thread and task queues, submit support asynchronous tasks will be called when the thread starts NioEventLoopof runmethods to perform I/Othe task and non- I/Otask:

  • I/OTasks, namely selectionKeyin readythe event, such as accept, connect, read, writeetc., by a processSelectedKeystrigger method.
  • Non- IOtask, added to taskQueuethe tasks, such as register0, bind0and other tasks, the runAllTaskstrigger method.
    Task execution time ratio of the two kinds of variable ioRatiocontrol is default 50, it indicates a non-allowed IOtime and the task execution IOis equal to the task execution time.


NioEventLoopGroupThe main management eventLooplife cycle, can be understood as a thread pool, to maintain a set of internal threads, each ( NioEventLoop) is responsible for handling multiple Channelevents, and a Channelcorresponds to only one thread.


ChannelHandlerIt is an interface to process I/Oan event or interception I/Ooperation, and forwards it to its ChannelPipeline(the processing chain) next handler.

ChannelHandler Itself does not provide a lot of ways, because there are many ways this interface need to implement, during the easy to use, it can be inherited subclass:

  • ChannelInboundHandlerFor processing inbound I/Oevents.
  • ChannelOutboundHandlerFor processing outbound I/Ooperations.
    Or using the following adapter class:
  • ChannelInboundHandlerAdapterFor processing inbound I/Oevents.
  • ChannelOutboundHandlerAdapterFor processing outbound I/Ooperations.
  • ChannelDuplexHandler For inbound and outbound events.
  • ChannelHandlerContextSave Channelall relevant contextual information, as well as a related ChannelHandlerobject.


Preservation ChannelHandlerof List, for processing or intercepting Channelinbound and outbound event operation.

ChannelPipelineImplements an advanced form of interception filter mode, the user has full control event handling, as well as Channelin each of ChannelHandlerhow they interact with each other.

Netty high-performance design

NettyAn asynchronous event-driven network, comes from its high performance of the I/Omodel and the threading model, the former determines how receive data, which determines how data.

I / O model

What kind of channel to send data to each other BIO, NIOor AIO, I/Omodel largely determine the performance framework.

Blocking I / O

Traditional obstructive I/O( BIO) can be represented by the following figure:image.png

The following characteristics and disadvantages:

  • Each request requires a separate thread to complete data Read, business processes, data Writeintegrity operational problems.
  • When a large number of concurrent need to create a large number of threads to handle connections, system resource consumption.
  • After the connection is established, if the current thread is temporarily no data to read, the thread blocked in Readoperation, resulting in waste of resources thread.

I / O multiplexing modelimage.png in I/Omultiplexing model will be used Select, this function will also make the process of blocking, and blocking but I/Othe difference is that this function can block multiple simultaneous I/Ooperations.

But also simultaneously to a plurality of read operations, the write operation of a plurality of I/Ofunctions detected, until the data is read or written, it really calls the I/Ooperating function.

NettyNon-blocking I/OAchieving key is based on the reuse model , used here represents an object:I/OSelectorimage.png

NettyThe IOthread NioEventLoopthe polymerization of the multiplexer Selectorcan handle hundreds of concurrent client connections.

When a thread from a client Socketwhen read and write data channel, if no data is available, the thread can perform other tasks.

The threads are usually nonblocking IOidle time for performing other channel IOoperation, the individual threads can manage a plurality of input and output channels.

Since the read and write operations are non-blocking, which can fully enhance IOthe efficiency of thread, avoid frequent I/Oclogging caused by thread suspension.

A I/Othread can handle concurrent Nclient connections and read and write operations, which solves the traditional synchronous blocking fundamentally I/Oa connection a thread model, architecture, performance, elastic scalability and reliability have been greatly improved.

Netty Threading model

NettyMainly based on master-slave multithreaded model (see below) do some modification, wherein the master from a plurality of multi-threading models :ReactorsReactorReactor

MainReactorResponsible for the client's connection request, and the request is forwarded to SubReactor. SubReactorIs responsible for the corresponding channel IOread and write requests. Non- IOrequest (specifically logical processing) will be written directly to task queue waiting worker threadsfor processing. The entire Doug LeeGreat God of Reactorintroduction: Scalable IO in Javainside on a master-slave Reactorview of a multi-threaded model: image.pngspecial note is: Although the Nettythreading model based on master-slave Reactormulti-threaded, borrowed MainReactorand SubReactorstructure. But the actual implementation SubReactorand Workerthreads with a thread pool.

Netty Zero copy

When data is sent, the traditional implementation is:;

This embodiment requires four data and four copies of context switching:

  • Data is read from the disk to the kernelread buffer
  • Data is copied from the kernel buffer to the user buffer
  • Copy the data from the user to the kernel buffersocket buffer
  • The kernel data socket buffercopied to the network interface (hardware) buffer

The concept of zero-copy

Significantly above second and third steps is not necessary, by javathe FileChannel.transferTomethod to avoid the above two extra copies (of course, supported by the underlying operating system)

  • Call transferTo, data from the file DMAcopy engine to the kernelread buffer
  • Then DMAfrom the kernel read buffercopies the data to the network interfacebuffer

The above two operations do not need CPUto participate, so I reached zero copy.

NettyThe zero copy is mainly reflected in three aspects:

  • bytebuffer

NettyIt is mainly used to send and receive messages bytebuffer, bytebufferusing the external memory ( DirectMemory) directly Socketread and write.

Reason: Using traditional heap memory for Socketreading and writing, JVMwill heap memory buffercopy directly into memory and then written into socket, a plurality of buffer memory copy. DirectMemoryIt can be sent directly to the card via the DMA interface.

  • Composite Buffers

Traditional ByteBuffer, if you need two ByteBuffercombinations of data together, we need to first create a size=size1+size2new array size, then copy the data into two arrays of the new array.

However, the use of Nettya combination provided ByteBuf, you can avoid such an operation, because CompositeByteBufno real multiple Buffercombined, but holds a reference to them, thus avoiding the copy of the data, to achieve a zero copy.

  • For FileChannel.transferTouse

NettyUsed in FileChannel的transferTothe method, the method relies on the operating system to realize zero copy.

This article from the blog article multiple platforms OpenWrite release!

For more information, please click on my blog Mu Chen

Guess you like