Blog about software development


High performance Java web service using Jetty

28 Mar 2016 - by 'Maurits van der Schee'

In my previous post I announced a attempts at high performance web server implementations in some popular languages (Java, Go, C# and JavaScript). Today I will show you a Java implementation and give you instructions on how to get it running on your own machine.

Jetty for high performance

Jetty is a Web server and "javax.servlet" container that can be easily embedded in devices, tools, frameworks, application servers, and clusters. Jetty is known to be the web application of choice of Yahoo's Hadoop Cluster, Google's AppEngine PaaS and Yahoo's Zimbra SaaS. Because it was used for such important projects, I expected that it would be hard to implement, but the opposite turned out to be true.

Source code

This is the code I found online for a Jetty powered Java-based server:

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;

public class HelloWorld extends AbstractHandler {

    public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        response.setContentType("text/html; charset=utf-8");
        response.getWriter().println("<h1>Hello World</h1>");

    public static void main(String[] args) throws Exception {
        Server server = new Server(8000);
        server.setHandler(new HelloWorld());

Save this file as "HelloWorld.java" in your project folder.

Installing Jetty

Install and run on Ubuntu Linux using:

sudo apt-get install openjdk-8-jre openjdk-8-jdk
wget -o jetty.jar http://central.maven.org/maven2/org/eclipse/jetty/aggregate/jetty-all/9.3.8.v20160314/jetty-all-9.3.8.v20160314-uber.jar
javac -cp jetty.jar HelloWorld.java
java -cp .:jetty.jar HelloWorld

The first two lines download the JDK, JRE and Jetty. The third command compiles and the last command should start the server.

Benchmarking Jetty

Fire a bunch of requests at it using Apache Bench:

ab -n 200000 -c 100 http://localhost:8000/

Lots of people have asked me how fast this example is. I suggest you test that for yourself, but to give you a rough idea: on my box this performs >30k requests per second with 70% CPU load. Just as good as a single line of PHP stating "echo 'Hello World';" served by "mod_php" under Apache.

NB: I have tested several implementations and analyzed their source code. I tried to pick a simple example that has high performance. If you feel there is better code, please let me know.

Links / References

PS: Liked this article? Please share it on Facebook, Twitter or LinkedIn.