Netty

Basic Introduction

Netty is a high-performance, asynchronous, event-driven network application framework provided by JBOSS. It is built on Java NIO technology, simplifying and abstracting the complexity of NIO, allowing developers to focus on implementing business logic without dealing with low-level network communication details.

Core Features

  • Asynchronous non-blocking: Uses Reactor thread model to support high-concurrency connections
  • Event-driven: Based on Selector mechanism for efficient event processing
  • Zero-copy: Provides optimized data transfer mechanism to reduce memory copying
  • High scalability: Modular design, supports flexible extensions
  • Multi-protocol support: Built-in HTTP, WebSocket, TCP/UDP and other protocol support

Application Scenarios

  1. Internet field:
    • High-performance RPC frameworks (such as Dubbo)
    • Distributed message middleware
    • Real-time communication systems
  2. Big data field:
    • Distributed computing framework communication layer
    • Data collection and transmission systems
  3. Gaming industry:
    • Multiplayer online game servers
    • Real-time battle systems
  4. Communication industry:
    • IoT gateways
    • Carrier-grade application servers

Notable Application Cases

  • Elasticsearch: Uses Netty as its distributed node-to-node communication component
  • Dubbo: Based on Netty for efficient RPC communication
  • Spark: Uses Netty in some network modules
  • RocketMQ: Message queue’s network communication layer is built on Netty

Why Use Netty

Disadvantages of NIO

Complex usage methods

NIO’s class library and API design are complex with a high learning curve. Developers need to master several core components:

  • Selector: Used to listen for events on multiple channels
  • ServerSocketChannel: Server-side channel for listening for new connections
  • SocketChannel: Network socket channel
  • ByteBuffer: Buffer for data reading and writing

Difficult development

Reliability assurance is difficult; developers need to:

  • Handle various network exceptions and boundary conditions
  • Implement complex thread synchronization mechanisms
  • Write a lot of boilerplate code to maintain connection state

Inherent defects

NIO has some hard-to-avoid bugs:

  • The famous Epoll BUG causes Selector to continuously poll even when there are no events
  • Especially prone to occurring in Linux environments
  • Can cause CPU usage to reach 100%, seriously affecting system performance

Advantages of Netty

Unified API interface

Provides unified API to support multiple transport protocols:

  • TCP/UDP
  • HTTP/HTTPS
  • WebSocket
  • Custom protocols

Flexible thread model

Provides highly customizable thread model:

  • Single-thread model: Suitable for low-concurrency scenarios
  • Multi-thread model: Uses thread pool to handle high concurrency
  • Master-slave thread model: Separates connection acceptance and processing

Excellent performance

Has significant advantages in performance:

  • Throughput improved 30%-50% compared to traditional solutions
  • Latency reduced 40%-60%
  • Resource consumption reduced 20%-30%

Efficient memory management

Uses advanced memory management technology:

  • Uses off-heap memory to reduce GC pressure
  • Implements zero-copy technology
  • Provides memory pooling mechanism

Thread Model

Netty Model

Netty uses a highly optimized multi-thread model. Its core thread pool design is divided into two key parts:

  1. BossGroup thread pool group:

    • Specially responsible for handling client TCP connection requests
    • By default, the thread pool size is set to 1 (suitable for most scenarios)
    • Each thread is an NioEventLoop instance
  2. WorkerGroup thread pool group:

    • Specially responsible for handling I/O read/write operations on established connections
    • Thread count is usually set to CPU core count × 2 (best practice configuration)
    • Each Worker thread is also an NioEventLoop instance

Core working mechanism of NioEventLoop:

  • Each NioEventLoop thread maintains a Selector instance
  • Uses event-driven model to continuously listen for registered SocketChannel events
  • Internally uses serialized design to ensure thread safety

Typical configuration example:

EventLoopGroup bossGroup = new NioEventLoopGroup(1);  // 1 Boss thread
EventLoopGroup workerGroup = new NioEventLoopGroup(); // Default CPU cores × 2
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
 .channel(NioServerSocketChannel.class)
 .childHandler(new ChannelInitializer<SocketChannel>() {
     @Override
     public void initChannel(SocketChannel ch) {
         // Add business processing Handler
     }
 });

Core Components

ChannelHandler and its Implementation Classes

The ChannelHandler interface defines many event handling methods. We can implement specific business logic by overriding these methods.

  • public void channelActive(ChannelHandlerContext ctx), channel ready event
  • public void channelRead(ChannelHandlerContext ctx, Object msg), channel read data event
  • public void channelReadComplete(ChannelHandlerContext ctx), data read complete event
  • public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause), channel exception event

ChannelPipeline

ChannelPipeline is a collection of Handlers that is responsible for processing and intercepting inbound or outbound events and operations, equivalent to a chain running through Netty.

ChannelHandlerContext

This is the event processor context object, the actual processing node in the Pipeline chain. Each processing node ChannelHandlerContext contains a specific event processor ChannelHandler.

ChannelFuture

Represents the result of asynchronous I/O operations in Channel. In Netty, all I/O operations are asynchronous.

EventLoopGroup and its Implementation Class NioEventLoopGroup

EventLoopGroup is an abstraction of a group of EventLoops. To better utilize multi-core CPU resources, Netty generally has multiple EventLoops working simultaneously. Each EventLoop maintains a Selector instance.

ServerBootstrap and Bootstrap

ServerBootstrap is the server startup assistant in Netty, through which various server configurations can be completed. Bootstrap is the client startup assistant in Netty.