Remote HTTP Request
Node.js is good for making remote http request within the node.js (web) server because of the async characteristic. In theory, you can make as many remote request as you want until max out socket or CPU. In practice, there are boundaries. More often people face problem, especially connection problem, way below expectation (like socket hang up very often ..etc) Here are some of the ways you can increase the throughput:
- Socket hangup
- Linux error: CONNRESET
- Too less throughput (less than a hundred request per seconds)
- Too many slow response
Usually, the reasons lie in two places. your server and remote server.
For remote server, basically nothing you can do. It is their capacity problem. Slow response may also be from remote server, or the network problem. That's usually beyond your control.
For your own server, there are few things you have to check & increase:
- Check ulimit, which is the # of open file allow if you are in LInux system. Increase ulimit by editing :
$ vi /etc/security/limits.conf
and put this:
httpd soft nofile 10240
httpd hard nofile 10240
or any number. I have even put 60000 before.
- In your node.js code, use the http.Agent to set the http.options. The http.Agent should have maxSockets a bit less than the number you set in limits.conf
- You may need to increase the heap and optimize your node server. see this: http://blog.caustik.com/2012/04/11/escape-the-1-4gb-v8-heap-limit-in-node-js/
$ sudo node –max-old-space-size=1700 myNode.js
- When running your code again, check netstat and count if the socket increase and goes back to default level when server stop sending out requests.
- If you are sending to a https server, make sure you store the cert locally and not reading the file everytime the request is made.
- Also, you may consider to run node in production mode:
$ sudo NODE_ENV=production node myNode.js