Appearance
DynamoDB Auto-Scaling
On-Demand vs Provisioned Capacity
On-Demand (Serverless):
Characteristics:
- Pay-per-request pricing
- Instant scaling (0 to thousands of requests)
- No capacity planning
- More expensive per request than well-utilized provisioned capacity (ratio depends on utilization)
Best for:
- New applications (unknown traffic)
- Unpredictable workloads (spiky traffic)
- Dev/test environments
- Rapid prototyping
Pricing (us-east-1, 2026-11-20; nguồn https://aws.amazon.com/dynamodb/pricing/on-demand/):
- Write: $0.625 per million write request units
- Read: $0.125 per million read request units (strongly consistent 4KB; eventually consistent = 0.5 RRU)Provisioned Capacity:
Characteristics:
- Pre-provision RCU/WCU
- Auto-scaling available (react to load)
- Cheaper for predictable traffic
- Reserved capacity discounts available
Best for:
- Production apps with predictable traffic
- Cost optimization (rẻ hơn on-demand khi tải ổn định; ví dụ bên dưới rẻ hơn ~71%)
- High-throughput applications
Pricing (us-east-1, 2026-11-20; nguồn https://aws.amazon.com/dynamodb/pricing/provisioned/):
- WCU: $0.00065 per hour (~$0.47/month)
- RCU: $0.00013 per hour (~$0.09/month)Cost comparison example:
Workload: 100 writes/sec, 1KB items (steady 24/7)
On-Demand:
- 100 writes/sec × 86,400 sec/day × 30 days = 259M writes/month
- 259M × $0.625 / 1M = $161.88/month
Provisioned:
- 100 WCU × $0.00065/hr × 730 hr = $47.45/month
- Savings: $161.88 − $47.45 = $114.43/month (~71% rẻ hơn)When to switch:
- Start with On-Demand (no planning needed)
- Switch to Provisioned when traffic becomes predictable
Auto-Scaling Configuration
Enable auto-scaling:
bash
# AWS CLI, bảng phải ở chế độ provisioned (chưa chạy: dịch vụ quản lý; đã đối chiếu tham số với tài liệu CLI)
aws application-autoscaling register-scalable-target \
--service-namespace dynamodb \
--resource-id table/MyTable \
--scalable-dimension dynamodb:table:WriteCapacityUnits \
--min-capacity 5 \
--max-capacity 1000
aws application-autoscaling put-scaling-policy \
--service-namespace dynamodb \
--resource-id table/MyTable \
--scalable-dimension dynamodb:table:WriteCapacityUnits \
--policy-name MyScalingPolicy \
--policy-type TargetTrackingScaling \
--target-tracking-scaling-policy-configuration \
'TargetValue=70.0,PredefinedMetricSpecification={PredefinedMetricType=DynamoDBWriteCapacityUtilization}'Auto-scaling behavior:
- Scale up: khi mức dùng vượt mức mục tiêu (ví dụ 70%) trong 2 phút liên tiếp
- Scale down: khi 15 điểm dữ liệu (phút) liên tiếp thấp hơn mức mục tiêu
- Cập nhật capacity mất vài phút; request vượt capacity cũ trong lúc đó bị throttle. Spike ngắn do burst capacity đỡ
- Scale up faster than down (protect against traffic spikes)
Partition Key Design for Even Distribution
❌ Bad: Hot partition (uneven access)
javascript
// PK = date (all writes go to today's partition)
PK: "2025-01-09" ← ALL traffic hits this partition✅ Good: Even distribution
javascript
// PK = userId + date
PK: "USER#12345#2025-01-09" ← Traffic distributed across usersPartition calculation:
# Ước lượng minh hoạ kiểu cũ, không phải công thức AWS cam kết (số partition do DynamoDB quản lý); mỗi partition tối đa 3,000 RCU / 1,000 WCU, adaptive capacity + split-for-heat giúp key nóng
capacity_partitions = CEIL((RCU / 3000) + (WCU / 1000))
storage_partitions = CEIL(data_size_GB / 10)
Example:
- 10,000 WCU, 10,000 RCU, 50GB data
- capacity_partitions = CEIL(10000/1000 + 10000/3000) = 14
- storage_partitions = CEIL(50/10) = 5
- Total partitions = 14
Each partition: ~714 WCU, ~714 RCU, ~3.5GBGlobal Tables (Multi-Region)
Setup global table:
bash
# Tạo bảng ở us-east-1 (on-demand để khỏi cấu hình auto scaling)
aws dynamodb create-table --table-name Users --region us-east-1 \
--attribute-definitions AttributeName=userId,AttributeType=S \
--key-schema AttributeName=userId,KeyType=HASH \
--billing-mode PAY_PER_REQUEST
# Thêm replica (global tables 2019.11.21); chờ bảng ACTIVE rồi thêm region kế tiếp
aws dynamodb update-table --table-name Users --region us-east-1 \
--replica-updates '[{"Create": {"RegionName": "eu-west-1"}}]'
aws dynamodb update-table --table-name Users --region us-east-1 \
--replica-updates '[{"Create": {"RegionName": "ap-southeast-1"}}]'Characteristics:
Replication (MREC, mặc định):
- Bất đồng bộ, thường trong vòng 1 giây hoặc ít hơn (theo doc AWS)
- Eventually consistent giữa các region
- Xung đột: last writer wins theo timestamp nội bộ từng item
- MRSC (multi-Region strong consistency): ghi đồng bộ tới ít nhất 1 region khác; phải đúng 3 region (hoặc 2 replica + 1 witness), không hỗ trợ TTL, transaction, LSI
Use cases:
- Global applications (low latency worldwide)
- Disaster recovery (region failover)
- Data residency compliance (EU data in EU)
Cost:
- Mỗi replica tiêu thụ write capacity cho bản sao (replicated write: rWCU ở provisioned, rWRU ở on-demand)
- Một lần ghi vào bảng global 3 region tốn write capacity ở cả 3 regionPhải nhớ:
- On-Demand cho tải khó đoán; provisioned + auto scaling rẻ hơn khi tải ổn định (ví dụ trên: ~71%).
- Auto scaling phản ứng sau ≥ 2 phút và mất vài phút để cập nhật; spike ngắn dựa vào burst capacity.
- Global tables MREC dùng last writer wins; cần strong consistency xuyên region thì dùng MRSC.