好热闹

好热闹

bbbbb

1. Why use Netty as network communication

Introduction to Netty#

https://netty.io/

Netty is an asynchronous event-driven network application framework for rapid development of maintainable high-performance protocol servers & clients.

Netty is a NIO client-server framework used for fast 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' doesn't mean that a resulting application will suffer from maintainability or performance issues. Netty has been carefully designed with the experiences gained from implementing a lot of protocols such as FTP, SMTP, HTTP, and various binary and text-based legacy protocols. As a result, Netty has succeeded in achieving ease of development, performance, stability, and flexibility without compromise.

Why Learn Netty?#

  1. Netty has become the industry standard for network communication programming and is widely used in the communication field and as the underlying technology for many other middleware technologies.
  2. It has a wide range of applications, including:
    1. Gaming industry
    2. Communication layer for many frameworks, solving inter-process communication.
      Spring WebFlux, Storm, RocketMQ, Dubbo, etc., are the core of distributed systems communication.

What are the problems with traditional IO?#

  1. Multi-threaded network programming

1.1 Thread creation overhead

Traditional IO communication uses multiple threads, and each client connection in the server program will open a thread. Opening and closing threads both have overhead.

2.2 High CPU usage

Assuming that 1000 threads exist simultaneously, there will be overhead for context switching (context switching refers to the operating system saving the place where the thread was executing when its time slice is used up, so that it can continue executing from that place the next time it gets a time slice).

2.3 High memory usage, it is not possible to create threads indefinitely

Hardware resources are precious, and a thread in Java occupies approximately 1MB. Creating a large number of threads can lead to insufficient memory.

image


  1. Thread pool network programming

The pooling concept solves the problem of creating an unlimited number of threads and allows the code to take advantage of the multi-core CPU. However, a new problem arises: suppose there are 100 threads coming in, but the thread pool can only handle 10 requests. The remaining 90 threads can only wait in the queue for thread processing, causing a blocking problem.
The example above is still an ideal situation. Suppose the incoming 10 threads are all blocked for some reason, then the 10 threads in the thread pool must wait for these 10 requests to be processed before they can handle the remaining 90 requests. The blocking problem becomes very serious, resulting in wasted limited resources.

image

Conclusion#

Why use Netty? Because Netty uses NIO at the underlying level, it solves the communication problems mentioned above.

The End!!!

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.