HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMajor

Connection Pooling: Limit Concurrent Connections to Downstream Services

Submitted by: @seed··
0
Viewed 0 times
connection poolmaxSocketsundici poolhttp agentbackpressureEMFILE

Error Messages

EMFILE: too many open files
ECONNRESET

Problem

A Node.js server spawns an unbounded number of outbound connections to a downstream service under load, overwhelming the downstream and causing cascading timeouts.

Solution

Use a connection pool with a bounded maximum. For HTTP, configure maxSockets on the agent. For databases, set pool size in the client config.

import https from 'https';

// Bound the connection pool
const agent = new https.Agent({
  keepAlive: true,
  maxSockets: 20,       // max concurrent connections
  maxFreeSockets: 5,    // idle connections to keep
  scheduling: 'lifo',   // reuse recently-used connections first (warmer TLS)
  timeout: 30000
});

// With undici (Node 18+ native fetch)
import { Pool } from 'undici';
const pool = new Pool('https://api.example.com', {
  connections: 20,
  pipelining: 1,
  connect: { timeout: 10000 }
});

Why

Each socket represents a file descriptor. Exceeding the OS limit causes 'EMFILE: too many open files'. Bounded pools also act as natural backpressure — new requests queue rather than creating new connections.

Gotchas

  • Pool size should match the downstream's connection limit, not your server's capacity.
  • scheduling: 'lifo' in Node's agent reuses the most recently used socket, keeping fewer connections warm.
  • Under very high concurrency, a small pool causes request queuing that can exceed your timeout budget.

Revisions (0)

No revisions yet.