# Netty — Async Event-Driven Network Application Framework > An asynchronous event-driven network application framework for building high-performance protocol servers and clients on the JVM. ## Install Save in your project root: # Netty — Async Event-Driven Network Application Framework ## Quick Use ```java EventLoopGroup group = new NioEventLoopGroup(); ServerBootstrap b = new ServerBootstrap(); b.group(group) .channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer() { protected void initChannel(SocketChannel ch) { ch.pipeline().addLast(new StringDecoder(), new MyHandler()); } }); b.bind(8080).sync(); ``` ## Introduction Netty is an asynchronous event-driven network application framework that simplifies development of high-performance protocol servers and clients. It powers the networking layers of numerous large-scale systems including gRPC, Elasticsearch, Cassandra, and Apache Spark. ## What Netty Does - Provides a unified asynchronous I/O API for TCP, UDP, HTTP, HTTP/2, WebSocket, and custom protocols - Manages event loops and thread pools to handle thousands of concurrent connections with minimal threads - Offers a composable ChannelPipeline for modular codec and handler chains - Includes built-in codecs for SSL/TLS, HTTP, Protobuf, DNS, MQTT, and more - Handles zero-copy file transfer and efficient ByteBuf memory management ## Architecture Overview Netty uses a Reactor pattern with EventLoopGroups that assign channels to event loops. Each event loop runs on a single thread and processes all I/O events for its assigned channels. Inbound and outbound data flows through a ChannelPipeline of handlers, allowing codecs and business logic to be composed as reusable modules. ByteBuf provides reference-counted buffer management with pooled allocators for reduced GC pressure. ## Self-Hosting & Configuration - Add via Maven or Gradle: io.netty:netty-all:4.1.x or pick individual modules - Use NioEventLoopGroup for cross-platform or EpollEventLoopGroup for Linux-optimized I/O - Configure channel options like SO_BACKLOG, SO_KEEPALIVE, and TCP_NODELAY on the bootstrap - Tune the number of event loop threads based on available CPU cores - Enable native transport (epoll/kqueue) for lower latency and reduced garbage collection ## Key Features - Handles millions of concurrent connections with a small thread pool via non-blocking I/O - Composable pipeline architecture for clean separation of protocol handling - Pooled ByteBuf allocators minimize memory allocation overhead - Native transport support on Linux (epoll) and macOS (kqueue) for kernel-level optimization - Battle-tested in production at companies running some of the largest distributed systems ## Comparison with Similar Tools - **Java NIO** — raw API that Netty abstracts; using NIO directly requires significant boilerplate - **Apache MINA** — similar async I/O framework but less actively maintained and smaller community - **Vert.x** — higher-level reactive toolkit that uses Netty internally as its network layer - **Project Reactor Netty** — Spring's reactive HTTP layer built on Netty; adds reactive streams semantics ## FAQ **Q: When should I use Netty directly vs a higher-level framework?** A: Use Netty when you need custom protocol handling or maximum control. Use frameworks like gRPC, Vert.x, or Spring WebFlux when standard protocols suffice. **Q: Does Netty support HTTP/2?** A: Yes. Netty has a full HTTP/2 codec with support for multiplexing, header compression, and flow control. **Q: How does Netty handle memory?** A: Netty uses reference-counted ByteBuf with pooled allocators. Buffers must be explicitly released or handled by pipeline handlers. **Q: Is Netty suitable for client-side use?** A: Yes. Netty provides Bootstrap for building both servers and clients with the same programming model. ## Sources - https://github.com/netty/netty - https://netty.io/ --- Source: https://tokrepo.com/en/workflows/1b352d40-433f-11f1-9bc6-00163e2b0d79 Author: AI Open Source