Capacity mode, hot partition và DAX
DynamoDB đo throughput capacity theo số lần đọc và ghi; capacity mode và cách phân bố dữ liệu theo partition key quyết định bảng có bị throttle khi tải tăng hay không.
DynamoDB đo throughput capacity bằng read capacity unit (RCU) và write capacity unit (WCU). 1 RCU tương ứng một lần đọc strongly consistent mỗi giây (hoặc hai lần đọc eventually consistent) cho item tối đa 4 KB. 1 WCU tương ứng một lần ghi mỗi giây cho item tối đa 1 KB. DynamoDB có hai capacity mode, khác nhau về cách tính phí:
- Provisioned: cấp phát trước số RCU/WCU, tính phí theo giờ bất kể mức sử dụng thực tế. Bật DynamoDB auto scaling để capacity điều chỉnh theo tải, tuy nhiên auto scaling cần vài phút để phản ứng.
- On-Demand: tính phí theo từng request, không cần dự báo trước. Không phát sinh phí đọc ghi khi không có request.
Mỗi partition có giới hạn riêng: 3.000 RCU và 1.000 WCU. Chọn mẫu tải và capacity mode, sau đó mô phỏng một ngày vận hành:
Shipper chỉ làm việc ban ngày. Đơn hàng phát sinh theo đợt, tải tăng gấp nhiều lần chỉ trong vài phút. Vị trí thay đổi liên tục nên hiếm khi đọc lại đúng dữ liệu cũ.
Chọn mẫu tải và capacity mode, sau đó nhấn Mô phỏng một ngày. Mô hình đã được đơn giản hoá: Auto Scaling đặt capacity theo tải của giờ trước, với mức sử dụng mục tiêu 70%.
DAX (DynamoDB Accelerator) là cache in-memory chuyên dụng cho DynamoDB: giảm độ trễ từ mili giây xuống micro giây, sử dụng cùng API nên gần như không cần thay đổi code, ghi theo cơ chế write-through. DAX chỉ cache eventually consistent read; request đọc strongly consistent được chuyển thẳng tới bảng. DAX phù hợp khi nhiều người dùng đọc cùng một dữ liệu, ví dụ 90% lượt đọc tập trung vào trang chủ và các sản phẩm bán chạy. Ảnh tĩnh trên S3 được cache bằng CloudFront.
Bản thân DynamoDB lưu dữ liệu trên SSD, không phải cơ sở dữ liệu in-memory. Khi cần lưu trữ in-memory:
- Dữ liệu trên DynamoDB: DAX.
- Dữ liệu trên RDS, Aurora, hoặc dùng làm session store, bảng xếp hạng, truy vấn theo vị trí: ElastiCache (bài Analytics). Cần cache đa luồng: Memcached. Cần sorted set hoặc geospatial: Valkey hoặc Redis OSS.
- Tải biến động, có thời điểm gần bằng 0: On-Demand.
- Tải ổn định, dự đoán được: Provisioned + Auto Scaling.
- Hot partition do đọc lặp lại: DAX, không phải tăng RCU.
- DAX dành cho DynamoDB; ElastiCache dành cho các nguồn dữ liệu khác.
Capacity modes, hot partitions and DAX
DynamoDB measures throughput capacity in reads and writes; the capacity mode and how data is spread across partition keys decide whether the table is throttled when load rises.
DynamoDB measures throughput capacity in read capacity units (RCUs) and write capacity units (WCUs). 1 RCU is one strongly consistent read per second (or two eventually consistent reads) for an item up to 4 KB. 1 WCU is one write per second for an item up to 1 KB. DynamoDB has two capacity modes, billed differently:
- Provisioned: you provision RCUs/WCUs in advance and pay by the hour regardless of actual usage. Enable DynamoDB auto scaling so capacity follows the load, though auto scaling needs a few minutes to react.
- On-Demand: billed per request, with no forecasting needed. There are no read or write charges when there are no requests.
Each partition has its own limits: 3,000 RCUs and 1,000 WCUs. Choose a load pattern and a capacity mode, then simulate one day of operation:
Shipper chỉ làm việc ban ngày. Đơn hàng phát sinh theo đợt, tải tăng gấp nhiều lần chỉ trong vài phút. Vị trí thay đổi liên tục nên hiếm khi đọc lại đúng dữ liệu cũ.
Chọn mẫu tải và capacity mode, sau đó nhấn Mô phỏng một ngày. Mô hình đã được đơn giản hoá: Auto Scaling đặt capacity theo tải của giờ trước, với mức sử dụng mục tiêu 70%.
DAX (DynamoDB Accelerator) is an in-memory cache built for DynamoDB: it cuts latency from milliseconds to microseconds, uses the same API so almost no code changes are needed, and writes through to the table. DAX caches only eventually consistent reads; strongly consistent read requests go straight to the table. DAX fits when many users read the same data, for example when 90% of reads are concentrated on the home page and best-selling products. Static images on S3 are cached with CloudFront.
DynamoDB itself stores data on SSDs; it is not an in-memory database. When you need in-memory storage:
- Data on DynamoDB: DAX.
- Data on RDS or Aurora, or for a session store, leaderboards or location-based queries: ElastiCache (Analytics lesson). Multithreaded caching: Memcached. Sorted sets or geospatial: Valkey or Redis OSS.
- Fluctuating load that at times drops near zero: On-Demand.
- Steady, predictable load: Provisioned + Auto Scaling.
- A hot partition caused by repeated reads: DAX, not more RCUs.
- DAX is for DynamoDB; ElastiCache is for other data sources.