(More than) One million requests per second in Node.js

This Post explain about ‘One million requests per second in Node.js’.  Also see the performance improvements in this pipelined case and performs 65k requests/second.

    (More than) One million requests per second in Node.js

Take a Look at this Code:

'use strict';

const http = require('http');

const document = Buffer.from('Hello world!');

const server = http.createServer((req, res) => {
    // handle some POST data
    if (req.method === 'POST') {
        var body = [];
        req.on('data', (chunk) => {
            body.push(Buffer.from(chunk));
        }).on('end', () => {
            res.end('You posted me this: ' + Buffer.concat(body).toString());
        });
    // handle some GET url
    } else if (req.url === '/') {
        res.end(document);
    } else {
        res.end('Unknown request by: ' + req.headers['user-agent']);
    }
});

server.listen(3000);

Nothing is too impressive here, A simple example of code that shows some of the features of the Node.js Core HTTP module like receiving POST data, headers, url, method and how you send responses back.

Now, we will simply replace require('http') with require('uws').http[1] and run the same server script, we see that it works as before. Nothing changed in terms of application logic, everything still works as it should.

    Performance comparison

Since every single http server built with Node.js makes use of the Node.js core http module indirectly or directly (via ExpressJS or not), it makes sense to try and make a replacement module that has a similar enough interface to allow seamless transition from slow to fast and from bloated to lightweight in terms of memory usage per connection.

However, it is not just about how fast the server is but also how lightweight its connections are – especially in any long polling / EventSource or any other similar case where you want long held http connections. As http sockets in µWS build on the same networking stack as its websockets, you can easily scale to millions of long polling connections, just like you can with the websockets. This is simply not possible with the built-in Node.js http networking stack as its connections are very heavyweight in comparison.

So back to pure performance, let’s look at a benchmark table:

This benchmark shows requests/second without HTTP pipelining. Node.js vanilla does about 40k here while µWS inside of Node.js does about 220k. Let’s look at the results with HTTP pipelining (made famous by Japronto):

One million requests per second in Node.js

Here we can see that µWS does more than 1 million solicitations/second within Node.js. Node.js’ own particular center http module likewise observes execution enhancements in this pipelined case and performs 65k solicitations/second.

While this is fun and all, µWS v0.13.0 is still in alpha stage and has not yet been discharged to NPM. I’m gaining ground yet I will require some more opportunity to prepare this cleaned and for utilization and criticism. In the event that you need to approve the execution you can investigate the post “WIP: Faster HTTP for Node.js” on the best way to order and run the addon, or perhaps you definitely know how?

[1]: Not distributed on NPM yet, you have to clone the repo, compact disc into nodejs envelope, make on Linux, and change to require(‘./dist/uws’) for the time being. Things will be done up in two or three weeks and discharged to NPM to play with.

source: GitHub.com

Was this article helpful?

Thanks

Thanks for your Feeback. It will helps us to improve our Article quality




  • https://www.linkedin.com/in/kais-ben-salah-9b7a24108/ Kais Ben Salah

    Interesting, thanks for sharing.

17 Shares
+1
Tweet
Share17
Stumble