HTTP Methods (Verbs)
| Method | Purpose | Idempotent | Safe | Body |
|---|---|---|---|---|
| GET | Retrieve resource | Yes | Yes | No |
| POST | Create resource | No | No | Yes |
| PUT | Replace entire resource | Yes | No | Yes |
| PATCH | Partial update | No | No | Yes |
| DELETE | Remove resource | Yes | No | Optional |
Idempotent: multiple identical requests produce the same result. Safe: does not modify the resource (read-only).
Stateless vs Stateful
HTTP is stateless — the server does not retain client information between requests. Each request is independent.
| Stateless | Stateful | |
|---|---|---|
| Server memory | None per client | Stores session data |
| Scalability | Trivial (any server handles any request) | Must route client to same server |
| Examples | HTTP, REST | FTP, TCP connections |
Statelessness is what makes the web scalable. Load balancers can distribute requests to any server in a pool.
WebSockets
WebSocket provides a full-duplex persistent connection over a single TCP socket after an HTTP upgrade handshake.
Client Server
│── HTTP GET (Upgrade: websocket) ──→
│←─ 101 Switching Protocols ──────────
│── bidirectional frames ────────────→
│←───────────────────────────────────
| Feature | HTTP | WebSocket |
|---|---|---|
| Direction | Half-duplex (request-response) | Full-duplex |
| Overhead | Headers per request (500-800 bytes) | 2-6 bytes per frame |
| Persistence | Connection closed after response | Connection stays open |
| Use case | REST APIs, web pages | Real-time apps, chat, games |
Statelessness in Practice
Modern web apps simulate state using:
- Cookies: server sends a token (session ID), client sends it back
- JWT (JSON Web Tokens): self-contained tokens with user info and expiry
- Client-side storage: localStorage, sessionStorage
These shift state management to the client while keeping the server stateless.
Q: Why use WebSockets instead of frequent HTTP requests?
A: Efficiency. HTTP polling requires a new TCP/TLS handshake and large headers each time. WebSockets upgrade once, then exchange tiny frames bidirectionally. For real-time apps (chat, trading, games), WebSockets are far more efficient.
Q: What is the difference between PUT and PATCH?
A: PUT replaces the entire resource. PATCH updates only the specified fields. Sending {"name": "Alice"} via PUT would delete all other fields; via PATCH only updates the name.
Q: Why is HTTP stateless?
A: Scalability. Stateless servers don’t need to remember clients, so any server can handle any request. This makes horizontal scaling trivial — just add more servers behind a load balancer.
Q: How do web apps maintain session if HTTP is stateless?
A: The server sends a signed token (session cookie or JWT) that the client includes with every request. The server validates the token on each request but doesn’t store session data — this keeps the server stateless.
Premium Content
Unlock Modern Web Protocols and all premium lessons with a subscription.
From ₹199.99/year — See plans