API Design Patterns
Ý chính
Thiết kế API tốt không chỉ là đặt endpoint đẹp. Nó là tổ hợp của contract ổn định, protocol phù hợp, composition đúng chỗ, async pattern đúng use case và cross-cutting concerns được áp dụng đồng nhất.
Map quyết định
| Câu hỏi | Pattern liên quan | Ghi nhớ |
|---|---|---|
| Client cần gom dữ liệu từ nhiều service? | API Composition | Chọn client, gateway, BFF, GraphQL hoặc edge theo round trip, cache và ownership |
| Các call độc lập hay phụ thuộc nhau? | API Aggregation, API Orchestration | Parallel giảm latency; chain làm latency cộng dồn |
| Work/event không fit request-response? | Async API Pattern | Chọn polling, SSE, WebSocket, webhook hoặc queue theo direction/durability |
| Public/simple CRUD hay internal high-performance? | API Protocol | REST dễ dùng; gRPC nhanh; GraphQL linh hoạt; SSE/WebSocket cho realtime |
| Concern nào phải chạy trên mọi route? | API Security, Input Validation, Rate Limiting | Partial coverage tạo false confidence |
| API sẽ thay đổi theo thời gian? | API Lifecycle Management, API Versioning, API Documentation | Version, deprecation và docs là một phần của contract |
Mental model
API product surface
-> contract và versioning
-> protocol/transport
-> composition hoặc async flow
-> auth/rate limit/validation/logging
-> observability và failure handling