How far can I push Java? Racing a 2010 HTTP client against C and Rust
I wanted to see how close Java can get to hand-written C and Rust, so I rebuilt an HTTP client from 2010 and raced it. Here's the journey, and what each result taught me.
I wanted to find out how far I could push Java.
Java runs banks, telcos and countless back-end systems, but when people want the absolute fastest code, they reach for C or Rust. The common belief is that Java’s conveniences, garbage collection above all, cost too much speed. So I took an old Java HTTP client, made it as fast as I could, and raced it against clients I wrote in C and Rust.
The test subject
hotpotato is an HTTP client for Java. Bruno de Carvalho wrote it in 2010, built for raw speed. In 2011 I worked on my copy and added a way to run several clients together. Then, like most side projects, it sat untouched for fifteen years.
It’s a good test of Java’s limits because it’s made of everything Java is supposed to be bad at: huge numbers of short-lived objects, threads handing work to each other, and a hot path where every microsecond shows. In a large system an HTTP client can make hundreds of thousands of calls a second, so how efficient it is decides how many servers you need.
Rule one: measure before you touch anything
Before changing a single line, I built a benchmark: a test server, several workloads, and every test repeated, with the versions taking turns so a laptop warming up couldn’t favour one of them.
It paid off straight away. My first move was the obvious one: upgrade the networking library hotpotato is built on, Netty, to a newer release of the same generation, and take the free win. It got slower.
Measure every upgrade. “Newer” and “faster” are not the same word.
The garbage collector that never got a break
Java’s garbage collector is normally cheap, because most objects die almost as soon as they’re created. When I profiled hotpotato, though, close to half its time was going on garbage collection.
The cause was one small piece of code. Every request scheduled a two-second timeout, and when the response arrived, nobody cancelled the timer. So every finished request, response and all, stayed reachable for two full seconds: at hundreds of thousands of requests a second, a mountain of dead work for the garbage collector to keep copying around. Behind that sat the timer itself, which accepted at most 200,000 new timeouts a second, a hard ceiling however fast everything else was. With both fixed, hotpotato passed a million requests a second for the first time.
Firing the middle manager
Patching had got me a long way, but now every measurement pointed at the design itself.
In the old hotpotato, every request crossed threads three or four times: from the caller, to one coordinating thread, to the I/O thread that talks to the network, and back again. Every hand-off costs time, especially when the next thread is asleep and has to be woken. And that coordinator, the middle manager every request had to go through, was busy 95% of the time.
So for version 2.0 I rebuilt the core. The middle manager is gone: each destination has its own queue, and the I/O threads pull their next request themselves. Completing a request is a single atomic step with no locks. Each request carries its own deadline instead of a timer. And it all sits on Netty 4.2, with pooled memory and each operating system’s fastest networking layer.
The delivery van lesson
The first version of the new design was slower than it should have been, and the reason taught me the most useful lesson of the whole project. It was about pipelining: sending several requests down one connection without waiting for each response.
My new code tried to be fair. Whenever a request arrived, it woke up any connection that had room for it. That spread requests thinly, one per connection, one trip each, like a delivery company sending out a van for every single parcel. Pipelining is fast because it batches, and I’d just removed the batching. Waking only idle connections, and letting busy ones collect a whole bundle of waiting requests in one trip, took hotpotato from 598,000 to 1.83 million requests a second.
The mail room
Even with the new core, profiling showed where most of each request’s memory went: copying. Netty’s HTTP decoder copies every header into new objects and wraps each response in new message objects. It’s a mail room that copies every parcel’s label onto a fresh index card and rewraps the parcel before delivering it, and every card and wrapper is garbage moments later.
So for version 2.1 I replaced it. hotpotato now parses responses straight out of the network buffers: headers stay as raw bytes until something asks for them, and the body is handed over as a slice of the buffer it arrived in, never copied. I also added a reusable exchange, an alternative to the usual one-future-per-request API that carries one request after another. Pipelining through it allocates 1 to 10 bytes per request. Together with a few smaller fixes, 2.1 did 3.7× the pipelined throughput of 2.0, at a fifth of the CPU per request.
Plenty of ideas I was sure would help didn’t, and each one was measured and thrown out:
- Virtual threads: 13% slower, using 80% more CPU.
- Spreading requests evenly across connections: throughput fell from 1.03 million to 188,000 requests a second. The delivery van again.
- Other garbage collectors: none was a clear win.
- Busy polling on macOS: slower. Hold on to that one.
The race
I wrote the opponents myself:
- C, the classic choice for speed. My C client does the bare minimum: no timeouts, no bookkeeping, nothing tidied up for the caller. It parses responses with a proven fast parser.
- Rust, with the same design on tokio, Rust’s standard async runtime. Plus hyper, the HTTP client most Rust programs actually use.
- Java’s built-in client, as the baseline.
Rust surprised me before the race even started. On tokio’s default work-stealing runtime, where threads share tasks and steal work from each other, my Rust client was 5× slower than the same design in C, and adding threads made it slower still: nearly every response meant waking a thread on another core. With a separate runtime on each thread, set up like the C client, it matched C. The charts show both.
Everything ran on Linux, in a virtual machine on my Mac with 8 cores shared by the clients and the test server. Every client ran each workload three times, taking turns, and each C and Rust client kept its best thread count. hotpotato ran on its defaults, either event-driven like the C and Rust clients, or with blocking callers, the way most Java code uses it. Treat the exact numbers as a guide; the comparisons are fair.
Besides throughput, the charts show CPU time per request, the client’s processor time divided by the requests it completed, and the slowest 1%, the 99th-percentile latency.
Everyday traffic
One service calling another over 16 keep-alive connections.
hotpotato reached 80% of hand-written C, nearly 2× hyper and 33× Java’s built-in client. That’s a full-featured client within a fifth of a bare-bones one. With blocking callers it managed about a third of its event-driven speed, because every response means waking a parked thread.
Pipelining
10 connections, with 50 requests in flight on each. hyper and Java’s built-in client don’t pipeline, so they sit this one out.
61% of C. Before the mail-room fix it was under a fifth. Each request still costs hotpotato nearly three times the CPU it costs C, and that’s where the remaining gap lives. With bigger responses, 16 KB instead of 1 KB, hotpotato reaches 88% of C: moving the bytes costs the same in any language, so what’s left is a fixed cost per request. Profiling puts most of it in Netty’s own layers rather than in hotpotato’s code.
One request at a time, and the time Java beat C
One caller, one connection, one request at a time: pure latency. Event-driven, hotpotato came within a few microseconds of C, level with hyper. With a blocking caller it was about twice as slow, because the caller parks after each request and has to be woken when the response arrives.
So I went after the wake-ups with busy polling: instead of sleeping until the kernel says data has arrived, the I/O thread keeps checking. It stands at the door instead of waiting for the doorbell. On macOS that had made things worse, but Linux supports it properly.
109,000 requests a second, at a median latency of 7 µs. Java, 2.7× faster than my C client.
With an asterisk, and it’s a big one. My C client sleeps between responses, so this isn’t like for like. Busy polling keeps a whole core busy even when nothing is happening, and with more polling threads than cores it runs about four times slower. It’s a tool for a few latency-critical callers on a machine with cores to spare, not a default.
So, how far can you push Java?
Further than “fast enough”, and closer to C than I expected when I started.
Busy polling is left out because my C client doesn't use it. With it, hotpotato did 2.7× C one request at a time.
It isn’t all good news. With chunked responses, hotpotato lost up to 30% in some runs and almost nothing in others, and I don’t know why yet. And with 16 connections, its p99 latency is two to three times C’s.
What slowed Java down wasn’t really the language. It was allocation, hand-offs between threads, and wake-ups. Rust showed the same thing from the other side: the same code ran five times slower on a different runtime layout. Those are design choices, and design choices can change. Most of the remaining gap sits in Netty’s own layers, so going around them for the common case is what I want to try next.
The lessons I’m keeping, whatever language I’m in:
- Measure every change, including upgrades.
- Batching beats spreading work evenly.
- How the work is organised matters as much as the language.
- Knowing where the time goes isn’t the same as knowing what’s worth fixing.
Try it
hotpotato is open source under the Apache 2.0 licence, on GitHub, and needs Java 25. It isn’t on Maven Central yet, so for now build it from source:
git clone https://github.com/jasondevj/hotpotato
cd hotpotato
mvn install
Then add it to your project:
<dependency>
<groupId>com.jasondevj</groupId>
<artifactId>hotpotato</artifactId>
<version>2.0.0</version>
</dependency>
And send a request:
DefaultHttpClient client = new DefaultHttpClient();
client.setMaxConnectionsPerHost(10);
client.init();
FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/");
request.headers().set(HttpHeaderNames.HOST, "example.com");
client.execute("example.com", 80, request, new BodyAsStringProcessor())
.addListener(future -> {
if (future.isSuccessfulResponse()) {
System.out.println(future.getProcessedResult());
}
});
The README shows how to use the reusable exchange, and lists the settings that made a measurable difference in these tests, including busy polling. Every result in this post can be reproduced with the benchmark scripts in the repository.
I’d love to see numbers from hardware I don’t have, especially Linux running directly on a machine rather than in a virtual machine. If you try hotpotato or run the benchmarks, open an issue on GitHub with what you found, good or bad.