Node.js Node.js Performance Increase maxSockets


Example

Basics

require('http').globalAgent.maxSockets = 25

// You can change 25 to Infinity or to a different value by experimenting

Node.js by default is using maxSockets = Infinity at the same time (since v0.12.0). Until Node v0.12.0, the default was maxSockets = 5 (see v0.11.0). So, after more than 5 requests they will be queued. If you want concurrency, increase this number.

Setting your own agent

http API is using a "Global Agent". You can supply your own agent. Like this:

const http = require('http')
const myGloriousAgent = new http.Agent({ keepAlive: true })
myGloriousAgent.maxSockets = Infinity

http.request({ ..., agent: myGloriousAgent }, ...)

Turning off Socket Pooling entirely

const http = require('http')
const options = {.....}

options.agent = false

const request = http.request(options)

Pitfalls

  • You should do the same thing for https API if you want the same effects

  • Beware that, for example, AWS will use 50 instead of Infinity.