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ỏiPattern liên quanGhi nhớ
Client cần gom dữ liệu từ nhiều service?API CompositionChọ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 OrchestrationParallel giảm latency; chain làm latency cộng dồn
Work/event không fit request-response?Async API PatternChọn polling, SSE, WebSocket, webhook hoặc queue theo direction/durability
Public/simple CRUD hay internal high-performance?API ProtocolREST 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 LimitingPartial coverage tạo false confidence
API sẽ thay đổi theo thời gian?API Lifecycle Management, API Versioning, API DocumentationVersion, 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

Liên kết