Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread in an event-driven vs non-event driven web server

Tags:

node.js

The following two diagrams are my understanding on how threads work in a event-driven web server (like Node.js + JavaScript) compared to a non-event driven web server (like IIS + C#)

Traditional (non-event driven) Web Server

enter image description here

From the diagram is easy to tell that on a traditional web server the number of threads used to perform 3 long running operations is larger than on a event-driven web server (3 vs 1.)

I think I got the "traditional web server" counts correct (3) but I wonder about the event-driven one (1). Here are my questions:

  1. Is it correct to assume that only one thread was used in the event-driven scenario? That can't be correct, something must have been created to handle the I/O tasks. Right?

  2. How did the evented server handled the I/O? Let's say that the I/O was to read from a database. I suspect that the web server had to create a thread to hand off the job of connecting to the database? Right?

  3. If the event-driven web server indeed created threads to handle the I/O where is the gain?

  4. A possible explanation for my confusion could be that on both scenarios, traditional and event-driven, three separate threads were indeed created to handle the I/O (not shown in the pictures) but the difference is really on the number of threads on the web server per-se, not on the I/O threads. Is that accurate?

like image 437
Hector Correa Avatar asked Jan 07 '13 03:01

Hector Correa


People also ask

Is event-driven programming multithreaded?

Event-driven programming requires less memory than multi-threaded programming because there are no threads that require stack memory. The entire system can run as a single thread, which requires only one single stack.

What is thread in Web server?

The term Threads in this context refers to the Primary Server Job that handles client requests/connections. This is the job with PGM-QZSRHTTP under the Function column in WRKACTJOB. IBM delivers the DG1 product with a default value of 40 in the HTTP Attributes (CHGHTTPA).

Is event loop a thread?

Event Loop — Means single threaded infinite cycle which is making one task at a time and it's not only making single task queue, but it is also prioritizing tasks, because with event loop you have only one resource for execution (1 thread) so for executing some tasks right away you need prioritizing tasks.

What is multithreaded server architecture?

A multithreaded server is any server that has more than one thread. Because a transport requires its own thread, multithreaded servers also have multiple transports. The number of thread-transport pairs that a server contains defines the number of requests that the server can handle in parallel.


2 Answers

The best way to picture NodeJS is like a furious squirrel (i.e. your thread) running in a wheel with an infinite number of pigeons (your I/O) available to pass messages around.

I/O in node is "free". Your squirrel works to set up the connection and send the pigeon off, then can go on to do other things while the pigeon retrieves the data, only dealing with the data when the pigeon returns.

If you write bad code, you can end up having the squirrel waiting for each pigeon.

So always write non-blocking i/o code.

If you can encourage your Pigeons to promise to come back ;)

Promises and generators are probably the best approach you can take to this.

HOWEVER you can always use Node cluster to establish a master squirrel that will procreate child squirrels based on the number of CPUs the master squirrel can find to dole out the work.

Hope this helps and note the complete lack of a car analogy.

like image 175
awjr Avatar answered Sep 22 '22 22:09

awjr


  1. Node may use threads for IO. The JS code runs in a single thread, but all the IO requests are running in parallel threads. If you want some JS code to run in parallel threads, use thread-a-gogo or some other packages out there which mitigate that behaviour.

  2. Same as 1., threads are created by Node for IO operations.

  3. You don't have to handle threading, unless you want to. Easier to develop. At least that's my point of view.

  4. A node application can be coded to run like another web server. Typically, JS code runs in a single thread, but there are ways to make it behave differently.

Personally, I recommend threads-a-gogo (the package name isn't that revealing, but it is easy to use) if you want to experiment with threads. It's faster. Node also supports multiple processes, you may run a completely separate process if you also want to try that out.

like image 33
randunel Avatar answered Sep 24 '22 22:09

randunel