Skip to main content
← PROBLEMS / SYSTEM-DESIGN
An end-to-end domain learning journey

Aligning HTTP Keep-Alive
Timeouts Across Proxies.

When a backend's HTTP keepAliveTimeout (e.g., 60s) is shorter than or equal to the upstream reverse proxy's idle timeout (e.g., AWS ALB 60s or Nginx 65s), the backend closes the socket with FIN at the exact millisecond the proxy reuses it—causing intermittent 502 Bad Gateway / ECONNRESET errors.
Start the journey5 stages
FIRST PRINCIPLES. PRODUCTION SCALE.
The idea to keep with you

When a backend's HTTP `keepAliveTimeout` (e, always set the backend's `keepAliveTimeout` strictly greater than the upstream proxy's idle timeout (e.g., ALB 60s -> Backend 75s), and let the proxy initiate idle closes.

TL;DR30-second simplified summary
▾

Core Mechanics: In a 3-hop checkout chain (AWS ALB idle_timeout = 60s -> Nginx keepalive_timeout = 65s -> Spring Boot Tomcat 60s / Node.js 5s) handling 6,000 RPS across 30 pods, 0.3% of POST /orders calls fail with `502 Bad.

Visual & Structural Flow: Depicts Backend FIN Race (502) vs. Proxy-Initiated Close (AWS ALB (60s) -> Nginx (55s) -> Backend (90s)); Increasing Keep-Alive & Request Limits Downstream (AWS ALB -> Nginx Ingress -> Backend App -> TCP Verify).

Trade-Offs & Production: Disabling HTTP keep-alive (Connection: close) eliminates the race but destroys performance at 6,000 RPS: every request pays a full TCP 3-way handshake + TLS handshake. Enforce a strictly increasing idle-timeout and max-request ladder from edge to origin so the caller always closes first: AWS ALB: idle_timeout = 60s (must be < Nginx downstream keepalive_timeout = 75s).

Failure Modes & Application: Timeout hierarchy alignment is critical across multi-hop cloud topologies: API Gateways & Ingress Controllers: Envoy, Nginx, HAProxy, and AWS ALB/NLB pooling to Go, Node.js, or JVM backends.

Full walkthrough · 5 stages
01
STAGE 01 · WHY IT MATTERSL4 · FUNDAMENTALS

Why HTTP Keep-Alive Timeout Alignment Matters.

Why this matters: The mechanism: understand how Why HTTP Keep-Alive Timeout Alignment Matters works from first principles.

Think of Why HTTP Keep-Alive Timeout Alignment Matters as the intuition layer for Aligning HTTP Keep-Alive Timeouts Across Proxies: The mechanism: understand how Why HTTP Keep-Alive Timeout Alignment Matters works from first principles.

In a 3-hop checkout chain (AWS ALB idle_timeout = 60s -> Nginx keepalive_timeout = 65s -> Spring Boot Tomcat 60s / Node.js 5s) handling 6,000 RPS across 30 pods, 0.3% of POST /orders calls fail with 502 Bad Gateway and ECONNRESET while backend pods show 0 errors and <20% CPU. Because POST /orders is non-idempotent once received by the backend, silent socket drops risk duplicate or lost orders.

02
STAGE 02 · IMPACTL5 · APPLIED DESIGN

Real-World Impact of the TCP FIN / RST Race.

Why this matters: The problem: see what breaks without Real-World Impact of the TCP FIN / RST Race under real concurrency or scale.

When a downstream server closes an idle keep-alive socket at the exact millisecond an upstream proxy writes a new request onto it:

  1. 502 Bad Gateway: Node.js (5s) or Tomcat (60s) sends TCP FIN while Nginx/ALB simultaneously writes POST /orders.
  2. TCP RST Aborts: The backend kernel replies TCP RST to the closed socket; Nginx logs upstream prematurely closed connection and returns 502.
  3. Unsafe Retries: Because the proxy cannot know whether the backend parsed the POST before sending FIN, retrying risks double-charging.
TCP Keep-Alive Close Race

Backend FIN Race (502) vs. Proxy-Initiated Close

AWS ALB (60s)Nginx (55s)Backend (90s)1. Naive (5s): Backend sends TCPFIN2. Simultaneous POST /orders onsocket3. Kernel sends TCP RST -> 502 BadGateway4. Fixed: Nginx (55s) closes idleconn first5. Backend (90s) cleanly ACKs FIN,0 RSTs

Strictly increasing keep-alive timeouts downstream ensures the upstream proxy always initiates idle socket teardown.

Engineering Decision Framework
Problem
How to implement Aligning HTTP Keep-Alive Timeouts Across Proxies without hitting unbounded tail latency, state divergence, or operational fragility.
Naive Solution
Use a single unpartitioned path with unbounded retries and synchronous global coordination.
Why It Fails
Global coordination serializes throughput under load, while unbounded retries amplify transient stalls into full outages.
Alternatives
Compare synchronous strict-consistency paths against bounded asynchronous replication, local caching, or partitioned ownership.
Engineering Decision
When a backend's HTTP keepAliveTimeout (e, always set the backend's keepAliveTimeout strictly greater than the upstream proxy's idle timeout (e.g., ALB 60s -> Backend 75s), and let the proxy initiate idle closes.
Trade-Offs
Gains predictable p99 latency and fault isolation in exchange for explicit bounding, observability, and slightly higher implementation complexity.
Decision Exercise · Make the Call

When Aligning HTTP Keep-Alive Timeouts Across Proxies experiences a sudden 5x traffic spike alongside a slow downstream dependency, which engineering choice best protects system stability?

03
STAGE 03 · NAIVE SOLUTIONL5 · APPLIED DESIGN

Naive Solution (and Why It Breaks).

Why this matters: The problem: see what breaks without Naive Solution under real concurrency or scale.

Think of Naive Solution (and Why It Breaks) as the intuition layer for Aligning HTTP Keep-Alive Timeouts Across Proxies: The problem: see what breaks without Naive Solution under real concurrency or scale.

Disabling HTTP keep-alive (Connection: close) eliminates the race but destroys performance at 6,000 RPS: every request pays a full TCP 3-way handshake + TLS handshake, exhausting ephemeral source ports (TIME_WAIT) and spiking CPU. Setting equal timeouts (e.g., ALB 60s = Tomcat 60s) still races due to clock drift and timer jitter.

Lifecycle & Failure Recovery

Naive Solution (and Why It Breaks)

admitsucceedbudget exceededInitializedProcessingInvariant MetDegraded Fallback
04
STAGE 04 · ENTERPRISE SOLUTIONL6 · SCALE & TRADE-OFFS

Enterprise Solution: Strict Downstream Timeout Hierarchy.

Why this matters: Operational control: configure bounds, failure handling, and safe production defaults.

Enforce a strictly increasing idle-timeout and max-request ladder from edge to origin so the caller always closes first:

  • AWS ALB: idle_timeout = 60s (must be < Nginx downstream keepalive_timeout = 75s).
  • Nginx -> Backend Upstream: Set keepalive_timeout 55s and keepalive_requests 5000 on the Nginx upstream pool.
  • Backend (Tomcat / Node.js): Set server.tomcat.keep-alive-timeout: 90s (server.keepAliveTimeout = 90000, headersTimeout = 95000 in Node.js) and max-keep-alive-requests: 10000 so the backend never closes an active pooled connection first.
  • Verification: Confirm with tcpdump that FIN packets originate from the proxy, not port 8080.
Timeout Ladder Alignment

Increasing Keep-Alive & Request Limits Downstream

AWS ALBidle_timeout: 60sNginx Ingressclient:75s | up:55sBackend AppkeepAlive: 90sTCP VerifyProxy sends FIN55s < 90s

1.Edge -> Nginx: ALB idle_timeout (60s) < Nginx client keepalive_timeout (75s).

2.Nginx -> App: Nginx upstream keepalive_timeout (55s) < Tomcat/Node (90s).

3.Max Requests: Backend max-keep-alive-requests (10,000) > Nginx (5,000).

4.Node.js Pitfall: Node defaults to 5s keepAliveTimeout; must override to 90,000 ms.

Every upstream hop closes idle sockets before the downstream server's timer expires, eliminating 502 ECONNRESET races.

# Spring Boot application.yaml
server:
  tomcat:
    keep-alive-timeout: 90s
    max-keep-alive-requests: 10000

# Node.js equivalent:
# server.keepAliveTimeout = 90000;
# server.headersTimeout = 95000;

# Nginx upstream block (safe)
upstream backend_pool {
  server app-service:8080;
  keepalive 128;
  keepalive_timeout 55s;
  keepalive_requests 5000;
}

# Verify FIN direction on backend pod
# tcpdump -ni eth0 'tcp[tcpflags] & (tcp-fin|tcp-rst) != 0 and port 8080'
05
STAGE 05 · SYSTEM DESIGN APPLICATIONSL7 · STAFF / PRINCIPAL

Where Keep-Alive Alignment Applies in System Design.

Why this matters: Scope boundary: identify where this pattern fits best and where simpler or async alternatives win.

Timeout hierarchy alignment is critical across multi-hop cloud topologies:

  • API Gateways & Ingress Controllers: Envoy, Nginx, HAProxy, and AWS ALB/NLB pooling to Go, Node.js, or JVM backends.
  • Service Mesh Sidecars: Istio/Envoy sidecars must have shorter idle timeouts than application servers.
  • gRPC / HTTP/2 Connection Pools: Aligning MAX_CONNECTION_IDLE and KEEPALIVE_TIME prevents GOAWAY races.
Practice · Explain the consequences

Practice: Make the trade-off yourself.

Question 1 of 3
Score: 0/3

To eliminate `502 Bad Gateway` / `ECONNRESET` races between Nginx and a Node.js backend, how should their HTTP keep-alive timeouts be configured?

Recommended Learning Flow