Official Introduction to Netty#
Netty is an asynchronous event-driven network application framework for rapid development of maintainable high performance protocol servers & clients.
Netty is an asynchronous event-driven network application framework for rapid development of maintainable high performance protocol servers and clients.
Netty is a NIO client server framework which enables quick and easy development of network applications such as protocol servers and clients. It greatly simplifies and streamlines network programming such as TCP and UDP socket server.
'Quick and easy' doesn't mean that a resulting application will suffer from a maintainability or a performance issue. Netty has been designed carefully with the experiences earned from the implementation of a lot of protocols such as FTP, SMTP, HTTP, and various binary and text-based legacy protocols. As a result, Netty has succeeded to find a way to achieve ease of development, performance, stability, and flexibility without a compromise.
Netty is a NIO client server framework that enables quick and easy development of network applications such as protocol servers and clients. It greatly simplifies and streamlines network programming such as TCP and UDP socket servers.
'Quick and easy' does not mean that the developed application will have maintainability or performance issues. Netty's design is carefully crafted, drawing from experiences in implementing many protocols such as FTP, SMTP, HTTP, and various binary and text-based legacy protocols. Therefore, Netty has successfully found a way to achieve ease of development, performance, stability, and flexibility without any compromise.
Why Learn Netty?#
- Netty has become the standard for network communication programming in the industry, widely used in the communication field and many other middleware technologies.
- Extremely widespread applications
- Gaming industry
- Communication foundations for many frameworks, solving inter-process communication.
Spring WebFlux, Storm, RocketMQ, Dubbo, etc., are distributed systems, the core of communication.
What Problems Exist with Traditional IO?#
- Multi-threaded network programming
1.1 Thread creation overhead
Traditional IO communication uses multiple threads, with each client connection in the server program starting a new thread. Both starting and stopping a thread incur overhead.
2.2 High CPU usage
Assuming 1000 threads exist simultaneously, after each thread's time slice is used up, context switching (which can be understood as the operating system saving the state of the thread when its time slice is exhausted so that it can continue from that point when it gets the time slice again) incurs overhead.
2.3 High memory usage, cannot create threads indefinitely
Hardware resources are precious; a thread in Java occupies about 1MB, and creating a large number of threads can lead to insufficient memory.
- Thread pool network programming
The pooling concept solves the problem of unlimited thread creation and allows the code to leverage multi-core CPU advantages. However, a new problem arises: if 100 threads come in, but only 10 in the thread pool can handle requests, the excess 90 threads can only wait in the queue for processing, leading to a blocking issue. The above example is still an ideal situation; if the 10 incoming threads are blocked for some reason, then all 10 threads in the thread pool must wait to finish processing these 10 requests before they can handle the remaining 90 requests, making the blocking issue very severe and wasting limited resources.
Summary#
Why use Netty? Because Netty uses NIO at its core, solving the aforementioned communication issues.
The End!!!