Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling multiple parallel HTTP requests in Node.js

Tags:

I know that Node is non-blocking, but I just realized that the default behaviour of http.listen(8000) means that all HTTP requests are handled one-at-a-time. I know I shouldn't have been surprised at this (it's how ports work), but it does make me seriously wonder how to write my code so that I can handle multiple, parallel HTTP requests.

So what's the best way to write a server so that it doesn't hog port 80 and long-running responses don't result in long request queues?

To illustrate the problem, try running the code below and loading it up in two browser tabs at the same time.

var http = require('http');
http.createServer(function (req, res) {
    res.setHeader('Content-Type', 'text/html; charset=utf-8');
    res.write("<p>" + new Date().toString() + ": starting response");
    setTimeout(function () {
        res.write("<p>" + new Date().toString() + ": completing response and closing connection</p>");
        res.end();
    }, 4000);
}).listen(8080);
like image 515
Andrew Avatar asked Jun 16 '13 02:06

Andrew


People also ask

How many concurrent HTTP requests can NodeJS handle?

JS can handle 10,000 concurrent requests they are essentially non-blocking requests i.e. these requests are majorly pertaining to database query.

How do I handle multiple requests at once in NodeJS?

How NodeJS handle multiple client requests? NodeJS receives multiple client requests and places them into EventQueue. NodeJS is built with the concept of event-driven architecture. NodeJS has its own EventLoop which is an infinite loop that receives requests and processes them.

Does NodeJS support parallelism?

Node can support "Parallelism" via either the Cluster or child_process modules packaged in the Nodejs Core API. Both of these modules create additional processes and not additional threads.


1 Answers

You are misunderstanding how node works. The above code can accept TCP connections from hundreds or thousands of clients, read the HTTP requests, and then wait the 4000 ms timeout you have baked in there, and then send the responses. Each client will get a response in about 4000 + a small number of milliseconds. During that setTimeout (and during any I/O operation) node can continue processing. This includes accepting additional TCP connections. I tested your code and the browsers each get a response in 4s. The second one does NOT take 8s, if that is how you think it works.

I ran curl -s localhost:8080 in 4 tabs as quickly as I can via the keyboard and the seconds in the timestamps are:

  1. 54 to 58
  2. 54 to 58
  3. 55 to 59
  4. 56 to 00

There's no issue here, although I can understand how you might think there is one. Node would be totally broken if it worked as your post suggested.

Here's another way to verify:

for i in 1 2 3 4 5 6 7 8 9 10; do curl -s localhost:8080 &;done                                                                                                                                                                       
like image 142
Peter Lyons Avatar answered Oct 09 '22 20:10

Peter Lyons