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

Yes -- node is by default a single threaded, single process server.

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

Not any more than this is the case with Python, PHP, Rails, etc -- which also don't do multi-threaded (or don't do it well and not by default), and which on top of that don't have asynchronous capabilities (again, not by default) and are even less performant than a single Node app.

Which is why a simple Node running with its single process and single threaded execution can e.g. beat a Python server with two dozens of workers (e.g. gunicorn) in handling simultaneous connections (assuming Node code is properly async in the most part).

>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?

Databases take care of serialization of multiple queries for you -- and for more complex cases (with or without transactions for fuller control).

Re:How JavaScript works: Event loop and the rise of Async pr... by corford on Sunday October 15, 2017 @11:39AM (#15477584)
>Not any more than this is the case with Python, PHP, Rails, etc -- which also don't do multi-threaded (or don't do it well and not by default), and which on top of that don't have asynchronous capabilities (again, not by default) and are even less performant than a single Node app.

uWSGI makes running a threaded or multi-proc python webapp trivially easy (and as of Python v3.6 async comes as standard)...

>Which is why a simple Node running with its single process and single threaded execution can e.g. beat a Python server with two dozens of workers (e.g. gunicorn) in handling simultaneous connections (assuming Node code is properly async in the most part).

Node can beat a threaded python app for sheer volume of concurrent connections to clients, yes. But for a lot of traditional backend work (e.g. talking to a DB) async is no faster (indeed it's often slower) than a threaded approach.

Node (or async in general) is great for terminating inbound client connections; talking to local, in-memory caches or making backend calls to remote, non-local REST services.

For making local DB connections or doing any CPU work (e.g. parsing XML docs returned from an API) single-threaded async rarely yields better performance over threads/multi-proc. A good illustration of this is pgbouncer (async on the client facing end; threaded - i think? - on the db facing side).

Basically, all node really does is reduce the number of front end app servers you need to serve X incoming client connections. Just because node can handle a high concurrent connection count doesn't mean the rest of your backend services can. Regardless of whether connections originate from a single node instance or a large fleet of php/python/rails servers; you still need reverse proxies like pgbouncer/haproxy/twemproxy/squid to manage and shape those connections before they get to things like your DB or internal micro-service APIs.

Because node is single-threaded you also need to keep a very close eye on any CPU bound activity to avoid blocking all your connections. This is not always obvious and can crop up in unexpected ways (see: https://news.ycombinator.com/item?id=15477419)

Re:How JavaScript works: Event loop and the rise of Async pr... by Kiro on Sunday October 15, 2017 @05:15AM (#15476463)
> Not any more than this is the case with Python, PHP, Rails, etc -- which also don't do multi-threaded

Why isn't PHP etc called single-threaded then? I thought the difference was in fact that PHP fires up new threads for each connection.

Re:How JavaScript works: Event loop and the rise of Async pr... by tyingq on Sunday October 15, 2017 @08:36AM (#15476858)
>I thought the difference was in fact that PHP fires up new threads for each connection.

PHP doesn't get to decide what happens for each new connection. That's determined upstream, and there's a lot of different pieces of software people choose to do that. Could be directly a webserver like apache's mod_php, or something like fastcgi, php-fpm, etc.

All of those front-ends chose to implement a model of a php process pool. Multiple processes, each with a single php interpreter running. Incoming connections are sent to a process in the pool. So, if the pool is 5 processes, and you get a 6th concurrent connection, that one waits.

However, you can write a C program with a single process and distinct php interpreter per thread. One example: https://github.com/basvanbeek/embed2-sapi

To date, nobody has taken something like that and done the work to connect it to a front end handler.