Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket best practices in Java

Writing any kind of web server in Java (be it a webserver, RESTful webapp or a microservice) you get to use Sockets for dual channel communication between client and server. Using the common Socket and ServerSocket class is trivial, but since Sockets are blocking, you end up creating a thread for each request. Using this threaded system, your server will work perfectly but won't scale very well. The alternative is using Streams by means of SocketChannel, ServerSocketChannel and Selector, and is clearly not as trivial as common Sockets.

My question is: which of these two systems are used in production ready code? I'm talking about medium to big projects like Tomcat, Jetty, Sparkjava and the like? I suppose they all use the Stream approach, right?

like image 709
Miguel El Merendero Avatar asked May 29 '15 07:05

Miguel El Merendero


1 Answers

To make a web server really scalable, you'll have to implement it with non-blocking I/O - which means that you should make it in such a way that threads will never get blocked waiting for I/O operations to complete.

Threads are relatively expensive objects. For example, for each thread memory needs to be allocated for its call stack. By default this is in the order of one or a few MB. Which means that if you create 1000 threads, just the call stacks for all those threads will already cost you ~ 1 GB memory.

In a naïve server application, you might create a thread for each accepted connection (each client). This won't scale very well if you have many concurrent users.

I don't know the implementation details of servers like Tomcat and Jetty, but they are most likely implemented using non-blocking I/O.

Some info about non-blocking I/O in Tomcat: Understanding the Tomcat NIO Connector

One of the most well-known non-blocking I/O libraries in Java is Netty.

like image 141
Jesper Avatar answered Oct 27 '22 22:10

Jesper