Status Code Categories
HTTP status codes indicate the result of a server's attempt to process a request. They are grouped into five categories:
Status Code Categories
┌──────┬──────────────┬───────────────────────────────────┐
│ Code │ Category │ Meaning │
├──────┼──────────────┼───────────────────────────────────┤
│ 1xx │ Informational│ Request received, processing │
│ 2xx │ Success │ Request successfully received │
│ 3xx │ Redirection │ Further action needed │
│ 4xx │ Client Error │ Invalid request from client │
│ 5xx │ Server Error │ Server failed to fulfill request │
└──────┴──────────────┴───────────────────────────────────┘
1xx Informational
| Code | Name | Use |
|---|---|---|
| 100 | Continue | Client can send body |
| 101 | Switching Protocols | Protocol upgrade (WebSocket) |
2xx Success
| Code | Name | Use |
|---|---|---|
| 200 | OK | Successful GET, PUT, PATCH |
| 201 | Created | Successful POST (resource created) |
| 202 | Accepted | Request accepted for async processing |
| 204 | No Content | Successful DELETE (no response body) |
3xx Redirection
| Code | Name | Use |
|---|---|---|
| 301 | Moved Permanently | URL has changed permanently |
| 302 | Found | Temporary redirect |
| 304 | Not Modified | Cached response is still valid |
4xx Client Error
| Code | Name | Use |
|---|---|---|
| 400 | Bad Request | Malformed request syntax |
| 401 | Unauthorized | Authentication required |
| 403 | Forbidden | Authenticated but not authorized |
| 404 | Not Found | Resource doesn't exist |
| 405 | Method Not Allowed | HTTP method not supported |
| 409 | Conflict | Resource state conflict |
| 422 | Unprocessable Entity | Validation failed |
| 429 | Too Many Requests | Rate limit exceeded |
5xx Server Error
| Code | Name | Use |
|---|---|---|
| 500 | Internal Server Error | Unexpected server failure |
| 502 | Bad Gateway | Upstream server returned invalid response |
| 503 | Service Unavailable | Server temporarily overloaded |
| 504 | Gateway Timeout | Upstream server timeout |
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 Status Codes 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 Status Codes in a backend system. Consider scalability, error handling, and production readiness.
Solution
// HTTP Status Codes implementation
// Key aspects: validation, error handling, logging, testing
public class HTTPStatusCodes {
// Production-ready implementation
}Identify and handle edge cases for HTTP Status Codes. 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 Status Codes. 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 status code should you return when rate limiting a client?
2. What is the difference between 401 and 403?
3. What is the primary purpose of HTTP Status Codes?
4. What is a common mistake when implementing HTTP Status Codes?
Flashcards
Question
What does 404 mean?
Click to reveal answer
Answer
Not Found — the requested resource doesn't exist
Question
Difference between 401 and 403?
Click to reveal answer
Answer
401 = not authenticated (needs login). 403 = not authorized (insufficient permissions)
Question
What is HTTP Status Codes?
Click to reveal answer
Answer
HTTP Status Codes is a key concept in backend development.
Question
When to use HTTP Status Codes?
Click to reveal answer
Answer
Use HTTP Status Codes when building production systems that require reliability, scalability, and maintainability.
Question
HTTP Status Codes 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.1xx=info, 2xx=success, 3xx=redirect, 4xx=client error, 5xx=server error
- 2.201 for creation, 204 for deletion with no body
- 3.401=unauthenticated, 403=unauthorized
- 4.429 for rate limiting
Interview Tips
- •Know the meaning of all common status codes
- •Understand when to use 401 vs 403
Cheat Sheet
Status Codes
- 200: OK, 201: Created, 204: No Content
- 301: Permanent Redirect, 304: Not Modified
- 400: Bad Request, 401: Unauthorized, 403: Forbidden
- 404: Not Found, 422: Validation Error, 429: Rate Limited
- 500: Server Error, 502: Bad Gateway, 503: Unavailable