Course contentsShow
← Node.js
Lesson 516 of 1,9806. Networking and HTTP FundamentalsFree lesson

TCP Backlog and Connection Queues

Optimize server connection limits by tuning the backlog queue size for high-traffic scenarios.

In this lesson, you will learn how to optimize your server’s availability under heavy traffic by managing the TCP backlog queue.

The Waiting Room Concept

When a client attempts to connect to your Node.js server, they initiate a three-way TCP handshake. However, your server process is often busy finishing a previous request or executing code. If a new connection arrives while your server is "occupied," the OS doesn't just drop it immediately. Instead, it places that connection into a backlog queue.

Think of this queue as a waiting room outside your server's front door. If the queue is full and a new connection request arrives, the server will ignore the request (or send a connection reset), resulting in a "Connection Refused" error for the user.

Tuning the Backlog in Node.js

In Node.js, the server.listen() method allows you to configure this queue size via the backlog option. If you are building a high-traffic service, the default value (usually 511) might be too small, causing your server to reject users during traffic spikes.

const net = require('net');

const server = net.createServer((socket) => {
  socket.end('Connected!\n');
});

// Setting the backlog to 1024 to accommodate more pending connections
server.listen({
  port: 8080,
  backlog: 1024 
});

Why Increase the Backlog?

If your server experiences sudden bursts of traffic, a larger backlog acts as a buffer. It holds the connections that have completed their TCP handshake but haven't yet been picked up by your Node.js application's event loop.

However, be careful: simply setting an arbitrarily large number isn't a cure-all. If your server is consistently unable to process requests fast enough to clear the backlog, increasing the queue size only delays the inevitable "Connection Refused" error and may consume more system memory. The true goal is to ensure your queue is large enough to handle temporary spikes without dropping legitimate users while you work on optimizing your application's processing speed.

Key Takeaway: The TCP backlog is your server’s shock absorber; tuning it ensures your application can queue incoming connection requests during traffic spikes before the OS is forced to reject them.