1

I know what node.js is & what it does.

What I really want to know is how does it work as an http server?

A server like apache gets a request, fork a process, allocate memory to process, handle the request & release the process after it is completed. If we get thousands of http requests at a time, then apache waits for the the process queue to release process.

How does node.js with a single threaded environment handles so many http requests. All the incoming requests are given their own event loop. All these event loops share same memory.

How does using node.js to handle millions of http requests more scalable than using apache?

1
  • All if the answers below presuppose that you're using Apache in prefork mode. That is not currently the standard according to the Apache project. Please don't use apache in the prefork mode. If you don't, then the advantages of using node don't include performance. Commented Mar 28, 2014 at 16:04

3 Answers 3

3

key point would be not forking a new process for each request,

node.js assumes your process is heavily IO bound (reasonable for a webserver with separate DB)

this means that each time you are waiting on the database to return the next result another request is being handled

0

You might find node.js pleasing a webserver because it uses an event loop, much like many UI frameworks. You simply wire up your routes to the http server's events. This is also the secret to its ability to achieve massive concurrency.

When the server gets a request, an event is raised, and your code runs asynchronously as a callback. When your code requests IO, it stops executing and continues as a callback when the IO is done. That way, it can use (and reuse) lightweight worker threads ( similar to the task framework in .NET ) instead of per-request threads or per-request processes.

0

With non-blocking code, you can issue a request for file-system or database, then instead of waiting for the response, you go on and serve another user and then another. Later you come back to first user (whose request was pending) and you serve him.

In summary, this is something like this:

user1 makes a request -> Node issues a call to file-system or database.
user2 makes a request -> Node serves the request immediately as there is no waiting call.
user3 makes a request -> Node serves the request immediately as there is no waiting call.
                         call to filesystem or database completes and Node serves user1.

Your Answer

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