This is the fourth in the NIO series, so stay tuned:

  1. NIO series — TCP exploration
  2. NIO series — IO model
  3. [NIO series] — Reactor model

If you’ve read the previous three articles, we’ve broken down the underlying principles and usage of NIO from the lowest level, helping us understand what NIO is, what problems it solves, and what it lacks.

In principle, the emergence of NIO has improved and accelerated the processing of network IO, but it can only help us solve the problem of reading and writing at the IO level. At the software level, we need a better programming architecture model to solve the problem of scalability and high concurrency. Netty is designed to solve these problems. In this article, we will introduce Netty in detail.


What is Netty

1. What is it

Let’s use the official words:

Netty is a high-performance, asynchronous event-driven network application framework. Based on Netty, high-performance and highly available network server and client applications can be quickly developed and deployed.

Simply put, it is a Web application framework that helps you solve three problems in Web-oriented development:

  • Network – oriented IO reads and writes, such as TCP socket connections

  • Codec application layer protocols, such as HTTP

  • High concurrency architecture

So what are his strengths?

  • Fast – Strong performance, high concurrency, low latency

  • Easy – High scalability, Easy API use, low barrier to development

  • Non-blocking – Non-blocking, NIO support


2. What’s the difference

For those of us who are used to the Web container, the first question is what netty can do and why use it. Since Netty is a network application framework, why use Netty when Tomcat can solve these problems?

The differences between Netty and Tomcat are as follows:

  • Tomcat is an “HTTP Server”, more accurately, it is a “Servlet/JSP” application container, it mainly solves the HTTP protocol level of transmission and access.

  • HTTP is an application-layer protocol. In addition to HTTP, other application-layer protocols include POP and IMAP for the post office, FTP, LDAP, SSH, and TLS/SSL.

  • Netty supports not only HTTP, but also FTP, LDAP, SSH, TLS/SSL and other application-layer protocols. It also supports customized application-layer protocols as long as you need them.

  • Although Netty is classified as OSI layer 7 application, it exists to help you support Layer 3 transport issues, such as TCP, UDP, and SCTP network protocol development. So it can be called a communication component.

  • In principle, Tomcat network communication components can also use Netty, but servlet3.0 was completely synchronous blocking model, Tomcat has to follow the servlet specification, so it can not maximize the features of NIO, while Netty does not follow the servlet specification, can maximize the features of NIO. Better performance.


Why use Netty

Netty’s three main features (fast, Easy, and no-blocking) can be broken down into the following features:

  • NIO support, asynchronous programming

  • Strong performance, high concurrency, low latency, lower resource usage and memory usage for faster performance support, strongest in the NIO field

  • Mature, stable, all TCP problems you can think of have been solved, especially NIO bugs, and perfectly solved.

  • Not only support HTTP, support a variety of application protocol and network protocol, such as TCP/UDP/UDT/SCTP/FTP, SMTP, etc.

  • Powerful, prefabricated a variety of codec processors, support a variety of mainstream application protocols

  • The API is simple to use and the development threshold is low


1.Fast

Why is it fast? Please refer to the Reactor IO model introduced in the previous article ([NIO series] — Reactor Model).


Netty implements this Reactor model based on NIO,Boss thread deals with connection establishment, SubReactor deals with I/O read and write and task processing. This threading model supports a large number of concurrent connections while taking full advantage of CPU performance.


How is the specific effect? Let’s take a look at the test data:

The above is the pure text response test of webFrame obtained from Techempower. It can be seen that Netty ranks second in response speed. Although the ranking of Netty has declined in recent years due to the continuous challenges of high-performance Web frameworks, it still retains the top position.


The above is the official test case of Dubbo, given the protocol serialized TPS comparison, it can be seen that Dubbo with Netty is the best in TPS.


2. Less memory usage

Netty has to deal with a lot of network packet processing, so there is a lot of network object creation and destruction, which puts a lot of pressure on the JVM.

Netty uses two main solutions to relieve the pressure on the JVM:

  • ByteBufAllocator Object pool. Pool ByteBuf instances to improve performance and minimize memory fragmentation, which returns a new instance each time it is called.

  • Zero copy. Support for the use of DirectBuffer, which allocates memory through local calls to the JVM, avoids copying the contents of the buffer to (or from) the intermediate buffer before (or after) each call to the local I/O operation.

Here is a sample of twitter’s Netty4 usage object pool application:


3.Easy, rapid development

We know that the programming involved in the network will be more complex, in addition to a pile of TPC parameters need to be set, there are a pile of IO read and write problems to deal with, at the same time in order to improve the concurrency, but also the use of multi-threading, which brings the problem of thread safety, often brings great challenges and complexity to developers.

Netty is aimed at these difficulties to do a unified simple encapsulation, in addition to making the API more simple and easy to use, such as: TCP server configuration start, data packet buffByte read and write. In addition, the serial processing of network events based on event mode not only ensures high efficiency but also reduces the complexity of programming.

Why serial execution improves Netty performance:

  • Serial lockless design, serial operation in IO thread, avoid multithreading competition caused by performance decline.

  • By adjusting the thread parameters of the NIO thread pool, multiple serialized threads can be started simultaneously to run in parallel. This partially lock-free serialized thread design is superior to the one-queue-multiple worker thread model

  • Reduced context switching and synchronization of state data


The following is the chain of responsibilities ChannelPipeline uses to handle network events, managing and executing channelhandlers. Network events flow in ChannelPipeline in the form of event flow. Support plug mode, scalability is also very strong.


4. Reliability and stability

In the aspect of network, applications often have to face complex network environment, such as poor network environment, often appear some network intermittent, single pass and network congestion and so on a series of problems. As an underlying communication component, there is also the issue of robustness, since bugs can cause a large number of dependent services to be interrupted.

Netty continuously adds some reliability features in the release iteration to meet users’ increasing reliability and robustness requirements. For example, NIO select idle bug, such as TCP disconnection reconnection, keep-alive detection and other problems, Netty has helped you solve.

In particular, Netty has been adopted as a communication component by various major manufacturers after the launch of 4.0, which is the verification of its reliability and the recognition of its stability by the community. Here are some companies that use Netty, see Netty.Adopters for more information


Three, how to use

With all this information about Netty, there won’t be a bunch of code to deal with when it comes to coding.

Let’s use a simple TCPServer as an example.


Requirements:

Client: receives user input and sends it to the Server. The Client also receives and displays the content returned by the Server

Server: monitors port 8088, receives and displays the content sent by the Client, and responds to the Client


Code:

BossGroup = new NioEventLoopGroup(); bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); ServerBootstrap b = new ServerBootstrap(); // set thread pool b.group(bossGroup, WorkerGroup) / / step3 set channel. The channel (NioServerSocketChannel. Class) / / setp4 set channel hanlder. ChildHandler (new ChildChannelHandler()) // Sets the size of the send and receive buffers. Option (channeloption.so_sndbuf,256). Option (channeloption.so_rcvbuf,256) // Small packet automatic connection. Option (channeloption. TCP_NODELAY,true)
              .childOption(ChannelOption.SO_KEEPALIVE, true) .handler(new LoggingHandler(LogLevel.INFO)); // step 7 Bind to listen to the port. ChannelFuture f = b.bind(PORT).sync(); logger.info("tcp server start success... ");
          f.channel().closeFuture().sync();
      } finally {
      }Copy the code


ChildChannelHander extension Pipeline execution strategy:

protected void initChannel(SocketChannel channel) throws Exception {
      ChannelPipeline pipeline = channel.pipeline();
      pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
      pipeline.addLast("frameEncoder", new LengthFieldPrepender(4));
      pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));
      pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));
      pipeline.addLast(new TcpHelloServerHandler());
​
  }Copy the code



With the above code, we need to go through about 7 steps to create a TCP Server. It seems that 7 steps is a lot, but actually considering the amount of code, it is very small.

From the above execution flow chart, we can see:

  • ServerBootstrap is a service bootstrap helper class that uses a no-parameter constructor (Builder mode) to provide a system method for setting boot-related parameters

  • EventLoopGroup: Netty’s Reactor thread pool is responsible for scheduling a set of Eventloops. Typically bossEventLoop is used to maintain connections, and subEventLoop is used to maintain I/O operations for all registered channels processing user-defined tasks and scheduled tasks

  • Channel: encapsulates the Java native NIO class library. For ordinary users, they do not need to care about the details of the underlying implementation and working principle. They only need to specify which Channel is used to connect IO devices (sockets) and support asynchronous I/O operations (such as read, write, connect and bind) with IO devices.

  • ChildChannelHandler, which configures the ChannelPipeline execution policy. The key interface for the expansion, users can complete most of the function customization, such as message codec, heartbeat, security authentication, TSL/SSL authentication, flow control, etc.


Four, the last

The advantages and basic introduction of Netty are described from three aspects: what is Netty, how to use Netty and why to use Netty. Hopefully, we can have a basic understanding of Netty. Later we will be on Netty high-performance architecture, as well as multi-threaded efficient programming, combined with the source code, there will be more analysis. We will focus not only on how the source code is implemented, but also on the architecture as a whole, analyzing design concepts and exploring ways to write robust code. If you are interested, please continue to pay attention.

For more architecture knowledge, please follow my official account, Cool_wier.