Table, item và primary key
Không phải dữ liệu nào cũng cần bảng quan hệ và câu lệnh SQL phức tạp; với dữ liệu luôn được tra cứu theo một khoá như mã giỏ hàng, key-value database cho độ trễ thấp hơn và dễ mở rộng hơn.
Một table chứa các item; mỗi item là một tập hợp attribute, các item không bắt buộc có cùng cấu trúc. Mỗi item có một primary key:
- Partition key: DynamoDB băm giá trị này để xác định partition chứa item.
- Sort key (không bắt buộc): sắp xếp các item có cùng partition key theo thứ tự, ví dụ theo ngày đặt hàng.
Dữ liệu được lưu trên SSD và tự động replicate trên nhiều AZ trong Region. AWS mô tả DynamoDB có hiệu năng mili giây một chữ số ở mọi quy mô, nên truy vấn theo key không chậm đi khi bảng lớn lên. Thử năm tác vụ của MFTECH:
Chọn một tác vụ của MFTECH và gửi request để quan sát đường đi của request trong DynamoDB. Với tình huống có tuỳ chọn cấu hình, bật tuỳ chọn và chạy lại để so sánh.
Một số kết luận:
- Khi cần truy vấn theo một attribute không phải key: tạo GSI (global secondary index), chọn attribute bất kỳ làm key mới; GSI có thể được tạo vào bất kỳ thời điểm nào. LSI dùng chung partition key với bảng, chỉ khác sort key, và chỉ tạo được khi tạo bảng.
- Tệp dung lượng lớn (video, ảnh, output 20 MB của một tác vụ) không lưu trong DynamoDB. Tệp được lưu trên S3 hoặc EFS; DynamoDB chỉ lưu S3 object key hoặc đường dẫn tới tệp.
- Dữ liệu quan hệ cần JOIN và có tải biến động: Aurora Serverless v2 (bài RDS), không phải DynamoDB.
- Truy xuất theo key, độ trễ vài mili giây, serverless, tự động mở rộng: DynamoDB.
- Truy vấn theo attribute khác: GSI.
- Item tối đa 400 KB; tệp lớn lưu trên S3.
- Truy cập từ private subnet: gateway VPC endpoint (chỉ S3 và DynamoDB hỗ trợ).
Tables, items and primary keys
Not all data needs relational tables and complex SQL; for data always looked up by one key, such as a cart ID, a key-value database delivers lower latency and is easier to scale.
A table contains items; each item is a set of attributes, and items do not have to share the same structure. Each item has a primary key:
- Partition key: DynamoDB hashes this value to determine the partition that holds the item.
- Sort key (optional): orders the items that share a partition key, for example by order date.
Data is stored on SSDs and automatically replicated across multiple AZs in the Region. AWS describes DynamoDB as delivering single-digit millisecond performance at any scale, so queries by key do not slow down as the table grows. Try five MFTECH tasks:
Chọn một tác vụ của MFTECH và gửi request để quan sát đường đi của request trong DynamoDB. Với tình huống có tuỳ chọn cấu hình, bật tuỳ chọn và chạy lại để so sánh.
Some conclusions:
- To query by an attribute that is not a key: create a GSI (global secondary index) with any attribute as the new key; a GSI can be created at any time. An LSI shares the table’s partition key, differs only in the sort key, and can be created only when the table is created.
- Large files (videos, images, a job’s 20 MB output) are not stored in DynamoDB. The files are stored on S3 or EFS; DynamoDB stores only the S3 object key or the file path.
- Relational data that needs JOINs and has fluctuating load: Aurora Serverless v2 (RDS lesson), not DynamoDB.
- Retrieval by key, millisecond latency, serverless, automatic scaling: DynamoDB.
- Queries on other attributes: GSI.
- Items are at most 400 KB; large files go on S3.
- Access from a private subnet: a gateway VPC endpoint (only S3 and DynamoDB support them).