Response Format
Consistent Response Wrapper
{
"success": true,
"data": {
"id": 123,
"name": "Wireless Mouse",
"price": 29.99
},
"metadata": {
"timestamp": "2025-01-15T10:30:00Z",
"traceId": "abc-123"
}
}
Collection Response
{
"success": true,
"data": [
{ "id": 1, "name": "Mouse" },
{ "id": 2, "name": "Keyboard" }
],
"pagination": {
"page": 1,
"limit": 20,
"total": 150,
"totalPages": 8
}
}
Error Response
{
"success": false,
"error": {
"code": "RESOURCE_NOT_FOUND",
"message": "Product not found with id: 123"
},
"metadata": {
"timestamp": "2025-01-15T10:30:00Z",
"traceId": "abc-456"
}
}
Response Headers
| Header | Purpose |
|---|---|
Content-Type |
Response format (application/json) |
X-Request-Id |
Request tracing |
X-Total-Count |
Total items for pagination |
Cache-Control |
Caching instructions |
ETag |
Cache validation |
Design Patterns
Creational
- Factory
- Builder
- Singleton
- Prototype
Structural
- Adapter
- Decorator
- Facade
- Proxy
Behavioral
- Observer
- Strategy
- Command
- State
Best Patterns
- Use appropriately
- Don't over-engineer
- Prefer composition
- Follow SOLID principles
Key Points
- Understanding Response Design 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 Response Design in a backend system. Consider scalability, error handling, and production readiness.
Solution
// Response Design implementation
// Key aspects: validation, error handling, logging, testing
public class ResponseDesign {
// Production-ready implementation
} Identify and handle edge cases for Response Design. 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, idempotency Write a testing strategy for Response Design. Include unit tests, integration tests, and performance tests.
Solution
// Test plan:
// - Unit: 80% coverage target
// - Integration: API contracts
// - Performance: latency, throughput
// - Chaos: failure injection Quiz
1. What should every API response include?
2. What header helps with request tracing?
3. What is a common mistake when implementing Response Design?
Flashcards
Question
What should a response wrapper include?
Click to reveal answer
Answer
success flag, data/error, metadata (timestamp, traceId)
Question
What header aids request tracing?
Click to reveal answer
Answer
X-Request-Id or X-Trace-Id
Question
Response Design 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. Use a consistent response wrapper format
- 2. Include success, data/error, and metadata
- 3. Use pagination objects for collections
- 4. Include trace IDs for debugging
Interview Tips
- • Design a response format for a given scenario
- • Explain why consistent responses matter
Cheat Sheet
Response Design
- Wrapper: { success, data, error, metadata }
- Collection: Include pagination { page, limit, total, totalPages }
- Error: { code, message, details }
- Headers: X-Request-Id, Cache-Control, ETag