5 Ultra Lightweight Http Server Implementations in Java for Blazing Fast Microservices, APIs or Even Websites
While Java is usually associated with extensive back-end infrastructures and complex server-side containers that require significant computing resources and well-designed setups, it's also very possible to create even in Java those lightweight and agile server implementations that many believe are exclusive of other realms, like Node.js and the like
In this article 5 ultra lightweight Java http server implementations are presented. From the first version that doesn't require other libraries than those already present in the jdk, up to later versions which increasingly require more or bigger external dependencies, we will see together that not only a blazing fast and agile server-side approach to software development is possible in Java too, but there are also a lot of different libraries and framework to choose from as a starting point.
1. "Look ma, no... dependencies!"
Yes, it's possible to write a complete Java http server with no dependencies at all in less than 80 lines of code. True, it's not the most modern and optimized solution you could desire. This is an old school version that doesn't use neither non blocking IO (NIO) nor it is multithreading, but it's usefull for two reasons:
- it teaches, or reminds us, a few things about the http protocol itself and Java in general in a very few lines of code;
- it is very lightweight, easy and fast, and of course extensible or even totally replaceable. If as part of a bigger infrastructure you need a very tiny http server for some minor services, well, here it is!
2. Faster, even smaller, and with almost no dependencies
Since Java 6 the JDK includes the package com.sun.net.httpserver. While this is not part of the Java standard, and as such a JDK implementation is not required to include it, both Oracle JDK and OpenJDK include it. So if you can count on one of those JDKs and you need a very small http server with decent performance and good extensibility, which offers a compact starting point implementation, even if partial, of RFC 2616 (HTTP 1.1) and RFC 2818 (HTTP over TLS), here we go.
The API also allows to implement the missing parts of the protocols and to extend the server in many ways. Further details are available directly from the javadoc pages docs.oracle.com/javase/8/... for JDK 8 and docs.oracle.com/javase/9/... for JDK 9.
Finally, note that this is a NIO server, and even if the presented version is not really multithreading, it's very easy to turn it into that.
3. Takes, only one jar, but very easy and complete
Takes is a very lightweight and well designed web development framework which has been developed adopting rock solid OOP principles, also fully explained in the documentation.
Despite its unique dependency, a single jar file smaller than half a megabyte, Takes offers:
- an essential yet complete documentation that let you really start in seconds;
- no configuration files required;
- Hit-Refresh-Debugging to avoid re-compiling and re-starting the application everytime you change something;
- built-in support for JSON;
- very easy to extend with third party libraries like Velocity for a template engine, restfb for Facebook API, and so on.
- free and open source under the very permissive MIT License
4. Undertow, performance and flexibility in less than 3 megabytes
Authored by JBoss, now developed by Red Hat, Undertow is a well known web server that fits absolutely well in this list of flexible, performant and small web servers. Starting with just 4 jars dependency at its entry level configuration, Undertow can be extended, if required, to support Web Socket, HTTP Upgrade, Servlet 4.0, and supports HTTP/2 out of the box, providing both blocking and non-blocking API’s based on NIO.
Having worked extensively with Undertow for some years, we can say that it's key strength lies exactly in its modularity and the way you can combine and extend its various parts. The overall feeling this framework gives to the developers who adopt it is that it's there to just let you add and use the functionalities, not to be something to which you have to adapt to in order to do what you need. Well, we believe this is one of the most appreciable traits a framework can have. The cherry on top of it all is Undertow's performance, we are talking here of one of the fastest server of the world.
Like the rest of JBoss software, Undertow too is free and open source, released under the Apache License, Version 2.0.
5. Rapidoid, the fastest of them all!
The reason Rapidoid is the last in this article has nothing to do with its qualities or its suitability to the list. At least for static, plain text pages, according to TechEmpower benchmarks, Rapidoid is the fastest http server of the world, even faster than some written in C++.
Rapidoid is a complete, modular Web Framework, but we have tested and we are talking here only of the rapidoid-http-fast module, which is the most minimalist setup you can have of Rapidoid, and also the only one that can make it suitable for our list in this article. In fact that's why we list Rapidoid as last: even in its smallest configuration we have here dependencies on 21 external libraries which amount to a little bit more than 4 megabytes. It's ok for us, but with more than this we would enter in a different category of servers, and those would be for a different article.
Using just rapidoid-http-fast is very feasible and pleasant. Rapidoid has extensive documentation and has been designed for high performance since its inception. It's built from scratch on top of Java NIO and offers: asynchronous I/O processing, minimized garbage collection pressure, fast object pools, reusable off-heap buffers, very fast and efficient HTTP parser, and many other optimizations.
Rapidoid is free and open source software, released under the Apache License 2.0.
In this article we have presented 5 of the best and most quick-to-be-setup solutions to do exactly that, demonstrating that simplicity, effectiveness and yet blazing fast performance and lightweight footprint are very feasible in Java too, even when talking of a simple http server. Was it for your next embedded mobile web server, or your next AWS Lambda installation, we are sure that at least one of these could be very well your next favorite best friend in business.