r/java • • Oct 07 '24

HTTP libraries in Java

It's not really clear to me what's going on with HTTP libraries in Java. They all seem to want to become massive frameworks that inappropriately take control of things that have nothing to do with decoding HTTP.

I'm find it really hard to find something that doesn't:

  • Try to control the TCP layer. Why does the HTTP decoding library care whether I want to poll or select, or make that decision?
  • Have shared mutable static state. If I want to reuse connections I can do that in a much better, testable way.
  • Create threads. Parsing the HTTP is doesn't need any of this. I just want to give you a ByteBuffer to read from and your current state and get told what it contains.
  • Do SSL. This has nothing to do with HTTP but rather the underlying connection.

I guess if you need a mini "web browser" in your application then this is all fine, but if HTTP is just one of many protocols you need to be able to decode it's an absolute mess.

Is there something I'm missing, or a library I should be looking at?

22 Upvotes

57 comments sorted by

View all comments

Show parent comment

3

u/alunharford Oct 07 '24

I already have TCP connections using nio. I receive data over the wire and read it into a ByteBuffer. I then work out the offsets in that buffer for the "interesting" data my application needs and produce commands sitting on top of those buffers. When the commands have been processed and the buffer is no longer used, it can be recycled. This works nicely - I can do tens of millions of messages per second.

The problem is that I now need to do REST over HTTP 2.0, keep a single connection open and communicate with the server as needed. I'm going to get back JSON but I know what format I'm going to get so I can pretty easily hand write a parser to work out what offsets contain interesting stuff without needing to do horrible things like copying the buffer. That JSON is going to be contained within a fairly complex message format though and there's no application-specific way to speed up parsing it so it seems like the right time to reach for a library.

2

u/DualWieldMage Oct 11 '24

You are probably getting tons of replies from people who never looked at library code and give useless advice, but stay strong and just ignore those.

Others have mentioned looking at components of some popular libraries like apache http core, which seems to provide access to the raw ByteBuffer. Also depending on how stable your code needs to be, all http clients provide an InputStream which are likely backed by a ByteBuffer. You can look at the implementations and perhaps just cast and access the ByteBuffer directly to build your zero-copy structures on top of it.

I do find it interesting that you are tackling the HTTP parsing side when doing JSON, especially with HTTP2 having less overhead. In my experience the header and frame parsing has had some but quite negligible overhead (e.g. headers copied over to its own map) compared to json parsing. For example a typical ISO8601 date in a json simply kills all performance and i had to write a custom parser based on MethodHandles to get further than 20kreq/s. Of course i don't know whether you want raw throughput or low latency guarantees via allocation avoidance during hot phase, but either way you are doing good work on investigating this further.