Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event Loop vs Multithread blocking IO

Tags:

I was reading a comment about server architecture.

http://news.ycombinator.com/item?id=520077

In this comment, the person says 3 things:

  1. The event loop, time and again, has been shown to truly shine for a high number of low activity connections.
  2. In comparison, a blocking IO model with threads or processes has been shown, time and again, to cut down latency on a per-request basis compared to an event loop.
  3. On a lightly loaded system the difference is indistinguishable. Under load, most event loops choose to slow down, most blocking models choose to shed load.

Are any of these true?

And also another article here titled "Why Events Are A Bad Idea (for High-concurrency Servers)"

http://www.usenix.org/events/hotos03/tech/vonbehren.html

like image 455
Unknown Avatar asked Jun 04 '09 22:06

Unknown


People also ask

Is the event loop multi-threaded?

It's single threaded, so it doesn't need to spawn a new thread for each new incoming connection. Behind the event-loop (which is in fact single threaded), there is a "Non blocking Worker". This thing is not single threaded anymore, so (as far as I understood) it can spawn a new thread for each task.

How is event loop non blocking?

NodeJS Event Loop allows NodeJS to perform non-blocking operations by offloading operation to the system kernel whenever possible. Most modern kernels are multi-threaded and they can perform multiple operations in the background. When one of these operations completes, the kernel tells NodeJS.

Is event loop single threaded?

Event Loop uses Single Thread only. It is main heart of Node JS Platform Processing Model. Even Loop checks any Client Request is placed in Event Queue.

What benefits if any do such asynchronous I O calls offer over multiple threads?

The main reason to use AIO is for scalability. When viewed in the context of a few threads, the benefits are not obvious. But when the system scales to 1000s of threads, AIO will offer much better performance. The caveat is that AIO library should not introduce further bottlenecks.


1 Answers

Typically, if the application is expected to handle million of connections, you can combine multi-threaded paradigm with event-based.

  1. First, spawn as N threads where N == number of cores/processors on your machine. Each thread will have a list of asynchronous sockets that it's supposed to handle.
  2. Then, for each new connection from the acceptor, "load-balance" the new socket to the thread with the fewest socket.
  3. Within each thread, use event-based model for all the sockets, so that each thread can actually handle multiple sockets "simultaneously."

With this approach,

  1. You never spawn a million threads. You just have as many as as your system can handle.
  2. You utilize event-based on multicore as opposed to a single core.
like image 131
sivabudh Avatar answered Oct 13 '22 00:10

sivabudh