Netty
Asynchronous network application framework — the engine under a great deal of JVM infrastructure.
What it is
Netty is an asynchronous, event-driven network application framework for rapid development of maintainable high-performance protocol servers and clients.
Netty provides abstractions for building servers and clients, handling connections, pipelines, codecs, and event-driven processing. It supports TCP, UDP, HTTP, WebSockets, and custom protocols.
- Licence
- Apache 2.0
- Powers
- gRPC-Java, Elasticsearch, Cassandra and much else
When to use it
The question documentation cannot answer for you — because it cannot recommend something else.
Reach for it when
- Implementing a custom network protocol
- Building a high-concurrency server where you need control over the event loop and buffers
Look elsewhere when
- You are writing an ordinary HTTP service — use a framework built on Netty rather than Netty itself
- Virtual threads have made simple blocking code viable for your load profile
Installation
Add io.netty:netty-all dependency in pom.xmlGetting started
The smallest useful thing you can do with it, and what each part means.
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
public class SimpleServer {
public static void main(String[] args) throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) {
ch.pipeline().addLast(new SimpleServerHandler());
}
});
ChannelFuture f = b.bind(8080).sync();
f.channel().closeFuture().sync();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
public class SimpleClient {
public static void main(String[] args) throws Exception {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) {
ch.pipeline().addLast(new SimpleClientHandler());
}
});
ChannelFuture f = b.connect("localhost", 8080).sync();
f.channel().closeFuture().sync();
} finally {
group.shutdownGracefully();
}
}
}Advanced usage
Where the library earns its place over a simpler alternative.
ch.pipeline().addLast(new StringDecoder(), new StringEncoder(), new CustomHandler());// Add WebSocketServerProtocolHandler to pipeline for WebSocket upgrade and frame handlingchannel.read();
channel.writeAndFlush(msg);SslContext sslCtx = SslContextBuilder.forServer(certFile, keyFile).build();
pipeline.addLast(sslCtx.newHandler(ch.alloc()));Errors and fixes
The failures you are most likely to hit, and what actually resolves them.
- ChannelException
- Occurs when there is a problem binding or connecting a channel. Verify port availability and network configuration.
- Decoder/Encoder errors
- Ensure message formats match decoders/encoders in the pipeline.
- OutOfMemoryError
- Tune Netty buffer allocation and release resources properly to avoid memory leaks.
Best practices
- Use separate EventLoopGroups for boss and worker threads to handle connections and I/O efficiently.
- Leverage the pipeline for modular and reusable handlers.
- Avoid blocking operations in event handlers; delegate to separate threads if necessary.
- Use proper resource cleanup to prevent memory leaks and dangling connections.
- Monitor and tune thread pools and buffers for high-performance network applications.
Alternatives
Comparable options, and the reason you would pick one over the other.
Background
Why it exists, and what it was reacting to.
Netty was created to simplify the development of scalable and high-performance network applications in Java. It abstracts low-level I/O operations, providing an easy-to-use API for TCP/UDP protocols and enabling event-driven architectures.
