Netty, And A Fresh View Of Web Development
Rather a long time ago, Iteratia Corporation received an order to develop an unusual client-server application. It was assumed that the server would hold a huge number of simultaneous connections with desktop clients and perform exchanges of data between them, and later in this article, we'll talk about the issue of the server. Our company has extensive experience in writing classic Web applications; however, in this case, we realized that the traditional approach, which we regularly use for solving such kind of tasks, would be inappropriate.
The traditional approach involves using Java Servlet-containers suitable for working with HTTP-requests, which are certainly very comfortable, but not always highly efficient. The first thought that comes to mind when you need to increase the performance of client-server's interaction is to completely abandon the use of HTTP. In our case, the client communicates with the server to exchange small amounts of data, and our main priorities are maintaining performance and stability. For such tasks, the use of HTTP does not make sense because of its redundancy. For this reason, we decided to use another communication protocol of a lower level—TCP/IP.
At first glance, Java NIO is the best solution from the viewpoint of performance, but this framework requires the implementation of low-level code for working with sockets and data transformation, which entail significant developmental costs. Therefore, we needed a framework that would "hide" the work of Java NIO but with a versatile and convenient API.
Unfortunately, to date, there are few such ready-made frameworks - according to articles on the Internet, discussions on IT forums, and test reports - and the invariable leaders in this area for quite some time have been Apache Mina and Netty. And, if the first framework, we have already had an opportunity to try out, then about Netty, we have only heard positive reviews and intriguingly high benchmarks figures. As it turned out, Netty is better documented than Mina and is accompanied by detailed practical examples of how to implement a particular feature - that is why it was simple enough to master this framework. Overall, the success of these two frameworks was probably justified by the fact that they were both founded by developer, Trustin Lee. Here are his own words about these two frameworks.
Netty is a framework that supports multiple simultaneous connections and processes them asynchronously. The major advantages of this framework are its high performance, open source, and versatility, which means it supports most protocols of the client-server connection “out of the box," ranging from low-level protocols TCP / IP and UDP, up to contemporary WebSocket. The work with this framework gave the developers complete freedom, allowing us to solve transport problems of any complexity. No wonder that, in the end, our choice fell on Netty.
General information about Netty
For our customer's web application, we have developed our own communication protocol, trying to minimize the size of each message exchanged between the client and the server. And the most resource-demanding task was the writing of a class of encoders and decoders for the handlers of messages on both the server and client. Only some time after publishing the web application, we came across Google Protobuf framework, which solves exactly this issue, that is, it encodes and decodes the messages without assistance; you just need to describe the structure of all the messages in a special format and generate message classes for your programming language. Eventually, we decided not to move our application to Protobuf, but we nevertheless noted that in subsequent similar projects, Protobuf would certainly save us a considerable amount of time, especially as Netty supports Protobuf "out of the box".
Speaking about the productivity of solutions based on Netty, it should be mentioned that Netty channel handlers should perform only the logic that is directly connected with encoding-decoding the data. When the handler produces a message, all further actions on it should already be made in a separate flow - any blocking of the handler's flow not only inhibits execution of the handlers of other channels, but could also potentially lead to a loss of control over the channel. This simple rule will help you to get maximum performance results when utilizing Netty.
As a result of using Netty in our application, we have achieved an impressive performance; on the server with a 4-core processor and 16 GB of RAM, Netty easily handles thousands of simultaneous open connections, loading the processor by not more than 50% at its peak. This has not only saved us from the problems with the support of the live application, but has also reduced the costs of the hardware.
So, as Netty is so good, why do not we also apply this framework to our web development? Instead of the traditional web server, we could implement the server based on Netty, which is ideal for fast work with a large number of asynchronous requests to the server, and, as it supports them “out of the box”, we could use both AJAX and WebSocket requests right away, resulting in a more powerful server than the traditional one. So what do you think about this suggestion? Is it something you would consider? We’d be interested to hear your thoughts!
Source: Iteratia.com
Google+ Page














