Request Lifecycle¶
What is it:
The full journey of an HTTPS API request, from the moment the client calls fetch("https://api.example.com/users") to receiving the response.
Full flow:
-
DNS resolution (0–300ms, cached = 0ms): Browser cache → OS cache →
/etc/hosts→ recursive resolver → root → TLD → authoritative server. Result: IP address ofapi.example.com. Cached for TTL duration. -
TCP connection (~1 RTT = 50ms on a typical internet connection): Client sends SYN → server sends SYN+ACK → client sends ACK. After this, the TCP connection is established. With HTTP keep-alive / connection pooling, existing connections skip this step.
-
TLS handshake (TLS 1.2: ~2 RTT = 100ms; TLS 1.3: ~1 RTT = 50ms; resumed: ~0ms): Cipher negotiation, certificate exchange, key derivation. After this, all bytes are encrypted.
-
HTTP request sent (negligible — just packet sending): Client sends HTTP/1.1 or HTTP/2 request: method, URL, headers (Host, Authorization, Content-Type), optional body.
-
Server processing (1–100ms depending on workload):
- Framework routing: match URL to handler
- Middleware: auth verification, rate limiting, logging
- Business logic: validation, computation
- Database query: most latency is here — a cold query with a B+Tree lookup takes 1–10ms, a slow query without index can take seconds
-
Response serialization: JSON marshaling
-
Response transmitted (function of response size and bandwidth): Server sends status code + headers + body. Client reads, parses JSON (or other format), returns to caller.
Where latency hides: - DNS cache miss: 100–300ms (use long TTLs for stable services) - New TCP connection: 1 RTT (use connection pooling, keep-alive) - TLS handshake: 1–2 RTT (use TLS session resumption, TLS 1.3) - DB query: most often the dominant term (add indexes, cache hot data) - Serialization: large response bodies slow down serialization (return only needed fields) - Geographic distance: a 1 RTT from Vietnam to US = 150–200ms (use CDN/regional servers)
Why a request can be slow: - Missing index on DB query (full table scan) - No connection pooling (new TCP+TLS per request) - Synchronous calls to slow downstream services (should be async or cached) - Large response body (SELECT * instead of specific columns) - DNS TTL too short (frequent DNS lookups) - No CDN for static assets (every request goes to origin)