r/java • u/elliotbarlas • Apr 26 '22
5,000,000 persistent connections, 200,000,000 messages, 0 errors with a single process using Project Loom.
https://github.com/ebarlas/project-loom-c5m9
u/lurker_in_spirit Apr 27 '22
Any idea how many carrier threads were required?
17
u/elliotbarlas Apr 27 '22 edited Apr 27 '22
Great question! I should include that in the docs.
As outlined in JEP 425, the number of carrier threads defaults to available processors.
The server-grade host reports 80 CPUs. I see 220 threads total and a Fork-Join worker pool of size 80.
$ grep ^processor /proc/cpuinfo | wc -l 80 $ ./jdk-19/bin/jstack 28226 > threads.txt $ grep tid= threads.txt | wc -l 220 $ grep tid= threads.txt | grep ForkJoin | wc -l 803
u/elliotbarlas Apr 27 '22
Added these details to the README doc. Thanks again u/lurker_in_spirit.
https://github.com/ebarlas/project-loom-c5m/commit/dfdd39090ac62ac20ca2db1ff71af1891d79b7d8
5
u/blargh9001 Apr 27 '22
what’s the learning curve for this for actual practical use?
Whenever I hear about project loom it’s not long before I start feeling out of my depth. I’m hoping that’s because it’s just the most advanced bleeding edge developers paying attention to it at this stage, getting into deep low level details…
7
u/elliotbarlas Apr 27 '22
In its current form, I tend to agree there are sufficiently many caveats and exceptions that it can feel out of reach and also produce some surprising results.
But the core concept is simple. In fact, virtual threads are all about simplicity. Same old threading primitives, same old synchronous code style. Much simpler than asynchronous programming with callbacks, futures, and the like. And also simpler than a brand new syntactic construct like async-await.
The best way to learn is to try it out! Early access Loom builds are available for multiple platforms. Getting started is easier than you might think.
5
u/papercrane Apr 27 '22
Looks good, and it's nice to see the code is pretty straightforward.
Unsolicited code review, but you probably want to use LongAdder in your code instead of AtomicLong. It should perform better under heavy contention like this.
4
u/elliotbarlas Apr 27 '22
Great suggestion!
3
u/elliotbarlas Apr 27 '22
I replaced
AtomicLongwithLongAdder.https://github.com/ebarlas/project-loom-c5m/commit/390d0040f7aadb80356ed3f56a162a7b9612581b
1
u/elliotbarlas Apr 27 '22
And an inevitable bug fix to follow.
https://github.com/ebarlas/project-loom-c5m/commit/72110d7d5004fb46fa51f1b58425d6e27b958c02
2
u/papercrane Apr 27 '22
lol, wouldn't really have called that a bug. The implementation of increment() is just add(1) anyways.
4
u/elliotbarlas Apr 27 '22
Good point. Though I may have fixed an
add(1)that should have beendecrement().3
24
24
Apr 26 '22
I am so pumped for this to go GA.
0
Apr 26 '22
[deleted]
22
Apr 26 '22
General Availability, where it's turned on by default and you don't have to enable preview features. Ideally in an LTS release, but that's not what I was referring to.
7
u/skippingstone Apr 27 '22
How does this compare to regular threads?
8
u/TheCountRushmore Apr 27 '22
Here are some simple comparisons that someone has been running against the loom builds.
2
u/Zyklonik Apr 27 '22
Depends on your usecase. Virtual Threads/Fibers/Green Threads will give you a massive boost on throughput. Latency will be unchanged . Basically, true data parallelism will depends on hardware - more cores/cpus, faster processing. More virtual threads, more concurrency. More responsive.
24
Apr 27 '22
Tell that to the node fucking js people
41
5
2
u/temculpaeu Apr 27 '22
you mean like uWebSockets.js can reach millions of connections with commodity hardware ?
2
3
u/papercrane Apr 30 '22
Discussion on Hacker News for anyone interested: https://news.ycombinator.com/item?id=31214253
2
2
Apr 27 '22
What do you think will loom make Go obsolete?
9
u/vips7L Apr 27 '22
I doubt it. Go still produces a lower memory footprint and deterministic performance due to being AOT compiled. Along with easier packaging into a stand alone binary.
15
u/Muoniurn Apr 27 '22
Deterministic, but slower performance, though. Java’s GCs are simply the state of the art, throughput-wise go doesn’t even come close to G1 and for latency, ZGC is generations ahead then Go’s quite naive GCs.
So for bigger applications in practice, Java will be faster.
2
u/cogman10 Apr 27 '22
Pretty much this.
Additionally, go optimized their compiler to compile fast. So while go could theoretically hit C speeds in terms of CPU performance, it's nerfed because the language devs decided optimization is less important than developer ergonomics.
1
u/preskot Apr 27 '22
You seem to compare apples and oranges. Those are two very much different memory models in place. Then there are things like escape analysis, which the two approach quite differently and where Go in particular has the upper hand.
1
u/Muoniurn Apr 27 '22
Java does escape analysis just fine. Go’s only upper hand is value types, but larger problems will pretty much require GC-d objects instead, where its relatively weak GC will be a hindrance.
1
u/preskot Apr 28 '22
But that's the point. Go doesn't need an advanced GC in the first place. You would know that if you've read how the memory model and allocation works.
1
u/Muoniurn Apr 28 '22
You can write only a very limited number of programs with stack allocation only. How many C programs can you count that do not use malloc? Sure, embedded does require such programs but Go with its heavy runtime is anything but ready for embedded development.
And even then, why not go for C# at least? I absolutely prefer Java, but Go is like the worst on all metrics. C# also has value types and pointers so there is absolutely nothing it couldn’t do that Go promises (other than go routines, but it is quite useless if the language otherwise sucks at concurrency. It can’t even goddamn defer unlocks at scope-level). And at least C# is a relatively expressive language, compared to Go’s atrocious copy-paste-loving one.
2
u/preskot Apr 30 '22 edited Apr 30 '22
Again, the fact the Go uses almost exclusively the stack to allocate objects (I’m looking at you Java strings allocation), it makes use of TCMalloc to counter fragmentation and allocate continuous blocks, it has working escape analysis (Java performs very poorly here) and supports value types (Java should finally get them with Project Valhalla) all point to the fact that Go simply does not need a complex GC like Java and C# do. Not sure what else you want me to say. These are different memory models.
As to the language itself. I love simplicity and I’m and oldschool pure C fan. Go allows me to be extremely productive, compiles fast and easy for all major platforms and beyond. I mostly write simple Makefiles. My day job btw is writing Java and Kotlin, so I do have a good comparison base.
0
Apr 28 '22
[deleted]
1
u/Muoniurn Apr 28 '22
As I mentioned, in toy examples with value types only. Do have a look at the benchmarkgame’s binary tree program which tests the GC. Almost any program will make use of many memory allocations, so it is a really important bottleneck.
But sure, some lower level like network microservice may perform better in Go than in Java — but then why even bother writing it in a managed language, when you could go lower to e.g. Rust and beat Go by possibly an order of magnitude.
5
Apr 27 '22
Java too can be compiled into a native executable with GraalVM.
// However with Go it is a built in feature and very comfy to use.
1
u/kiteboarderni Apr 30 '22
I wonder how much further this could go if using soemthing like onload? Obviously would need specific switch hardware, but it would be pretty cool to see.
0
Apr 27 '22
[deleted]
2
u/elliotbarlas Apr 27 '22
Because this is experiment is specifically targeting Project Loom virtual threads.
The milestone here is achieving 5,000,000 sockets with synchronous programming using a virtual thread per socket.
0
Apr 28 '22
[deleted]
5
u/elliotbarlas Apr 28 '22
Yes, but I've always viewed that as the worst of both worlds. Pay the price of complex NIO API without the benefits of non-blocking I/O.
In my view, idiomatic synchronous network programming in Java features sockets. In addition, there is a certain appeal to showing scalability with Java 1.0 primitives. The fact that synchronous network code compiled with Java 1.0 socket API can reach this scale is really remarkable.
9
u/spicycurry55 Apr 26 '22
Out of curiosity because I see you mentioned using bare metal and EC2 for testing, how expensive was this experiment?