Search code examples
node.jshttpserver

In a Node.js HTTP server's listen() method, what is the backlog parameter?


See the backlog parameter mentioned here:

All listen() methods can take a backlog parameter to specify the maximum length of the queue of pending connections. The actual length will be determined by the OS through sysctl settings such as tcp_max_syn_backlog and somaxconn on Linux. The default value of this parameter is 511 (not 512).

A more thorough explanation of this parameter would be appreciated. Some questions that come to mind after reading that description:

  • When is a connection 'pending' vs accepted?
  • How do you max out this backlog limitation?
  • What happens when the backlog parameter is exceeded, is an error returned?

Solution

  • This parameter will help you throttle connections to your server and, hence, protect it from "too many connections" issues.

    Without this parameter, your server will (theoretically speaking and without OS limitations) accepts any number of connections. And since each connection consumes memory from your underlying machine, you will get to an "out of memory" situation when there are too many connections to your server and your server will stop running or, at least, will behaving in unexpected manner.

    Receiving too many connections can be either normal (too many valid users trying to use your server) or abnormal (a result of a DOS/DDOS attack). In all cases, it is a good practice to put a limit to the number of connections your server can handle to ensure a high quality of service (connections above the limit will be "polity" declined).

    Hopefully, Linux put some system-wide limitations that are tcp_max_syn_backlog and somaxconn. Even if you set the backlog parameter to a high value, Linux will review it to align it with the value set for these two system parameters. You can read more about this feature in this post.