Resource Hierarchy
Resource Relationships
Order
├── customer (belongs to)
├── items (has many)
│ └── product (references)
├── payment (has one)
└── shipping (has one)
Flat vs Nested Resources
# Flat (preferred for most cases)
GET /orders
GET /orders/123
GET /order-items?orderId=123
# Nested (for strict ownership)
GET /users/456/orders
GET /users/456/orders/123
Resource Design Patterns
| Pattern | Example | When to Use |
|---|---|---|
| Single resource | /products/123 |
Fetch specific item |
| Collection | /products |
List/filter items |
| Sub-resource | /products/123/reviews |
Owned resources |
| Action | /products/123/archive |
Non-CRUD operations |
| Search | /products/search?q=phone |
Search endpoints |
Handling Actions in REST
When an action doesn't map to CRUD:
# Option 1: Use a sub-resource
POST /orders/123/cancel
# Option 2: Use HTTP method + status
PATCH /orders/123
{ "status": "cancelled" }
# Option 3: Use action endpoint
POST /orders/123/actions/cancel
Resource Field Design
{
"id": "123",
"name": "Wireless Mouse",
"price": 29.99,
"currency": "USD",
"createdAt": "2025-01-15T10:30:00Z",
"_links": {
"self": "/products/123",
"reviews": "/products/123/reviews"
}
}
Include: IDs, core data, timestamps, links.
Exclude: Internal implementation details.
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 Resource 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 Resource Design in a backend system. Consider scalability, error handling, and production readiness.
Solution
// Resource Design implementation
// Key aspects: validation, error handling, logging, testing
public class ResourceDesign {
// Production-ready implementation
}Identify and handle edge cases for Resource 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, idempotencyWrite a testing strategy for Resource Design. 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. When should you use nested resources?
2. What should you exclude from API resource responses?
3. What is the primary purpose of Resource Design?
4. What is a common mistake when implementing Resource Design?
Flashcards
Question
When to use nested resources?
Click to reveal answer
Answer
When there is a strict ownership relationship (e.g., /users/123/orders)
Question
What goes in a resource response?
Click to reveal answer
Answer
IDs, core data, timestamps, links — exclude internal details
Question
What is Resource Design?
Click to reveal answer
Answer
Resource Design is a key concept in backend development.
Question
When to use Resource Design?
Click to reveal answer
Answer
Use Resource Design when building production systems that require reliability, scalability, and maintainability.
Question
Resource 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.Design resources as nouns with clear relationships
- 2.Use nested resources for strict ownership only
- 3.Flat resources are simpler and preferred
- 4.Include IDs, data, timestamps, links — exclude internals
Interview Tips
- •Explain when to use nested vs flat resources
- •Know how to design resource field responses
Cheat Sheet
Resource Design
- Flat:
/orders,/products(simpler, preferred) - Nested:
/users/123/orders(strict ownership) - Actions: POST
/orders/123/cancelor PATCH with status - Fields: id, data, timestamps, _links (no internals)