What is HTTP?
HTTP (Hypertext Transfer Protocol) is the application-layer protocol that powers all communication on the World Wide Web. It defines how messages are formatted, transmitted, and how web servers and browsers should respond to commands.
How HTTP Works
Client (Browser) Server
| |
| 1. Send HTTP Request |
|---------------------------------->|
| |
| 2. Process Request |
| |
| 3. Send HTTP Response |
|<----------------------------------|
| |
Key Characteristics of HTTP
- Stateless — Each request is independent; the server doesn't remember previous requests
- Client-Server — Client initiates requests, server responds
- Media Independent — Can transfer any type of data (HTML, JSON, images, etc.)
- Connectionless (HTTP/1.0) — Connection closes after each request-response (improved in later versions)
HTTP Methods
HTTP defines several methods (verbs) that indicate the desired action:
| Method | Purpose | Has Body | Idempotent |
|---|---|---|---|
| GET | Read a resource | No | Yes |
| POST | Create a new resource | Yes | No |
| PUT | Replace a resource | Yes | Yes |
| PATCH | Partial update | Yes | No |
| DELETE | Remove a resource | Yes | Yes |
HTTP Status Codes
Servers respond with status codes indicating the result:
1xx: Informational
2xx: Success → 200 OK, 201 Created
3xx: Redirection → 301 Moved, 304 Not Modified
4xx: Client Error → 400 Bad Request, 404 Not Found
5xx: Server Error → 500 Internal Error, 503 Unavailable
HTTP Versions
| Version | Year | Key Feature |
|---|---|---|
| HTTP/0.9 | 1991 | No headers, GET only |
| HTTP/1.0 | 1996 | Headers, status codes, Content-Type |
| HTTP/1.1 | 1997 | Persistent connections, chunked transfer |
| HTTP/2 | 2015 | Multiplexing, header compression, server push |
| HTTP/3 | 2022 | QUIC protocol, UDP-based |
Why HTTP Matters for Backend
At Amazon, every API call you build uses HTTP. Understanding HTTP deeply helps you:
- Design RESTful APIs correctly
- Debug client-server communication
- Optimize performance with caching and compression
- Implement security with HTTPS and CORS
HTTP is the lingua franca of backend development.
HTTP Request Lifecycle
Complete Request Lifecycle
When you type a URL in your browser, here's what happens:
1. URL Parsing
https://api.example.com:443/users/123?active=true
___________________/ ____/ ______/ _____/ ______/
Host Port Path Query Fragment
2. DNS Resolution
api.example.com → 93.184.216.34
3. TCP Connection
Client:SYN → Server:SYN-ACK → Client:ACK
(3-way handshake)
4. TLS Handshake (for HTTPS)
Exchange certificates, agree on cipher suite
5. Send HTTP Request
GET /users/123?active=true HTTP/1.1
Host: api.example.com
Authorization: Bearer <token>
Accept: application/json
6. Server Processing
- Parse request
- Route to handler
- Execute business logic
- Query database
- Build response
7. Send HTTP Response
HTTP/1.1 200 OK
Content-Type: application/json
{"id": 123, "name": "Alice"}
8. Client Processing
Parse response, render content
Time Breakdown (Typical)
DNS: ~20ms
TCP: ~30ms
TLS: ~50ms
Server: ~100ms
Transfer: ~20ms
Total: ~220ms
Why This Matters
- Each step is a potential bottleneck
- DNS caching reduces lookup time
- HTTP/2 multiplexing eliminates TCP connection overhead
- Keep-alive connections avoid repeated handshakes
Practice Problems
Design and implement a solution for HTTP in a backend system. Consider scalability, error handling, and production readiness.
Solution
// HTTP implementation
// Key aspects: validation, error handling, logging, testing
public class HTTP {
// Production-ready implementation
}Identify and handle edge cases for HTTP. What happens under high load, with invalid input, or during failures?
Solution
// Edge case handling:
// 1. Null/empty input -> validation
// 2. High load -> rate limiting, queuing
// 3. Failures -> retries, circuit breaker
// 4. Concurrent access -> locks, idempotencyWrite a testing strategy for HTTP. Include unit tests, integration tests, and performance tests.
Solution
// Test plan:
// - Unit: 80% coverage target
// - Integration: API contracts
// - Performance: latency, throughput
// - Chaos: failure injectionQuiz
1. What does HTTP stand for?
2. What makes HTTP a stateless protocol?
3. What is the primary purpose of HTTP?
4. What is a common mistake when implementing HTTP?
Flashcards
Question
What is HTTP?
Click to reveal answer
Answer
Hypertext Transfer Protocol — the foundation of web communication
Question
Is HTTP stateless or stateful?
Click to reveal answer
Answer
Stateless — each request is independent, server retains no session state
Question
What is HTTP?
Click to reveal answer
Answer
HTTP is a key concept in backend development.
Question
When to use HTTP?
Click to reveal answer
Answer
Use HTTP when building production systems that require reliability, scalability, and maintainability.
Question
HTTP best practices
Click to reveal answer
Answer
Follow SOLID principles, write clean code, test thoroughly, document decisions, and monitor in production.
Revision Notes
Key Takeaways
- 1.HTTP is the foundation of web communication
- 2.HTTP is stateless — each request is independent
- 3.HTTP methods: GET, POST, PUT, PATCH, DELETE
- 4.HTTP versions: 1.0, 1.1, 2, 3 with increasing performance
Interview Tips
- •Be able to explain the full HTTP request lifecycle
- •Know the difference between HTTP/1.1, HTTP/2, and HTTP/3
Cheat Sheet
HTTP
- Definition: Hypertext Transfer Protocol
- Stateless: Each request independent
- Methods: GET, POST, PUT, PATCH, DELETE
- Versions: 1.0, 1.1, 2, 3