Case Study 4: E‑commerce Platform (Amazon)
Sàn thương mại điện tử với product catalog, cart, order, payment, recommendations.
Bước 1: Thu thập yêu cầu
Functional requirements
- Product catalog: Browse, search, filter products.
- Shopping cart: Add/remove items, update quantity.
- Order processing: Checkout, payment, shipping.
- Inventory management: Track stock levels.
- Recommendations: Personalized product suggestions.
- User accounts: Profiles, order history, addresses.
- Reviews & Ratings: User feedback.
Non‑functional requirements
- High availability: 99.99% uptime (critical for sales).
- Consistency: Inventory và orders phải consistent.
- Low latency: Page load < 2s, search < 200ms.
- Scalability: Handle traffic spikes (Black Friday, sales).
- Security: PCI DSS compliance cho payment.
Scale estimation
- Users: 100 triệu DAU.
- Products: 100 triệu SKUs.
- Orders per day: 10 triệu.
- Page views per day: 1 tỷ.
- Peak traffic: 10x normal (sales events).
Bước 2: Ước lượng
Traffic estimates
- Page views: 1B / 86400 ≈ 12,000 RPS (average).
- Search requests: 100M / 86400 ≈ 1,200 RPS.
- Orders: 10M / 86400 ≈ 120 RPS.
- Peak RPS: ~10x → 120,000 page views, 1,200 orders.
Storage estimates
- Product catalog: 100M products * 10 KB ≈ 1 TB.
- User data: 100M users * 1 KB ≈ 100 GB.
- Orders: 10M/day * 1 KB * 365 * 5 ≈ 18 TB (5 years).
- Images: 100M products * 5 images * 500 KB ≈ 250 TB.
Bandwidth estimates
- Page load: 12,000 RPS * 100 KB ≈ 1.2 GB/s.
- Images: Significant, cần CDN.
Bước 3: Thiết kế High‑Level
Components chính
┌──────────┐ ┌─────────────┐ ┌──────────────┐
│ Client │ ──→ │ CDN / WAF │ ──→ │ API Gateway │
│ │ │ (CloudFlare)│ │ │
└──────────┘ └─────────────┘ └──────────────┘
│
┌───────────────────────────────────┼───────────────────────────────────┐
▼ ▼ ▼
┌───────────────┐ ┌───────────────┐ ┌───────────────┐
│ Product │ │ Cart │ │ Order │
│ Service │ │ Service │ │ Service │
└───────────────┘ └───────────────┘ └───────────────┘
│ │ │
▼ ▼ ▼
┌───────────────┐ ┌───────────────┐ ┌───────────────┐
│ PostgreSQL │ │ Redis │ │ PostgreSQL │
│ (Products) │ │ (Cart) │ │ (Orders) │
└───────────────┘ └───────────────┘ └───────────────┘
│
▼
┌───────────────┐
│ Elasticsearch │
│ (Search) │
└───────────────┘
Technology selection
- Frontend: React/Next.js với SSR.
- API Gateway: Kong hoặc AWS API Gateway.
- Services: Microservices (Java/Spring hoặc Node.js).
- Database: PostgreSQL cho transactions, Redis cho cart.
- Search: Elasticsearch cho product search.
- Cache: Redis cho sessions, cart, popular products.
- CDN: CloudFront cho static content, images.
- Message Queue: Kafka cho events (order placed, inventory updated).
Bước 4: Thiết kế Chi tiết
Database Schema
Table: products
| Column | Type | Description |
|---|---|---|
| product_id | BIGINT | Primary key |
| title | VARCHAR(500) | Product name |
| description | TEXT | Product description |
| price | DECIMAL(10,2) | Current price |
| category_id | BIGINT | Foreign key |
| inventory_count | INT | Stock level |
| created_at | TIMESTAMP | Created time |
Table: users
| Column | Type | Description |
|---|---|---|
| user_id | BIGINT | Primary key |
| VARCHAR(255) | Unique, indexed | |
| password_hash | VARCHAR(255) | Hashed password |
| created_at | TIMESTAMP | Join date |
Table: orders
| Column | Type | Description |
|---|---|---|
| order_id | UUID | Primary key |
| user_id | BIGINT | Foreign key |
| total_amount | DECIMAL(10,2) | Order total |
| status | TINYINT | Pending/Paid/Shipped/Delivered |
| created_at | TIMESTAMP | Order time |
| Index: (user_id, created_at) |
Table: order_items
| Column | Type | Description |
|---|---|---|
| order_id | UUID | Foreign key |
| product_id | BIGINT | Foreign key |
| quantity | INT | Quantity ordered |
| price | DECIMAL(10,2) | Price at time of order |
API Design
GET /api/v1/products?category=electronics&q=laptop&page=1&limit=20
GET /api/v1/products/{id}
POST /api/v1/cart/items
{
"product_id": 12345,
"quantity": 2
}
GET /api/v1/cart
POST /api/v1/orders
{
"items": [
{ "product_id": 12345, "quantity": 2 }
],
"shipping_address": {...},
"payment_method": {...}
}
POST /api/v1/payments
{
"order_id": "uuid",
"card_token": "tok_xxx"
}
Data Flow
Browse Products:
- Client request /products với filters.
- API Gateway → Product Service.
- Query Elasticsearch với filters.
- Return results với pagination.
- Cache popular searches.
Add to Cart:
- Client POST /cart/items.
- Cart Service validate product availability.
- Store in Redis hash:
cart:{user_id}. - Set TTL 30 days.
- Return updated cart.
Checkout:
- Client POST /orders.
- Order Service create order (PENDING).
- Reserve inventory (decrement count).
- Call Payment Service.
- Payment success → update order status (PAID).
- Publish
order.placedevent. - Inventory Service consume event, update stock.
- Shipping Service consume event, prepare shipment.
Payment Processing:
- Payment Service call Stripe/PayPal API.
- Handle 3D Secure nếu cần.
- Store payment result.
- Idempotency key để tránh double charge.
- Webhook handling cho async payment status.
Bước 5: Bottlenecks & Tối ưu
Single Point of Failure
- Database: PostgreSQL với streaming replication (1 master, 2 slaves).
- Payment: Multiple payment providers (Stripe + PayPal).
- API Gateway: Multiple instances với health checks.
Scalability Bottlenecks
- Database write: Orders table partitioning theo created_at (monthly).
- Search: Elasticsearch cluster với sharding.
- Cart: Redis cluster cho horizontal scaling.
- Inventory: Optimistic locking để tránh overselling.
Performance Optimization
- CDN: Cache product images, static assets.
- Database read replicas: Cho product browsing.
- Caching:
- Redis cho popular products, categories.
- Application-level cache cho product details.
- Async processing:
- Email notifications via Kafka.
- Recommendation updates batched.
Inventory Consistency
- Optimistic locking:
UPDATE products SET inventory = inventory - 1 WHERE id = ? AND inventory > 0. - Reserve timeout: Release reserved inventory sau 15 phút nếu không thanh toán.
- Queue-based: Serialze inventory updates qua message queue.
Bước 6: Trade‑offs
Consistency vs Availability
- CP system cho inventory và orders: Strong consistency required.
- AP system cho product browsing: Eventual consistency acceptable.
Latency vs Throughput
- Checkout: Low latency critical → synchronous processing.
- Notifications: High throughput → async via Kafka.
Monolith vs Microservices
| Approach | Pros | Cons |
|---|---|---|
| Monolith | Simple, ACID transactions | Hard to scale, deployment risk |
| Microservices | Independent scaling, deployment | Distributed transactions, complexity |
Database per Service vs Shared Database
- Database per service: Isolation, independent scaling, nhưng cần saga pattern cho transactions.
- Shared database: Easier transactions, nhưng coupling và scaling khó.
Kết luận
E-commerce platform là hệ thống phức tạp với nhiều requirements:
- Transaction integrity: Payment và inventory phải consistent.
- High availability: Downtime = mất revenue.
- Traffic spikes: Black Friday, flash sales.
- Security: PCI DSS, user data protection.
- Personalization: Recommendations, search ranking.