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.
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.
Singleton Bounded Connection Pools.
Instantiate HTTP/gRPC clients once per process with bounded max_connections and jittered keepalive_expiry.
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)
)
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.
- 28,232 ephemeral ports exhausted at 30k RPS
- +4-10 ms TCP/TLS handshake per request
- NAT packet drops if tcp_tw_recycle enabled
- 200 persistent sockets (0.71% port usage)
- 0 ms warm handshake overhead
- NAT-safe egress without synchronized FIN storms
- 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.
Why does calling 'with httpx.Client() as client:' inside a FastAPI/Flask route handler crash under load?
| Choice | What it buys | What it costs |
|---|---|---|
| New Client per RPC | Simple per-request isolation NAT safe | 28,232 ports exhausted in 0.94s at 30k RPS +4-10 ms TCP/TLS handshake tax |
| Singleton Keep-Alive / HTTP/2 Pool | Only 200 ports (0.71% of ephemeral range) at 30k RPS 0 ms warm handshake latency NAT safe without tcp_tw_recycle | Requires bounded pool sizing and jittered keepalive_expiry |
Practice: Make the trade-off yourself.
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?