News for hackers. Stuff that matters.
Re:How JavaScript works: Event loop and the rise of Async pr... by nitely on Saturday October 14, 2017 @03:25PM (#15473740)
> Does this mean that a web server written in node is running single-threaded?

Yes.

> But doesn't running on a single thread put an upper limit on the amount of work a server can handle?

Is not about how much work it can handle, it's about how much it can offload. Async servers can handle a much greater volume of I/O bounded tasks. So it can handle more connections. When the task is CPU bounded you can either create a thread (which does not really scale well) or offload it to some other servers that can scale horizontally (i.e doing micro services)

> And, if the solution is to spin up more servers with access to the same database, doesn't that mean that we are now having multiple threads accessing the database concurrently? Much like, say, Python Django?

Yes, to take advantage of all CPU cores you have to create more server instances. But why would they talk to the same database instance? It could be a replica or a shard. You can even have a pool of shards connections per server instance.

Re:How JavaScript works: Event loop and the rise of Async pr... by amelius on Saturday October 14, 2017 @03:42PM (#15473819)
The problem with offloading to a thread is that you can't structurally share data with another thread. So any copying of e.g. arguments can be very expensive, and awkward/inconvenient as well.
Re:How JavaScript works: Event loop and the rise of Async pr... by fauigerzigerk on Saturday October 14, 2017 @04:06PM (#15473963)
If the other thread/process is running on a different machine (or you want to keep that option open) that's what you're going to have to do anyway though.
Re:How JavaScript works: Event loop and the rise of Async pr... by amelius on Saturday October 14, 2017 @08:25PM (#15475113)
Yes, but that's a big "if".

Sometimes you want to use a thread to keep the CPU from blocking (e.g. long database operations).

Also, sometimes you want to use the CPU to its full capabilities.

Re:How JavaScript works: Event loop and the rise of Async pr... by fauigerzigerk on Sunday October 15, 2017 @04:58AM (#15476435)
>Sometimes you want to use a thread to keep the CPU from blocking (e.g. long database operations).

On the contrary. That's the prototypical use case for non-blocking event based IO. No threads needed.

>Also, sometimes you want to use the CPU to its full capabilities.

You can use all CPUs by using multiple processes. That's not an issue.

Threads are useful when you want to run multiple algorithms in parallel on the same bigish in-memory data structure, especially one that has a lot of pointers.

Something like an in-memory graph database or a desktop application that lets you work with huge files in-memory, or even complex user interfaces. For instance, I'm not convinced that the cross process bridging that React Native has to do is a great idea.

So yes there are use cases where threads are very beneficial. But on the server side it's essentially the database/analytics systems themselves, not the code that accesses them.

Re:How JavaScript works: Event loop and the rise of Async pr... by frutiger on Saturday October 14, 2017 @11:55PM (#15475809)
Database access, likely the most common I/O operation from a web server, is certainly running in another process (or on another machine) - I'm not sure that "if" is as big as you suggest.
Re:How JavaScript works: Event loop and the rise of Async pr... by nitely on Saturday October 14, 2017 @04:04PM (#15473954)
I agree. But like always, trade-offs.

> The problem with offloading to a thread is that you can't structurally share data with another thread.

That's really a JS issue, not a general async programming issue, though.

Re:How JavaScript works: Event loop and the rise of Async pr... by richmarr on Saturday October 14, 2017 @05:24PM (#15474396)
> > Does this mean that a web server written in node is running single-threaded?

> Yes.

Just to expand, this (like most things) is a simplification, as I'm sure nitely is just being too brief to explain. It's true for Hello World, and a little further, but real-world web servers in non-trivial contexts typically utilise techniques like clustering, workers, and other ways of delegating tasks to external processes.

Re:How JavaScript works: Event loop and the rise of Async pr... by coldtea on Saturday October 14, 2017 @06:52PM (#15474758)
>Yes, to take advantage of all CPU cores you have to create more server instances. But why would they talk to the same database instance?

Why not? Unless you've exceeded the capacity of a single DB and have a real use for sharding etc, it would not make sense to have difference DBs (+ replication overhead) for different Node processes.

Re:How JavaScript works: Event loop and the rise of Async pr... by nitely on Saturday October 14, 2017 @07:15PM (#15474859)
The parent is asking how comes async I/O can handle higher volumes of requests given it's single threaded and even if it does how comes the database is still not the bottleneck. I answered both of these questions.
Re:How JavaScript works: Event loop and the rise of Async pr... by coldtea on Saturday October 14, 2017 @07:52PM (#15474991)
Yes, and I had an objection with the answer to the second question, that it might leave the impression to the parent that sharding or replication and pooling is required to have good DB IO performance with multiple Node processes -- when in practice it might or might not be an issue.

You can have a 12-processes node cluster and still not need a second db.