HTTP, HTTPS, and WebSockets
Most web systems communicate using HTTP or WebSockets. Understanding when to use each is crucial for system design.
1. HTTP (HyperText Transfer Protocol)
HTTP is a request-response protocol. The client sends a request, and the server sends a response.
- Stateless: Each request is independent.
- Methods: GET, POST, PUT, DELETE, PATCH.
- Status Codes: 2xx (Success), 3xx (Redirection), 4xx (Client Error), 5xx (Server Error).
HTTP Versions
- HTTP/1.1: Persistent connections, but suffers from "Head-of-Line Blocking" (one slow request blocks others).
- HTTP/2: Multiplexing (many requests over one connection), Server Push, Header Compression.
- HTTP/3: Uses QUIC (UDP-based) to reduce latency and handle packet loss better.
2. HTTPS (HTTP Secure)
HTTPS is HTTP over TLS/SSL. It provides:
- Encryption: Protects data from eavesdropping.
- Integrity: Prevents tampering.
- Authentication: Verifies the identity of the server.
3. WebSockets
Unlike HTTP, WebSockets provide a full-duplex, persistent connection between client and server.
- Real-time: Server can push data to the client without the client asking.
- Low Overhead: Once the connection is established (via an HTTP handshake), there are no HTTP headers sent with every message.
- Use Cases: Chat applications, live sports scores, collaborative editing, stock tickers.
| Feature | HTTP | WebSockets |
|---|---|---|
| Type | Request-Response | Full-Duplex |
| Connection | Short-lived (mostly) | Long-lived (Persistent) |
| Communication | Unidirectional (Client -> Server) | Bidirectional |
| Overhead | High (Headers per request) | Low (Minimal framing) |
Knowledge Check
Question 1 of 3
Q1Single choice
Which HTTP status code category indicates a Server Error?