DynamoDB Data Modeling: Partition and Sort Keys
Svep för att visa menyn
Indexing and Queries
DynamoDB provides indexing options to optimize query flexibility:
Global Secondary Index (GSI)
- Allows querying by different keys from the base table;
- Ideal when you need multiple ways to access data (e.g., by username instead of user ID).
Local Secondary Index (LSI)
- Uses the same partition key, but a different sort key;
- Useful for filtering data within a single partition based on other attributes.
Advanced Features
Transactions
- Enables atomic operations across multiple items and tables;
- Ensures data consistency in financial, inventory, or user-related systems.
Time to Live (TTL)
- Attach an expiry timestamp to records;
- DynamoDB will automatically delete expired items — perfect for session tokens, temporary cache, or logs.
DynamoDB Streams
DynamoDB Streams captures every item-level change (insert, update, delete) in near real-time and stores them for 24 hours.
You can:
- Trigger AWS Lambda functions in response;
- Build event-driven workflows like sending notifications, syncing data, updating analytics dashboards, or even audit trails;
- Avoid manual polling, reducing overhead.
Creating a Table from the AWS CLI
Let's walk through creating a table called orders using the AWS CLI:
aws dynamodb create-table \
--table-name orders \
--attribute-definitions \
AttributeName=user_id,AttributeType=S \
AttributeName=order_id,AttributeType=S \
--key-schema \
AttributeName=user_id,KeyType=HASH \
AttributeName=order_id,KeyType=RANGE \
--provisioned-throughput \
ReadCapacityUnits=5 \
WriteCapacityUnits=5
- user_id: partition key;
- order_id: sort key;
- Provisioned Throughput: setting read and write capacity manually (more on this below).
Once created, verify it:
aws dynamodb describe-table --table-name orders
And finally, to clean up and avoid charges:
aws dynamodb delete-table --table-name orders
Capacity Modes
DynamoDB supports two capacity modes:
Provisioned Mode
- You manually specify read and write units;
- Suitable for predictable workloads;
- Supports auto-scaling to adjust capacity based on usage.
On-Demand Mode
- No upfront capacity planning;
- Pay per request (read/write);
- Ideal for spiky or unpredictable traffic.
For production, on-demand mode offers simplicity. If your workload is stable and predictable, provisioned mode with autoscaling can be more cost-effective.
Monitoring and Optimization
Use Amazon CloudWatch to:
- Track performance metrics;
- Identify throttling events, which signal your application is exceeding the provisioned throughput;
- Optimize access patterns or scale up as needed.
Summary
Amazon DynamoDB is:
- Fast: consistently low-latency reads and writes;
- Scalable: handles millions of operations seamlessly;
- Serverless: zero infrastructure to manage;
- Feature-rich: with streams, TTL, indexes, and transactions.
Design your schema based on how you query the data, not how it's structured. Think in terms of access patterns, not normalization.
1. Which of the following best describes Amazon DynamoDB?
2. What are the primary components of a DynamoDB table?
3. True or False: DynamoDB supports SQL queries and table joins.
4. What does a Global Secondary Index (GSI) allow you to do?
5. True or False: A Local Secondary Index (LSI) allows you to query using a different partition key.
6. Which feature allows you to automatically delete expired records in DynamoDB?
7. What is required at a minimum when defining a DynamoDB table?
8. Which DynamoDB capacity mode is ideal for unpredictable traffic patterns?
9. What can DynamoDB Streams be used for?
10. True or False: DynamoDB is optimized for schema-first design and data normalization.
Tack för dina kommentarer!
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal