Skip to main content
← CONCEPTS / DISTRIBUTED-SYSTEMS-NETWORKING
An end-to-end domain learning journey

TCP TIME_WAIT &
Ephemeral Port Multiplexing.

Short-lived outbound TCP connections enter 60-second TIME_WAIT on close, exhausting Linux's 28,232 ephemeral ports in 0.94 seconds at 30,000 RPS.
Start the journey3 sections· Last modified Nov 29, 2026
FIRST PRINCIPLES. PRODUCTION SCALE.
The idea to keep with you

When fixing EADDRNOTAVAIL in microservices, convert per-request HTTP clients into bounded singleton pools with idle-expiry jitter.

TL;DR30-second simplified summary
▾

200 persistent connections serve 30,000 RPS using 0.71% of ephemeral ports.

Before you begin · Building blocksAlready know these? Jump to Stage 01 ↓
TCP 4-tuple (src_ip, src_port, dst_ip, dst_port)
Core prerequisite: TCP 4-tuple (src_ip, src_port, dst_ip, dst_port).
TCP FIN teardown and 2*MSL TIME_WAIT state
Core prerequisite: TCP FIN teardown and 2*MSL TIME_WAIT state.
Full walkthrough · 3 stages
01
STAGE 01L4 · FUNDAMENTALS

Why TIME_WAIT Exists (And Why 28,232 Ports Run Out).

At 30,000 new connections/sec, 28,232 ephemeral ports saturate in 0.94 seconds and remain locked for 60 seconds.

Before · Naive
After · Hardened
02
STAGE 02L4 · FUNDAMENTALS

Singleton Bounded Connection Pools.

Instantiate HTTP/gRPC clients once per process with bounded max_connections and jittered keepalive_expiry.

Worker Threads (30kRPS)Bounded Pool (200Conns)Downstream Server1. Borrow warm socket (0mshandshake)2. Send HTTP/2 or Keep-Aliverequest3. Return response; socket staysESTABLISHED4. Return socket for immediatereuse
import httpx
client = httpx.Client(
    http2=True,
    limits=httpx.Limits(max_connections=200, max_keepalive_connections=200, keepalive_expiry=45.0),
    timeout=httpx.Timeout(2.0, connect=0.25)
)
03
STAGE 03L5 · APPLIED DESIGN

Why tcp_tw_recycle Is Dangerous & Idle Jitter.

Never enable removed sysctl tcp_tw_recycle (drops packets behind NAT); use HTTP/2 multiplexing and jittered idle expiry. [INSERT YOUR BENCHMARK / PROD NOTE] for handshake savings.

Before · Per-RPC Client / tcp_tw_recycle
  • 28,232 ephemeral ports exhausted at 30k RPS
  • +4-10 ms TCP/TLS handshake per request
  • NAT packet drops if tcp_tw_recycle enabled
After · Singleton HTTP/2 Pool + Jittered Idle Expiry
  • 200 persistent sockets (0.71% port usage)
  • 0 ms warm handshake overhead
  • NAT-safe egress without synchronized FIN storms
Engineering Decision Framework
Problem
High-QPS outbound HTTP/gRPC calls (e.g., 30,000 RPS) exhaust Linux's 28,232 ephemeral ports in ~0.94 seconds and add 4-10 ms of TCP/TLS handshake latency per request.
Naive Solution
Instantiate and close a new HTTP/gRPC client per request, or enable Linux sysctl tcp_tw_recycle to reclaim TIME_WAIT sockets early.
Why It Fails
Per-request clients push every closed 4-tuple into 60-second TIME_WAIT until EADDRNOTAVAIL occurs, while tcp_tw_recycle (removed in Linux 4.12) breaks PAWS timestamp checks and drops legitimate packets behind NAT.
Alternatives
Expand ip_local_port_range or add destination IPs (delays exhaustion without fixing handshake tax), or use a singleton bounded Keep-Alive / HTTP/2 connection pool with jittered keepalive_expiry.
Engineering Decision
Use a singleton bounded Keep-Alive or HTTP/2 connection pool for all high-throughput outbound microservice, REST, gRPC, and database clients; reserve unpooled clients only for one-off CLI scripts making <5 requests.
Trade-Offs
Requires tuning max_connections and jittered keepalive_expiry to prevent synchronized FIN storms and stale idle sockets, in exchange for <1% ephemeral port usage, 0 ms warm handshake latency, and NAT-safe egress.
Decision Exercise · Make the Call

Why does calling 'with httpx.Client() as client:' inside a FastAPI/Flask route handler crash under load?

ChoiceWhat it buysWhat it costs
New Client per RPCSimple per-request isolation NAT safe28,232 ports exhausted in 0.94s at 30k RPS +4-10 ms TCP/TLS handshake tax
Singleton Keep-Alive / HTTP/2 PoolOnly 200 ports (0.71% of ephemeral range) at 30k RPS 0 ms warm handshake latency NAT safe without tcp_tw_recycleRequires bounded pool sizing and jittered keepalive_expiry
Practice · Explain the consequences

Practice: Make the trade-off yourself.

Question 1 of 2
Score: 0/2

At 30,000 new outbound TCP connections/sec with default Linux ip_local_port_range (28,232 ports) and 60s TIME_WAIT, how fast do ports exhaust?

Recommended Learning Flow