Core concepts
- Partition key (PK) → hashed to a partition. Sort key (SK) orders items within a PK.
- Item ≤ 400 KB. Partition limits: ~3000 RCU / 1000 WCU per partition.
Query= one PK (+ SK condition), efficient.Scan= whole table, avoid.- Design from access patterns first, not entities.
Capacity
| Mode | Use |
|---|---|
| On-demand | unpredictable/spiky, pay per request |
| Provisioned + auto scaling | steady, cheaper at scale |
- 1 RCU = 1 strongly consistent read/s of 4 KB (or 2 eventually consistent).
- 1 WCU = 1 write/s of 1 KB. Transactions cost 2x.
- Hot partitions: skewed PKs throttle. Fix with high-cardinality keys or write sharding (
PK#rand(0..9)).
Indexes
| GSI | LSI | |
|---|---|---|
| Keys | any PK + SK | same PK, different SK |
| When | anytime | table creation only |
| Consistency | eventual only | strong or eventual |
| Limit | 20 per table | 5; 10 GB per PK |
Sparse index: only items with the GSI attribute appear in it, great for "status = pending" lists.
Single-table design
| PK | SK | Data |
|---|---|---|
USER#123 | PROFILE | name, email |
USER#123 | ORDER#2024-05-01#987 | total, status |
ORDER#987 | ITEM#1 | sku, qty |
- Query
PK = USER#123 AND begins_with(SK, 'ORDER#')→ user's orders, sorted by date. - Overload GSIs (
GSI1PK,GSI1SK) for inverted/alternate patterns.
SDK v3 (JS)
import { DynamoDBDocumentClient, QueryCommand, PutCommand, UpdateCommand } from '@aws-sdk/lib-dynamodb';
const ddb = DynamoDBDocumentClient.from(new DynamoDBClient({}));
await ddb.send(new QueryCommand({
TableName: 'app',
KeyConditionExpression: 'PK = :pk AND begins_with(SK, :sk)',
ExpressionAttributeValues: { ':pk': 'USER#123', ':sk': 'ORDER#' },
ScanIndexForward: false, Limit: 20,
}));
// idempotent create
await ddb.send(new PutCommand({ TableName: 'app', Item: item,
ConditionExpression: 'attribute_not_exists(PK)' }));
// atomic counter + optimistic locking
await ddb.send(new UpdateCommand({ TableName: 'app', Key: { PK, SK },
UpdateExpression: 'SET #c = #c + :one, version = :v2',
ConditionExpression: 'version = :v1',
ExpressionAttributeNames: { '#c': 'count' },
ExpressionAttributeValues: { ':one': 1, ':v1': 3, ':v2': 4 } }));Features
- Streams → Lambda for CDC, event publishing, aggregations (24 h retention).
- TTL attribute auto-deletes expired items (free, within ~48 h).
- Transactions:
TransactWriteItemsup to 100 items, all-or-nothing. - PITR backups, global tables (multi-region active-active), DAX cache (microsecond reads).
- Pagination via
LastEvaluatedKey→ExclusiveStartKey; 1 MB per page.
Performance tips
- Use
ProjectionExpressionto fetch fewer attributes. BatchGetItem(100) /BatchWriteItem(25); retryUnprocessedItems.- Avoid large items; store blobs in S3 with a pointer.
- Monitor
ThrottledRequests,ConsumedReadCapacityUnits, hot keys via Contributor Insights.
DynamoDB vs relational
Pick DynamoDB for known access patterns, massive scale, predictable single-digit ms latency. Pick Postgres for ad-hoc queries, joins, complex transactions and evolving access patterns.