Nodejs Reverse Proxy Performance

I am exploring the possibility of using Node to work as a reverse proxy. One of the main goals of my project is VERY high productivity. Therefore, I configured the Node server to proxy requests to the target Node server, which will say hello world regardless of the request.

Using Apache Bench I made some comparison of the number of requests processed per second. The proxy server, target audience, and caller are on separate instances of M1 Large in AWS. My results are disappointing and confusing.

Straight from the caller to the target:

ab -c 100 -n 10000 http://target-instance/ 

= ~ 2600 requests per second

From caller through proxy to target

 ab -c 100 -n 10000 http://proxy-instance/ 

= ~ 1100 requests / sec

Using lighttpd, I was able to get ~ 3500 requests / second for proxies and target

I am disappointed that the proxy server is less efficient than the target server. Comparing other products, such as lighttpd, I saw the proxy achieve comparable results with the goal, so I'm confused by the fact that Node (supposedly to quickly illuminate) does not achieve the same.

Here is my proxy code in Node v0.5.9: Am I missing something?

 var server = http.createServer(function(req, res){ var opts = { host: 'target-instance', port: 80, path: '/', method: 'GET'}; var proxyRequest = http.get(opts, function(response){ response.on('data', function(chunk){ res.write(chunk); }); response.on('end', function(){ res.end() }); }); }); server.listen(80); 
+7
source share
4 answers

While Node.js is very efficient, it is not multi-threaded, so the proxy node will handle more connections than the target, but with only one thread, and therefore will become a bottleneck. There are two ways:

  • Use a multi-threaded load balancer in front of multiple instances of your node proxy (e.g. nginx).
  • Change the node proxy to use multiple processes . There are several node modules for this, but node now includes " cluster " out of the box and seems to me to be the simplest method.
+6
source

Try bouncy: https://github.com/substack/bouncy

It has been optimized for very high performance.

+3
source

From the http.request docs:

Sending "Connection: keep-alive" will notify Node that the connection to the server should be maintained until the next request.

So, I'm sure your proxy reconnects to the target instance with each request, which is very inefficient. I think your options variable should look like this to speed it up:

 var opts { host: 'target-instance', port: 80, path: '/', headers: { "Connection": "keep-alive" } }; 
0
source

after adding the connection: keep-alive header, you should also check with keep-alive (-k option):

ab -c 100 -n 10000 -k http: // xxxx /

0
source

All Articles