Phương pháp tiếp cận thiết kế
Quy trình 6 bước để thiết kế một hệ thống trong phỏng vấn hoặc thực tế.
Bước 1: Thu thập yêu cầu (Requirements Gathering)
Xác định rõ ràng những gì hệ thống cần làm.
Functional requirements
- Chức năng cụ thể hệ thống cần cung cấp.
- Ví dụ: “User có thể đăng bài”, “User có thể like bài viết”.
Non‑functional requirements
- Scalability: Số lượng users, requests per second.
- Availability: SLA yêu cầu (99.9%, 99.99%?).
- Latency: Response time tối đa cho phép.
- Consistency: Strong consistency hay eventual consistency?
Ước lượng scale
- Số lượng users (DAU, MAU).
- Requests per second (RPS).
- Data size hiện tại và growth rate.
Bước 2: Ước lượng (Estimation)
Tính toán các con số để định hướng thiết kế.
Traffic estimates
- RPS (Requests Per Second):
RPS = (Total requests per day) / (Seconds per day) - Peak RPS: Thường gấp 2-10 lần average RPS.
Storage estimates
- Dung lượng:
Size per item * Items per day * Days - Growth rate: Dự kiến tăng trưởng theo tháng/năm.
Bandwidth estimates
- Upload bandwidth:
Data per upload * Uploads per second - Download bandwidth:
Data per download * Downloads per second
Bước 3: Thiết kế high‑level (High‑Level Design)
Vẽ sơ đồ khối với các components chính.
Components cần xác định
- Client: Web, Mobile, Desktop.
- Load Balancer: Phân phối traffic.
- App Servers: Xử lý business logic.
- Database: Lưu trữ dữ liệu.
- Cache: Tăng performance.
- CDN: Phân phối nội dung tĩnh.
- Message Queue: Xử lý bất đồng bộ.
Chọn công nghệ
- Dựa trên requirements và team expertise.
- Consider trade-offs giữa các lựa chọn.
Bước 4: Thiết kế chi tiết (Detailed Design)
Đi sâu vào từng component và data flow.
Database schema
- Tables, collections, indexes.
- Sharding strategy (nếu cần).
- Partition key selection.
API design
- Endpoints:
GET /api/posts,POST /api/posts. - Request/response format (JSON, GraphQL).
- Authentication & Authorization.
Data flow
- Sequence diagrams cho các use cases chính.
- State transitions.
Xử lý edge cases
- Failure scenarios (database down, network partition).
- Consistency issues (race conditions, concurrent updates).
- Retry logic & error handling.
Bước 5: Xác định bottlenecks và tối ưu
Tìm và giải quyết các điểm nghẽn.
Single point of failure
- Database → Add read replicas, failover.
- Load balancer → Multiple instances with health checks.
- Cache → Redis cluster.
Scalability bottlenecks
- Database → Sharding, read replicas.
- App server → Horizontal scaling, stateless design.
- Network → CDN, caching layers.
Performance bottlenecks
- Slow queries → Indexing, query optimization.
- Network calls → Batch requests, async processing.
- Memory → Caching, pagination.
Bước 6: Đánh giá trade‑offs
Mọi thiết kế đều có trade-offs. Cần hiểu và giải thích rõ.
Consistency vs Availability (CAP theorem)
- CP: Strong consistency, tolerate partitions (banking systems).
- AP: High availability, eventual consistency (social media).
Latency vs Throughput
- Low latency: Optimize for fast response time.
- High throughput: Optimize for number of requests processed.
Cost vs Performance
- More servers, replicas → Better performance, higher cost.
- Managed services → Less ops work, higher cost.
Complexity vs Flexibility
- Microservices → Flexible but complex to operate.
- Monolith → Simple but hard to scale.