0

From what I understand, Node.js has a main thread for the event loop which handles all incoming requests and does IO asynchronously, while traditional multi-threaded web servers handle each incoming request with a separate thread (does everything asynchronously if seen from the main thread's point of view).

In terms of IO, since IO operations are handled asynchronously in both cases, I don't quite see how Node.js can give a performance boost here.

In terms of CPU, isn't Node.js just trading responsiveness for better memory usage? For an extreme example, suppose there's no IO, then Node.js just sums up several threads' work into one.

I see one major advantage of Node.js is to hide multi-thread programming details behind the framework and simplify programmer's work. But could anyone please help explain what the performance advantage is?

Help appreciated.

5
  • 1
    I think this would make more sense for you to read a bunch of articles in this Google search and then ask more specific questions about the things you don't understand as a LOT has already been written on this topic. Commented Nov 28, 2015 at 8:22
  • Short answer: spawning threads is not cheap, takes resources, and adds overhead for communication across the whole server. Besides, agreed with jfriend00: it's not a good SO question. Commented Nov 28, 2015 at 8:23
  • @jfriend00 I read more than two dozens and really hoped someone could write more details about it's performance advantage rather than event-driven, single thread and developer happiness. Commented Nov 28, 2015 at 8:31
  • @Touffy If you don't use threads or processes then all computation is sequential, which is why I want to know how responsive that'd be. Threads are much cheaper than processes. How much overhead does it have in a practical web server deployment? Commented Nov 28, 2015 at 8:34
  • @Touffy Some web servers even use multiple processes to handle requests for fault isolation. If the overhead of forking a new process is OK, shouldn't it be much better by spawning a thread anyway? Commented Nov 28, 2015 at 8:36

1 Answer 1

2

Node.js of course requires that you design your code in such a way that it won't do intensive computations in the main thread. You don't have auto threads for each request but you can explicitly spawn them if it makes sense.

The idea is that typically, responding to a request is very light computationally, and so it is possible for a single thread to handle hundreds of thousands of simultaneous clients, by letting the server handle other requests while any lengthy process required for a request is being done asynchronously.

Node.js (and nginx which uses the same single-threaded design) are faster and use less resources than thread-based servers because they're optimized for those typical requests. They remain fast anyway when you do need threads, but the point is, usually you don't, and nginx is an even better example, optimized for serving static resources without any computation at all.

Moreover, Node.js apps use the same JavaScript context to handle every request, which, in some applications, saves a huge amount of overhead when you do perform computations.

Sign up to request clarification or add additional context in comments.

3 Comments

Nice. It looks like typically serving a request is not a performance bottleneck at all. And by using this framework the programmer has freedom to decide whether and when to spawn a new thread (while traditional multi-threaded servers always do it at the very beginning).
As you mentioned typical processing time, it'd be a greater answer if you can also shed some light upon the resource usage when handling requests in serial and in parallel (e.g. how much memory or any other type of resource it can save when serving 10k pages).
I don't have experience personally with serving huge amounts of concurrent clients (your search results are as good as mine), but that's where node.js is supposed to shine, since in the best case it can do it with close to zero extra memory (just an object that represents the request) and minimal CPU usage. Switching from handling one response to another is free, they're all in the same context.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.