What is Keep-Alive?
HTTP Keep-Alive (persistent connections) allows multiple HTTP requests/responses over a single TCP connection, avoiding the overhead of establishing a new connection for each request.
Without Keep-Alive (HTTP/1.0)
Request 1: TCP handshake → Request → Response → Close
Request 2: TCP handshake → Request → Response → Close
Request 3: TCP handshake → Request → Response → Close
Total: 3 TCP handshakes + 3 requests
With Keep-Alive (HTTP/1.1+)
Connection Established (1 TCP handshake)
|
+-- Request 1 → Response 1
+-- Request 2 → Response 2
+-- Request 3 → Response 3
|
Connection Closed
Total: 1 TCP handshake + 3 requests
Keep-Alive Headers
# Client request
Connection: keep-alive
# Server response
Connection: keep-alive
Keep-Alive: timeout=5, max=100
# Close connection explicitly
Connection: close
Keep-Alive Parameters
| Parameter | Description |
|---|---|
| timeout | Seconds to keep idle connection alive |
| max | Maximum requests per connection |
Performance Impact
Without Keep-Alive:
- 3 requests × (TCP handshake: 30ms + TLS: 50ms) = 240ms overhead
With Keep-Alive:
- 1 handshake (80ms) for 3 requests = 80ms overhead
- 67% reduction in connection overhead
Keep-Alive in Practice
// Node.js
const http = require('http');
const server = http.createServer((req, res) => {
res.end('Hello');
});
// Enable keep-alive (default in HTTP/1.1)
server.keepAliveTimeout = 5000; // 5 seconds
Why Keep-Alive Matters
- Reduces latency — No repeated TCP/TLS handshakes
- Improves throughput — Multiple requests pipelined
- Reduces server load — Fewer connections to manage
- Better UX — Faster page loads
HTTP Best Practices
Methods
- GET: Read (safe, idempotent)
- POST: Create
- PUT: Replace (idempotent)
- PATCH: Partial update
- DELETE: Remove (idempotent)
Headers
- Content-Type: Body format
- Cache-Control: Caching rules
- Authorization: Authentication
- Accept: Desired response format
Status Codes
- 2xx: Success
- 3xx: Redirection
- 4xx: Client error
- 5xx: Server error
Key Points
- Understanding HTTP Keep-Alive is essential for production systems
- Always consider scalability and maintainability
- Test thoroughly before deploying to production
- Monitor performance and set up alerting
Common Patterns
- Validation: Always validate input at the boundary
- Error Handling: Use structured error responses
- Logging: Log key events for debugging
- Testing: Unit, integration, and load tests
- Documentation: Keep docs updated with code changes
Practice Problems
Design and implement a solution for HTTP Keep-Alive in a backend system. Consider scalability, error handling, and production readiness.
Solution
// HTTP Keep-Alive implementation
// Key aspects: validation, error handling, logging, testing
public class HTTPKeepAlive {
// Production-ready implementation
}Identify and handle edge cases for HTTP Keep-Alive. 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 Keep-Alive. 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 Keep-Alive do?
2. Is Keep-Alive enabled by default in HTTP/1.1?
3. What is the primary purpose of HTTP Keep-Alive?
4. What is a common mistake when implementing HTTP Keep-Alive?
Flashcards
Question
What is HTTP Keep-Alive?
Click to reveal answer
Answer
Persistent connections that reuse TCP for multiple requests
Question
Is Keep-Alive default in HTTP/1.1?
Click to reveal answer
Answer
Yes — connections are persistent by default, Connection: close to disable
Question
What is HTTP Keep-Alive?
Click to reveal answer
Answer
HTTP Keep-Alive is a key concept in backend development.
Question
When to use HTTP Keep-Alive?
Click to reveal answer
Answer
Use HTTP Keep-Alive when building production systems that require reliability, scalability, and maintainability.
Question
HTTP Keep-Alive 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.Keep-Alive reuses TCP connections for multiple requests
- 2.Default in HTTP/1.1 (must send Connection: close to disable)
- 3.Reduces latency by avoiding repeated handshakes
- 4.timeout and max parameters control connection lifecycle
Interview Tips
- •Explain why Keep-Alive improves performance
- •Know the Keep-Alive header parameters
Cheat Sheet
Keep-Alive
- Purpose: Reuse TCP connections
- HTTP/1.1: Default enabled (Connection: close to disable)
- Params: timeout (idle time), max (requests per connection)
- Benefit: ~67% less connection overhead