AWS DynamoDB
Fast and flexible NoSQL database service for any scale
Amazon DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability.
Key Benefits
DynamoDB offers single-digit millisecond latency at any scale, with built-in security, backup, and in-memory caching.
Key Concepts
Tables
The primary resource in DynamoDB. Unlike relational databases, tables don't have a fixed schema.
Items
Individual records in a table. Each item is a collection of attributes.
Attributes
Data elements within items. Similar to columns in relational databases.
Data Access Patterns
Primary Key
Every table requires a primary key:
| Type | Description |
|---|---|
| Partition Key | Single attribute that uniquely identifies an item |
| Composite Key | Partition key + sort key for more complex queries |
Choose your partition key carefully! A good partition key distributes data evenly and supports your query patterns.
Data Types
| Type | Description |
|---|---|
| S | String |
| N | Number |
| B | Binary |
| BOOL | Boolean |
| NULL | Null |
| Type | Description |
|---|---|
| M | Map (nested attributes, like JSON objects) |
| L | List (ordered collection, like arrays) |
| Type | Description |
|---|---|
| SS | String Set (unique strings) |
| NS | Number Set (unique numbers) |
| BS | Binary Set (unique binary values) |
Table Design
Example: E-commerce Orders
{
"TableName": "Orders",
"KeySchema": [
{"AttributeName": "customerId", "KeyType": "HASH"},
{"AttributeName": "orderId", "KeyType": "RANGE"}
],
"AttributeDefinitions": [
{"AttributeName": "customerId", "AttributeType": "S"},
{"AttributeName": "orderId", "AttributeType": "S"}
],
"BillingMode": "PAY_PER_REQUEST"
}Item Example
{
"customerId": {"S": "CUST-001"},
"orderId": {"S": "ORD-2024-001"},
"orderDate": {"S": "2024-01-15"},
"total": {"N": "99.99"},
"items": {"L": [
{"M": {"productId": {"S": "PROD-1"}, "quantity": {"N": "2"}}}
]},
"status": {"S": "SHIPPED"}
}Secondary Indexes
Global Secondary Index (GSI)
Query data using different partition and sort keys.
{
"IndexName": "StatusIndex",
"KeySchema": [
{"AttributeName": "status", "KeyType": "HASH"},
{"AttributeName": "orderDate", "KeyType": "RANGE"}
],
"Projection": {"ProjectionType": "ALL"}
}GSIs have their own provisioned capacity and can be created/deleted at any time.
Local Secondary Index (LSI)
Same partition key but different sort key. Must be created with the table.
LSIs share throughput with the base table and cannot be created after table creation.
Capacity Modes
- Pay per request pricing
- Automatic scaling for spiky workloads
- No capacity planning needed
Best for:
- New applications with unknown workloads
- Unpredictable traffic patterns
- Preferring pay-per-use pricing
- Specify read/write capacity units (RCU/WCU)
- More cost-effective for predictable workloads
- Auto-scaling available
aws dynamodb update-table \
--table-name Orders \
--provisioned-throughput ReadCapacityUnits=100,WriteCapacityUnits=50Read Consistency
| Type | Description |
|---|---|
| Eventually Consistent | Default, lower latency, may return stale data |
| Strongly Consistent | Always returns latest data, higher latency |
Query vs Scan
- Requires partition key
- Efficient and fast
- Uses indexes
Always use Query when possible!
- Reads every item in table
- Expensive and slow
- Consumes lots of RCUs
Warning
Avoid Scan operations in production. Design your data model to use Query operations instead.
Streams
Capture item-level changes for real-time processing:
aws dynamodb update-table \
--table-name Orders \
--stream-specification StreamEnabled=true,StreamViewType=NEW_AND_OLD_IMAGESStream View Types:
- KEYS_ONLY - Only the key attributes
- NEW_IMAGE - The item after modification
- OLD_IMAGE - The item before modification
- NEW_AND_OLD_IMAGES - Both versions
Use DynamoDB Streams with Lambda to build real-time applications and event-driven architectures.
TTL (Time to Live)
Automatically delete expired items:
aws dynamodb update-time-to-live \
--table-name Orders \
--time-to-live-specification Enabled=true,AttributeName=expirationTimeTTL deletions don't consume WCUs and are eventually consistent (may take up to 48 hours).
Best Practices
Data Modeling
- Start with access patterns, not data structure
- Use composite keys for related data
- Denormalize when appropriate
- Use GSIs for alternative access patterns
Performance
- Distribute workload evenly across partitions
- Use Query instead of Scan
- Enable DAX for read-heavy workloads
- Use batch operations for multiple items
Cost Optimization
- Use on-demand for variable workloads
- Enable TTL to delete old data
- Project only needed attributes
- Use reserved capacity for predictable workloads
DAX (DynamoDB Accelerator)
In-memory cache for DynamoDB:
- Microsecond latency for reads
- No application changes needed (API compatible)
- Write-through caching
- Ideal for read-heavy workloads