r/java • u/elliotbarlas • Feb 14 '22
Microhttp is an event-driven, single-threaded, zero-dependency web server with 500 LOC. Benchmarks on EC2 show 100,000+ requests per second and 50,000+ persistent connections.
https://github.com/ebarlas/microhttp60
u/nutrecht Feb 14 '22
You should add a license. As it stands it's not actually open source.
Anyway; it's pretty neat :) I doubt anyone would pick it over Netty, but it looks like a fun project.
37
u/elliotbarlas Feb 14 '22
Agreed. Any license suggestions? Something that is maximally permissive and well known.
At least one person will pick it over Netty!
56
u/nutrecht Feb 14 '22
I just slap MIT on everything. It's basically the "here, take it, I don't care" of OS licenses :)
48
u/elliotbarlas Feb 14 '22
Added MIT license via the inline add-file option in GitHub. Very convenient!
4
Feb 15 '22
The WTFPL is far more literally that.
1
u/thomascgalvin Feb 15 '22
The WTFPL also makes a bunch of lawyers break out in hives. Avoid it if you want people to actually use your software.
1
0
u/nutrecht Feb 15 '22
Sure, but it's obviously a joke license. A big part of "do whatever" is the "but you can't sue me". MIT has that. That joke license doesn't. Not to mention the unprofessional language.
18
u/stefanos-ak Feb 14 '22
I find that when projects of this category (micro-implementations) publish benchmarks, then other people compare those with popular implementations, which leads to invalid conclusions. It's not an apples to apples comparison.
But, I like projects like this :)
8
u/elliotbarlas Feb 14 '22
Agreed. If you have any recommendations in that regard, I'm all ears.
Microhttp isn't weighted down with features. I'm confident that it would be competitive with other servers. But I'm not sure how best to compare.
5
u/_INTER_ Feb 15 '22
see https://www.techempower.com/benchmarks/, maybe gives you some input
2
u/elliotbarlas Feb 15 '22 edited Feb 15 '22
The method used is completely out in the open. This is excellent. In fact, one of the tests is indeed a static hello world test, very similar to my own.
I'll report back once I've had a moment to go through the documentation.
1
u/elliotbarlas Feb 15 '22 edited Feb 15 '22
A colleague pointed this out as well. I'll see if they publish the methodology used.
11
u/tristan97122 Feb 14 '22
Looks neat! I suspect the « no dependencies » goal is what motivated avoiding slf4j for the logs?
21
u/elliotbarlas Feb 14 '22
Yes, in part. If you look, you'll notice a slightly different logging abstraction.
Logging in Microhttp is treated more like data. Each log event consists of key-value pairs. It reflects the way I think about logs. Rather than a free form string, each log event is something that can be indexed, searched, etc.
11
u/pron98 Feb 15 '22
You should consider using JFR for this kind of logging. JFR is now the main focus of OpenJDK's monitoring and profiling work. Not only is logging and storage efficient, but there are APIs for online streaming consumption of the logs.
1
u/elliotbarlas Feb 15 '22
Thanks for pointing that out. I'll need to take a closer look at JFR. That might indeed be the right abstraction for this sort of trace event logging.
6
u/lambdacats Feb 15 '22
Nice, this is how I like to do my logging. Structured events are so much easier to work with.
6
5
9
u/mdaniel Feb 14 '22
When developing a custom HTTP parser one should either consider, or say they're out of scope, the almost unbounded sharp edges of the HTTP spec. The one that sprang to mind was HTTP request smuggling, which might not specifically affect this project but for sure represents the kinds of bugs I'm highlighting
1
u/elliotbarlas Feb 14 '22 edited Feb 15 '22
Agreed. I could certainly elaborate more on the limitations and the areas where Microhttp falls short of the spec.
I actually think that the simplicity and accessibility of the code gives it an advantage in the security arena. One can reason easily about the behavior. There is also a security benefit from the discretization that occurs. No matter what is sent, the request size limits are enforced.
9
u/mdaniel Feb 15 '22
I am torn about whether I am belaboring the point, or adding clarity, but in the spirit of the latter: your current implementation does exactly what both of the cited vulns claim: it is vulnerable to CL:CL and CL:TE
Defending against DoS by limiting the
new byte[]allocation is great, but being aware that it is already vulnerable to two known classes of request smuggling is something that can help you grow as an engineer, even if this project was just for learning and not for deploying on the wild Internet6
u/elliotbarlas Feb 15 '22
Thanks for pressing the issue. I wasn't aware of request smuggling.
For Microhttp to be vulnerable, it must be proxied by a non-compliant frontend, which seems like a very strange configuration. As stated in the docs, Microhttp is tailored for integration with an edge server or proxy that provides TLS, HTTP/2, etc.
Having said that, the remediation is exceedingly simple.
6
u/elliotbarlas Feb 15 '22
Pushed commit that forbids multiple transfer-length headers.
https://github.com/ebarlas/microhttp/commit/b0648bf9b7b59c9ac9f54b7be5fe26abe190489a
5
u/vasiapatov Feb 14 '22
Looks great, code is clean and well-structured. Implementing core technologies yourself is an invaluable learning experience, kudos!
2
3
u/chabala Feb 15 '22
How do you actually use this? Readme has a code snippet, but no mention of the library being published anywhere; from the coordinates in the pom, I know it's not on Central.
2
u/elliotbarlas Feb 15 '22
A Maven dependency with microhttp grpupid and artifactid. I'll add a snippet to the readme shortly to clarify this point. I'll also publish 0.1 to Maven Central.
2
u/chabala Feb 15 '22
Looking forward to that. I am surprised that your repo picked up so many stars, so quickly, when it's essentially an unpublished preview at the moment. It didn't even have a license until suggested by /u/nutrecht.
3
u/elliotbarlas Feb 15 '22 edited Feb 15 '22
I suspect there's an appetite for minimal solutions in the age of overwhelmingly complex frameworks crowding the market.
I just grabbed the microhttp.org domain and I'll hopefully have an initial release in Maven Central shortly.
And I ought to have a branch of the TechEmpower benchmark ready shortly as well.
3
u/elliotbarlas Feb 15 '22
Microhttp is now available in Maven Central. I updated the README as well.
<dependency> <groupId>org.microhttp</groupId> <artifactId>microhttp</artifactId> <version>0.1</version> </dependency>
3
u/guywithcircles Feb 15 '22
This is perfection, so clean, congrats.
I'm looking at it while reading Spring documentation LOL.
It brings that simplicity mindset that sparked my curiosity when I first started, but also that keeps me excited about the future of this industry.
Given cloud nodes these days tend to have SSL termination endpoints already (sending plain HTTP requests east-west is a no-no and side-cars are expensive), this can actually be good for adoption in that context.
4
u/elliotbarlas Feb 15 '22 edited Feb 15 '22
Thanks! It's kind of shocking how little is actually needed from a backend web server. This provides everything I need from a web server for a REST/JSON/GraphQL type of ecosystem.
2
u/maxandersen Feb 15 '22
Nice little micro focused project - if I read it right your test is to put load on a response that returns “hello world” ? That will give you insanely misleading comparison numbers as it excludes any io, and buffer overruns nor anything with a bit compute. I.e. what a normal server will have to deal with.
Do you have a more real load test run ?
1
u/elliotbarlas Feb 15 '22
Yes, that's right. The test is specifically meant to highlight the potential of the library itself. But I'll happily run other benchmarks that folks recommend. I'm not personally aware of a standard in that regard.
1
u/elliotbarlas Feb 15 '22
After doing a little more digging, it turns out hello world tests are indeed part of a widely used benchmark suite found here:
2
u/NPException Feb 17 '22
I love the little thing! :D
I have one question though: What is the rationale behind having the headers be Lists of Header records? Would just a Map<String,String> suffice? The way that headers are treated in the Request assumes that there is only ever a single header of the same key, so a regular Map sounds reasonable in my head.
1
u/elliotbarlas Feb 17 '22
It's tempting to use a map, but at minimum a multi-map is required since HTTP permits multiple occurrences of the same header by name.
A list of header name-value pairs just struck me as the absolute simplest. And though I haven't measured, a linear scan through a small list or array is typically faster than a hash table lookup.
2
u/NPException Feb 17 '22
The performance for small lists is a good point.
If I am not mistaken, while multiple headers with the same name can occur, they are equivalent to just one header with the values concatenated with a comma. So it wouldn't rule a Map implementation out entirely.
2
u/elliotbarlas Feb 17 '22
You're right. Section 4.2 of RFC 2616 says the following.
It MUST be possible to combine the multiple header fields into one "field-name: field-value" pair, without changing the semantics of the message, by appending each subsequent field-value to the first, each separated by a comma. The order in which header fields with the same field-name are received is therefore significant to the interpretation of the combined field value, and thus a proxy MUST NOT change the order of these field values when a message is forwarded.In any case, I do prefer the simplicity and clarity of a list which communicates exactly what was seen in the order they were parsed.
1
u/bowbahdoe Feb 14 '22
What is the reason for the "reason" field on responses? At least with ring all thats needed is status, headers, body.
On that note, are you able to support everything required by the ring spec?
1
u/elliotbarlas Feb 14 '22
For the same reason so much else is omitted: simplicity and brevity. I don't want Microhttp to maintain a mapping of status code to reason. And it doesn't impose much of a burden on application programmers.
1
u/lufyluo Feb 16 '22
hi! OpenJDK 17.0.2 is the lowest version?
2
u/elliotbarlas Feb 16 '22
Java 17 is required. Microhttp makes use of records in many places, among other things. OpenJDK 17.0.2 is the specific build I used for the benchmarks. Any JDK supporting Java 17+ ought to work.
2
-8
u/Worth_Trust_3825 Feb 14 '22
Great example at marketing, where server that does nothing at all is praised for being "fast".
15
u/elliotbarlas Feb 14 '22
It's a networking library. The benchmarks are intended to highlight the cost and overhead of that layer, not the application.
If you have an alternative benchmark suggestion, I'm listening.
7
u/Chemical-Birds Feb 14 '22
It is still a decent number.
Try to achieve this numbers and you will see how many things will go wrong...
I made a while ago a POC for a payment system, we achieved 10k/s with tomcat, and yes it was doing "nothing", it was returning a dummy response... but oh boy ...it was a lot of tuning and investigation, at some point even hardware failures (network card)
1
u/lambdacats Feb 15 '22
Nio is fast, async io is fast. I don't think it's fair to say that the wrapper is fast by comparing it to web servers. It is however a good example on how to use the nio API.
3
u/elliotbarlas Feb 15 '22
But, it is a web server. I designed this to meet all of my web server needs in a typical REST/JSON setting.
If you can think of a fair metric or benchmark to use for comparison, I'd readily consider it.
1
u/elliotbarlas Feb 15 '22
The closest thing to a standard benchmark includes a hello world test nearly identical to the one I used.
-9
u/thisisjustascreename Feb 14 '22
Neat toy, but it doesn't even support TLS.
38
u/elliotbarlas Feb 14 '22
As the name implies, it's absolutely minimal. It doesn't support what isn't needed in a typical deployment setting. Every web application I deploy in AWS cloud terminates SSL at the load balancer (ALB/ELB) or CDN (CloudFront).
1
u/lambdacats Feb 15 '22
How do you reason about privacy and security when running plaintext transfers in the AWS cloud?
4
u/elliotbarlas Feb 15 '22
A VPC is just that, a private network. Running almost any workload in the cloud requires a level of trust in the provider. I also trust AWS with encryption keys (KMS), objects (S3), and in countless other domains.
1
u/lambdacats Feb 15 '22
Ah ok, well if you give Amazon the keys it barely matters anyways. At this level of trust the projects I've worked on could never be allowed in the cloud. I was wondering about ways to make it more secure / require less trust in the provider.
2
Feb 15 '22
If you really want to go hardcore with your zero trust, run the Microhttp instances in containers with sidecar proxy doing the heavy lifting of mTls.
1
1
u/nutrecht Feb 15 '22
At this level of trust the projects I've worked on could never be allowed in the cloud.
Ah, the age-old "we don't trust AWS so we're going to build our own datacenter with a bunch of underpaid sysadmins instead".
2
u/lambdacats Feb 15 '22
To specify it has all been with government, so there are security, privacy and also legal concerns. I don't know if they are underpaid or not.
1
u/nutrecht Feb 15 '22
To specify it has all been with government
Governments are generally now starting to realize that humans are by far the biggest factor in security and that letting a company that specialises in this stuff and can afford world-class developers tends to do better than a random bunch of nobodies you hire for your own datacenter.
There's a reason even banks use the cloud extensively.
2
u/lambdacats Feb 15 '22
The authorities that work with security or manage sensitive information shouldn't store it in a private business, they should own physical access and hire consultants as needed. I agree that it doesn't make sense for most of government, which is why we should have state owned data centers. Rather than giving away free power AND buying services from cloud providers - the data is ensured to be stored within the country.
2
Feb 15 '22
Any popular cloud allows you to configure that your data is hosted in datacentres in a specific geographic location.
There's plenty of advantage to using a known cloud implementation, with published and well-known APIs and services. Hiring staff becomes a lot easier when you're using standards available to everyone, and the pool of consultants you can draw on is larger, thus more competitive. Have you seen the money people charge when they know they're one of about a dozen people who know your specific tech platform intimately?
→ More replies (0)1
Feb 15 '22
AWS themselves recommend a zero trust model. It isn't about not trusting the platform, it's about bad actors who may be within your enterprise.
2
u/nutrecht Feb 15 '22
it's about bad actors who may be within your enterprise.
And this is almost always a much bigger issue than any perceived security of a cloud provider. Be it 'bad actors' or simply a sysadmin leaving something open that should not be open.
1
Feb 15 '22
Indeed. To be clear, I'm very much not in the 'this is too important to go in the cloud' camp.
8
u/crummy Feb 14 '22
do people actually do TLS in their Java webservers? i.e. they aren't behind nginx, caddy etc?
5
u/elliotbarlas Feb 14 '22
Not in my experience. A perfect example of a reasonable feature in a comprehensive, general purpose web server that just isn't used in practice.
7
u/stringbet Feb 14 '22
Eh.. there are reasons to do it. When you're handling 1m requests/s, putting the SSL on your LB is a significant CPU burden which makes scaling harder. Pushing SSL back to your server pool allows the LB to operate in Layer 4 and thus it can handle a lot more traffic.
5
u/elliotbarlas Feb 14 '22
I've never heard that argument before. Load balancers are horizontally scalable, so while it does place a greater burden on the LB, I don't think that's actually problematic.
Also, HTTP aware layer 7 load balancers are indispensable.
6
u/stringbet Feb 14 '22
Definitely not disputing the value of Layer 7 load balancing. My point is that an LB is a larger unit of scaling than backend servers. If I can handle 10% more traffic by adding a cheap backend server, great.. but at some point, I have to add another (expensive) LB. The longer I can delay that, the better. I'm speaking from personal experience here, so this is not a theoretical situation.
2
u/elliotbarlas Feb 14 '22
Thanks for clarifying, that makes sense. I'll keep that in mind.
Do you deploy that setup in the cloud? The certificate management capabilities and integrations in AWS are another reason that terminating TLS on the LB or CDN is a natural choice.
3
u/stringbet Feb 14 '22
Nope, this is an on-prem environment. Believe me, there have been significant cost comparisons done with the cloud and for our workload, this is the better deal. Horizontally scaling load balancers isn't a technical challenge, just a capex decision.
1
u/lambdacats Feb 15 '22
Why are L7 load balancers indispensable? Any time I've got requests for it has been because sticky sessions are needed due to poor design.
2
u/elliotbarlas Feb 15 '22
AWS CloudFront, Application Load Balancer, and API Gateway are all L7 proxies. They provide essential services. I'm not sure why sticky sessions came to mind. That's not a feature I use personally.
1
u/lambdacats Feb 15 '22
Okay, but I'm still wondering what essential services you need in L7? It's the only argument I've heard for wanting L7 load balancing.
2
1
u/yawkat Feb 15 '22
If you don't support tls you can't use http 2 except with h2c (which is super ugly) or prior knowledge (which is poorly supported). That alone is a good reason to use tls
2
u/elliotbarlas Feb 15 '22
Both TLS and HTTP/2 ought to be provided by an edge server or proxy. That is a very standard arrangement and it's the one I use in my AWS deployments. CloudFront and ALB both support this configuration.
https://aws.amazon.com/blogs/aws/new-http2-support-for-cloudfront/
1
u/yawkat Feb 15 '22
You can't support all HTTP 2 features if your backend is HTTP 1, and it's much less efficient too.
2
u/elliotbarlas Feb 15 '22
The main leap with HTTP/2 is advanced multiplexing to address head-of-line blocking. That benefit remains almost entirely intact when TLS and HTTP/2 are terminated at the edge.
2
Feb 15 '22
You absolutely can deploy TLS on a Java origin server. It isn't generally done, but all the big name Java web servers allow it. It's not an enormous task to add TLS support, either.
2
u/elliotbarlas Feb 15 '22
SSLEngine serves exactly this purpose. It would add quite a bit of additional complexity, but it's certainly doable.
1
3
1
-10
u/guitcastro Feb 15 '22
As it does not have dependency would be great if was written in kotlin, then you could compile to kotlin native. I wounder how much faster it would be.
6
u/elliotbarlas Feb 15 '22
As far as I know, Kotlin doesn't even have its own network subsystem. The closest thing is Ktor which itself is built atop Java NIO.
In any case, I would personally use C, C++, or Rust if I wanted natively compiled binaries.
The amazing thing about the JVM is that it provides nearly the same performance. Ultimately, Microhttp gets just-in-time compiled to optimal machine code making the normal select/poll/epoll system call.
4
u/pron98 Feb 15 '22
AOT compilation usually brings some slowdown, as the JIT compiler can do more aggressive optimisation, but, possibly more importantly, there are currently no GCs available for AOT compilation (at least open implementations) that are as good as those in OpenJDKs.
2
0
Feb 15 '22
[deleted]
3
u/elliotbarlas Feb 15 '22
I don't know if it has a name. Broadly that approach is called a fluent API. I didn't see the utility in separate classes for Options. It's effectively a parameter struct.
5
3
u/hippydipster Feb 15 '22
The downside of this is you don't get an immutable object. Well, technically it's possible, if every "withXXX" method creates a new object.
1
u/nutrecht Feb 15 '22
Yup. But you can do this with a separate builder class. My personal preference.
2
3
Feb 15 '22
[removed] — view removed comment
3
u/elliotbarlas Feb 15 '22 edited Feb 15 '22
Thanks! Yes, I'll publish a version to Maven Central in the near future. You'll find a thread about the logging question elsewhere in the comments.
In short, my aim was to use structured log event records rather than free text.
2
u/elliotbarlas Feb 15 '22
Piotr, I published version 0.1 in Maven Central earlier today. Reach out with questions or feedback or contributions.
<dependency> <groupId>org.microhttp</groupId> <artifactId>microhttp</artifactId> <version>0.1</version> </dependency>2
28
u/agentoutlier Feb 14 '22
It looks like you might not handle
Expect: 100-continueI know you don't want to deal with streaming but it might still be worthwhile to handle the response as I have seen some clients hang or go really slow if you don't respond with
100 continueparticularly on PUT.