News for hackers. Stuff that matters.
Re:Networking I/O with Virtual Threads – Under the hood by ec109685 on Thursday July 08, 2021 @01:39AM (#27768763)
I often find developers jump to async too early. Clearly a million native threads is too many, but on modern processors a couple thousand mostly idle (due to IO blocking) Java threads are fine (e.g. one per request in a typical middle tier). While Java's stack sizes are large, if a thread's stack isn't deep, most of that memory is never actually allocated.

What is that breaking point where developers should switch to more scalable models? From the article:

> There is a threshold beyond which the use of synchronous APIs just doesn’t scale

What is a good rule of thumb these days?

Re:Networking I/O with Virtual Threads – Under the hood by riobard on Thursday July 08, 2021 @01:59AM (#27768846)
> on modern processors a couple thousand mostly idle (due to IO blocking) Java threads are fine

This “mostly idle” assumption is gonna break the moment you hit a load spike and wake up most of those “idle” threads.

The success of async is often (mis)attributed to eliminating unnecessary resources, but in practice it's actually because async model implicitly enforces an invisible job queue, where usually only a single process/thread per core is heavy lifting the actual work, and async I/O will feed processing pressure back to the load source via network (e.g. TCP ACK).

Re:Networking I/O with Virtual Threads – Under the hood by fulafel on Thursday July 08, 2021 @02:57AM (#27769074)
I'm not sure scheduling the work in your own process necessarily solves the load problem better? The described queue behaviour would happen in the OS in the threads scenario, and TCP would work the same too.
Re:Networking I/O with Virtual Threads – Under the hood by riobard on Thursday July 08, 2021 @10:50AM (#27772683)
It's not about which way is better, but which default is more useful in practice.
Re:Networking I/O with Virtual Threads – Under the hood by jchw on Thursday July 08, 2021 @04:37AM (#27769552)
Context switching is pretty cheap, but it’s hard to beat free. Eventually the overhead of OS context switching is probably going to become non-negligible in most cases. I think up to a point the answer to when it matters is “that depends on how deep your pockets are.” You can keep scaling vertically and horizontally forever with more resources, obviously, but I don’t think it would take too long for the memory and CPU efficiency of async to start to win out, at least for runtimes with proper async.

Besides that, it’s pretty hard to decide you want to go async later on. In languages where async is not omnipresent, you usually get some form of explicit continuations; async/await sugar if you’re lucky. But you can always accidentally call something not async and hang, or just spend a lot of CPU time with no ability to preempt.

I think Erlang, Go and probably a couple of others basically stand alone in this regard; everything is just async by default. It’s a shame that it requires a decent amount of runtime baggage to accomplish this.

Re:Networking I/O with Virtual Threads – Under the hood by bestinterest on Thursday July 08, 2021 @08:43AM (#27771204)
I also think people forget that Robinhood was made with Python, Reddit used Python, Github/Gitlab use Ruby. Stripe AKA payments that need to be processed fast within SLA's use Ruby.

Ruby and Python can only ever have one thread running at a time due to global interpreter lock. Though to keep in mind when I/O happens on a thread it can be parked and another thread can be picked up to continue processing. But in essence one thread is running at a time and these languages built billion dollar businesses.

Just scale horizontally computers are cheap is the message I get and remember to keep it simple.

Re:Networking I/O with Virtual Threads – Under the hood by jchw on Thursday July 08, 2021 @12:23PM (#27774177)
Horizontal scaling works!

But, it’s only efficient in convenient, shared-nothing type architectures. If you are dealing with longer lived, more stateful stuff (like a game server perhaps) it’s not going to be so easy.

Of course, it’s easy for fancy VC funded stuff to defer optimizations later. If you are working solo you are probably going to want to, counter intuitively maybe, spend more time thinking about how to keep things optimal and simple. There’s probably startups out there worth real money whose entire stack could run on a $20/mo DigitalOcean box if it were better optimized, but actually runs on a $1000/mo Amazon or GCP account. Of course there is a lot to be said about that (obviously, the latter set up should, if done well, be a lot more resilient and prepared to deal with scale) but it does go a long way to demonstrate why someone might care about efficient async; the scale of money you can burn when you are bootstrapping or doing solo hobby projects is a lot different from well-funded startup or established business...

Everything has tradeoffs. I do believe Go wins in context switching, but the GC latency and CPU usage has definitely caused some problems. The Go developers have done an impressive job with GC latency and performance, but it’s hard to be cheaper than free, so sometimes Rust is a better option, for example.

Re:Networking I/O with Virtual Threads – Under the hood by BenoitP on Thursday July 08, 2021 @12:19PM (#27774101)
Python and Ruby are fine for embarrassingly parallel problems, like serving content from well-sharded users.

I'd be very surprised if Robinhood's trade matching engine is done in Python. I'd bet they use an off-the-shelf engine in C++/Java or another language with good multi-threading support.

> computers are cheap is the message I get and remember to keep it simple.

Fully agree! Getting to product-market fit is probably easier with python and ruby.

Re:Networking I/O with Virtual Threads – Under the hood by engie on Thursday July 08, 2021 @06:17AM (#27770146)
Latency can be a key consideration. Async style code can make it a lot easier to fire off a set of requests in parallel and wait for all the responses to come back (e.g. with https://docs.hhvm.com/hack/asynchronous-operations/concurren...), rather than the serial request waterfall a basic thread-per-request model will push you towards.
Re:Networking I/O with Virtual Threads – Under the hood by DaiPlusPlus on Thursday July 08, 2021 @02:09AM (#27768878)
Java may be an exception, but on Windows in general, and in .NET, every thread has its stack preallocated and is generally 256KB to 4MB by default, so spawning “a couple thousand threads” as you suggest will immediately consume 250MB-4GB of main-memory which adds considerable memory-pressure. That’s not something to be taken lightly. By using (true) async IO you’ll likely only need 20-50MB total because it uses the threadpool for continuations which is sized according to the actual available level of concurrency.

Async NetworkStreams or Sockets in .NET is a well-designed API over traditional BSD sockets’ select() function which is how you scale to tens of thousands of concurrent connections using only as many threads as you have hardware DoP - it’s not that I’m wowed that .NET has modernised it, it’s that Java’s solution is to wallpaper over a gaping hole in their fundamental design instead.

Re:Networking I/O with Virtual Threads – Under the hood by native_samples on Thursday July 08, 2021 @05:24AM (#27769806)
I don't see how this is wallpapering over a hole in the design. Far from it. Java has supported async IO since Java 1.4 using APIs like java.nio.channels.AsynchronousChannel. That's a more or less conventional wrapper around epoll.

What's being introduced now is a way to use that API without needing to touch the whole concept of async code. From the developer's perspective blocking calls and threads is pretty nice. It's async that's awkward and hard to use. Making the existing threads facility scale much better is a very clean approach: virtual threads introduces "virtually" no new concepts at all, in fact, if you aren't working with native code via JNI or Panama then you can act as if virtual and physical threads are the same. You literally just upgrade Java and maybe flip a couple of switches and you can now assign one thread per connection yet handle millions of connections (assuming you don't run out of other resources of course).

Re:Networking I/O with Virtual Threads – Under the hood by DaiPlusPlus on Thursday July 08, 2021 @06:17PM (#27778295)
java.nio.channels was introduced in Java SE 7, not 1.4. It also is far from a drop-in replacement for InputStream/OutputStream.

Even with Loom's green-threads (not the original green-threads) async IO in Java will be too otherworldly until Java's language designers cease their intrasigence about adding first-class support for `await`.

Re:Networking I/O with Virtual Threads – Under the hood by native_samples on Friday July 09, 2021 @04:43AM (#27781658)
I don't see how you arrived at that conclusion at all. Async/await is always strictly harder to use than green threads. Can you explain precisely why you view them as "otherworldly"?
Re:Networking I/O with Virtual Threads – Under the hood by DaiPlusPlus on Friday July 09, 2021 @06:53AM (#27782378)
"Async/await is always strictly harder to use than green threads"

I'm sorry but I can't take you seriously after you say something like that.

Re:Networking I/O with Virtual Threads – Under the hood by pjmlp on Thursday July 08, 2021 @03:27AM (#27769202)
Tainting the whole call is major problem in .NET code.

That is why you will find out lots of code bases full of Task.Run() and direct calls to .Result, instead of using async/await operators.

Having worked with both platform since their yearly days, I am not sure which design is actually the best one, considering brown field development.

Re:Networking I/O with Virtual Threads – Under the hood by DaiPlusPlus on Thursday July 08, 2021 @03:54AM (#27769336)
What you’re describing is just fake-async and that is discouraged in the ecosystem. I’m not aware of any major NuGet packages that use fake-async, and certainly nothing in the BCL.
Re:Networking I/O with Virtual Threads – Under the hood by pjmlp on Thursday July 08, 2021 @04:54AM (#27769630)
Not at all.

What I am describing is what a large majority of .NET consulting projects do, because async/await taints the whole call stack up to Main() or event handlers, and almost no one is doing .NET Core projects from scratch.

Just because it is discouraged at conference talks and blog posts, doesn't mean devs aren't doing it on the trenches when dealing with the crossfire of project deadlines and massive codebases.

Re:Networking I/O with Virtual Threads – Under the hood by DaiPlusPlus on Thursday July 08, 2021 @06:15PM (#27778284)
> Just because it is discouraged at conference talks and blog posts, doesn't mean devs aren't doing it on the trenches when dealing with the crossfire of project deadlines and massive codebases.

Oh, I'm sure of that, don't worry.

I'm just fortunate that I've never had to experience that myself.

Re:Networking I/O with Virtual Threads – Under the hood by ec109685 on Thursday July 08, 2021 @12:06PM (#27773855)
Unless the stack is used, the memory of those stack threads is never resident, so it doesn’t create memory pressure.
Re:Networking I/O with Virtual Threads – Under the hood by DaiPlusPlus on Thursday July 08, 2021 @06:15PM (#27778276)
That depends on how the memory-pages of the stack-space of the threads is managed. That could still result in memory-fragmentation and disk-paging.
Re:Networking I/O with Virtual Threads – Under the hood by ec109685 on Friday July 09, 2021 @12:54AM (#27780575)
At least the last time I tested on Java, resident memory << virtual memory as idle thread count increased.
Re:Networking I/O with Virtual Threads – Under the hood by fulafel on Thursday July 08, 2021 @02:45AM (#27769023)
A million threads is a lot, but the reasonable limit is more like 100k idle threads than a few thousand, on 64-bit Linux.
Re:Networking I/O with Virtual Threads – Under the hood by MichaelMoser123 on Thursday July 08, 2021 @12:13PM (#27773973)
if you get to a very high number of cooperative threads, then each such thread will have a very small stack (otherwise the OOM killer will get you). i am not sure that you can impose such limitations on the stack size in java.
Re:Networking I/O with Virtual Threads – Under the hood by fulafel on Tuesday July 13, 2021 @09:25AM (#27820633)
If you are short on memory, sure. 100k times 10 mb for example is just 1 TB, an order of magnitude or two less than what bigger x86 servers take.
Re:Networking I/O with Virtual Threads – Under the hood by nly on Thursday July 08, 2021 @04:38AM (#27769557)
Once you have an async API (of any kind), and have paid the cognitive and technical price of having one, you have a lot of flexibility about what you do on the implementation side - including thousands of threads.

If you start off with a synchronous API and thousands of threads, migrating to something else later is very expensive.

Re:Networking I/O with Virtual Threads – Under the hood by grandinj on Thursday July 08, 2021 @07:32AM (#27770612)
i run up to about 500 threads on my Java app with ease, with about 1/3 of them pumping significant data (think stereo streaming audio type data).

I started writing some fancy asynch code, but on checking with a profiler, the thread overhead was pretty negligable, so I left it alone.

Re:Networking I/O with Virtual Threads – Under the hood by dom96 on Thursday July 08, 2021 @09:03AM (#27771390)
I often find that developers jump for threads too early. Concurrency is much easier to reason about than parallelism.
Re:Networking I/O with Virtual Threads – Under the hood by signa11 on Thursday July 08, 2021 @09:45AM (#27771858)
using thread scheduling for packet scheduling goes only so far.