Servers

Node.js + PM2 server exhausts ephemeral ports (TIME_WAIT) during traffic bursts causing upstream connection failures and 502s

Environment

Environment

Ubuntu 20.04 VMs running Node.js 14 behind HAProxy, PM2 process manager spawning 4 worker processes, kernel 5.4, sysctl at defaults for net.ipv4.ip_local_port_range and tcp_tw_reuse disabled

Symptoms

Symptoms

Under short high-concurrency bursts (thousands of clients), backend process fails to establish outbound connections to database/other services; HAProxy returns 502 for requests that require outbound calls. netstat shows local ports in TIME_WAIT accumulating and no free ephemeral ports.

Observations

Observations

ss -s shows tens of thousands of TIME_WAIT in the node process PID range. /proc/sys/net/ipv4/ip_local_port_range remains default 32768-60999. Node process logs show 'connect ECONNREFUSED' for outbound connections when ephemeral port exhaustion occurs. After burst subsides, ports free slowly and services recover.

Already attempted

Already attempted

Bumped ulimit -n, added connection pooling in app but still saw TIME_WAIT growth under synthetic load. Restarting PM2 clears TIME_WAIT quickly and restores service. Tried enabling tcp_tw_reuse=1 temporarily on one host with reduced failures.

Question

Question

How can ephemeral port exhaustion from many short outbound connections by Node.js workers be prevented without restarting processes, and what kernel or app-level changes are safest in production?

COMMUNITY RESPONSES

Verified community troubleshooting

0 replies

No answers yet..

Showing 0 of 0 community responses
AI DIAGNOSTICS

Incident analysis

Updated 1mo ago• Trouble ID

Most likely causes

High rate of short-lived outbound TCP connections causing ephemeral port pool exhaustion (TIME_WAIT accumulation) 95%
Application not reusing connections for repeated outbound calls (no pooling for DB/HTTP upstreams) 80%
Too narrow ip_local_port_range configured on host limiting available ephemeral ports 50%

Summary

Node.js workers create many short-lived outbound TCP connections so local ephemeral ports enter TIME_WAIT for the TCP 2MSL. With default port range and high concurrency, the ephemeral pool is exhausted. Temporary tcp_tw_reuse helped but isn't ideal alone. Safe mitigations: increase ephemeral port range, enable reuse-related sysctls with caution, implement connection pooling or reuse at the app layer, and reduce short-lived connect patterns.

Suggested Fixes

Diagnostic step 1
Implement connection pooling in the Node app for outbound services (keep-alive agents for HTTP, pooled DB connections) to avoid frequent connects.
Diagnostic step 2
Increase ephemeral port range: sysctl -w net.ipv4.ip_local_port_range='15000 61000' and persist in /etc/sysctl.conf.
Diagnostic step 3
If safe for your environment, enable tcp_tw_reuse: sysctl -w net.ipv4.tcp_tw_reuse=1 (test carefully; not recommended with NAT requiring strict semantics).

Continue troubleshooting

Matched from the title, description and tags