Theory
How HTTP Works: The Request-Response Lifecycle
📋 Overview
HTTP (HyperText Transfer Protocol) is the foundational protocol of the World Wide Web. It is a stateless, application-layer protocol that facilitates communication between clients (browsers) and servers. Understanding HTTP involves tracing a message from a high-level string of text down to physical signals (light/radio) and back.
🏗️ Core Principles & Characteristics
- Request/Response Model: Every interaction starts with a client request and ends with a server response.
- Statelessness: By default, each request is independent. State is managed via external mechanisms like Cookies or JWTs.
- TCP/IP Foundation: HTTP traditionally sits on top of TCP (for reliability/ordering) and IP (for routing).
- Methods & Status Codes: Standardized verbs (
GET,POST,PUT,DELETE) and numerical responses (200 OK,404 Not Found,500 Error) define the intent and outcome.
⚖️ Trade-offs: Pros & Cons
- Pros:
- Simplicity: Human-readable text format makes debugging easy.
- Extensibility: Headers allow for custom metadata (auth, caching, compression).
- Ubiquity: Supported by every programming language and device.
- Cons:
- Overhead: Large text headers can be inefficient for small payloads (partially fixed by HTTP/2 Binary Framing).
- Latency: The 3-way TCP handshake + TLS handshake adds significant "Time to First Byte" (TTFB).
- HoL Blocking: In HTTP/1.1 and HTTP/2, a single lost packet can stall the entire stream.
🌍 Real-World Implementation
- The Journey of a Packet:
- Browser: Generates text request.
- OS: Wraps text in TCP segments.
- Network Card: Converts segments into Bits (0s and 1s).
- Physical Medium: Bits become Radio Waves (Wi-Fi) or Light Pulses (Fiber).
- Internet Backbone: Routers use BGP and IP tables to hop the packet to the destination.
- Server: Reassembles the segments and parses the HTTP text.
💡 Interview "Gotchas" & Tips
- HTTP/2 vs. HTTP/3: Know the difference. HTTP/2 uses Multiplexing over TCP. HTTP/3 uses QUIC over UDP to eliminate Head-of-Line blocking.
- TCP 3-Way Handshake: Be ready to explain
SYN->SYN-ACK->ACK. - HTTPS is not a separate protocol: It is HTTP inside a TLS (Transport Layer Security) tunnel.
- Persistent Connections (Keep-Alive): Explain how HTTP/1.1 reuses the same TCP connection for multiple requests to avoid handshake overhead.
📐 Suggested Architecture Primitives
- Client (Browser/App): The initiator.
- DNS Resolver: Translates domain names to IP addresses.
- Load Balancer/Reverse Proxy: Terminates TLS and forwards requests.
- API Gateway: Handles routing, auth, and rate limiting.
- Application Server: Processes the logic and returns the response.
Canvas