Amazon DynamoDB: Keys, Indexes and Data Modelling
Amazon DynamoDB is a fully managed, serverless key-value and document database that delivers single-digit-millisecond performance at any scale. There are no servers, no patching and no connection pools – you call an API.
Data model
| Concept | Meaning |
|---|---|
| Table | Collection of items |
| Item | A record – up to 400 KB; items can have different attributes |
| Attribute | Typed value: String, Number, Binary, Boolean, List, Map, Set |
| Partition key (PK) | Hashed to choose the storage partition – must be in every item |
| Sort key (SK) | Optional; orders items with the same PK and enables range queries |
| Primary key | PK alone (simple) or PK + SK (composite) – must be unique |
flowchart LR
subgraph P1["Partition for customerId = C1"]
a["orderDate 2026-11-01 · DELIVERED"] --> b["2026-11-15 · SHIPPED"] --> c["2026-11-17 · PENDING"]
end
subgraph P2["Partition for customerId = C2"]
d["2026-11-10 · PENDING"]
end
Reading data
| Operation | Reads | Cost / speed |
|---|---|---|
| GetItem | One item by full primary key | Cheapest, fastest |
| Query | Items with one PK, optional SK condition (=, <, between, begins_with) |
Reads only matching items |
| Scan | Every item in the table, then filters | Slow and costly on big tables – avoid in hot paths |
A FilterExpression is applied after reading, so it reduces the response, not the capacity consumed.
Secondary indexes
| Index | Keys | Notes |
|---|---|---|
| GSI (global) | Any PK + optional SK | Separate capacity, eventually consistent, add any time |
| LSI (local) | Same PK, different SK | Must be created with the table |
Example: table CustomerOrders (PK customerId, SK orderDate) answers “orders of a customer”; a GSI (PK status, SK orderDate) answers “all PENDING orders, newest first”.
Model for access patterns
- List the queries first (who reads what, how often).
- Choose keys so each frequent query is a GetItem or Query.
- Add GSIs for other patterns; consider single-table design for related entities.
- Pick high-cardinality partition keys to avoid hot partitions.
Writes and features
| Feature | Use |
|---|---|
PutItem, UpdateItem (SET, ADD, REMOVE), DeleteItem |
Write operations |
| Condition expressions | Optimistic locking, “create only if not exists” (attribute_not_exists(pk)) |
| Transactions | All-or-nothing across up to 100 items |
| Capacity | On-demand (pay per request) or provisioned RCU/WCU with auto scaling |
| TTL | Delete expired items automatically |
| Streams | Change feed → Lambda (event-driven) |
| PITR, backups, global tables | Recovery and multi-Region active-active |
| DAX | In-memory cache, microseconds |
await db.send(new QueryCommand({
TableName: 'CustomerOrders',
KeyConditionExpression: 'customerId = :c AND orderDate > :d',
ExpressionAttributeValues: { ':c': 'C1', ':d': '2026-11-10' },
ScanIndexForward: false,
}));
In DynamoDB you design for your queries, not for normalisation. Query by key, avoid Scan, and add a GSI for each extra access pattern.
Practise it
Virtual lab · Amazon Web Services · 30 min
Model data in DynamoDB: partition and sort keys, Query vs Scan, and a GSI
Design an Orders table around its access patterns: composite primary key, items with flexible attributes, efficient Query versus expensive Scan, and a global secondary index for a second access pattern.
Virtual lab · Amazon Web Services · 45 min
Build a serverless REST API: API Gateway + Lambda + DynamoDB
Build the most common serverless backend in the industry: a Products API with an HTTP API front door, a Lambda function using the AWS SDK v3, and a DynamoDB table – and fix a real AccessDenied error with least-privilege IAM.
Quick revision
- Items are schemaless except for the primary key.
- PK decides the partition; SK orders and enables range queries.
- GetItem/Query read by key; Scan reads everything.
- GSIs add access patterns; LSIs must be created up front.
- On-demand capacity, conditional writes, TTL and Streams.
Practise this topic
Hands-on virtual labs
Apply what you just read in a realistic, guided simulation. Free with a verified account.
Create a free account to track your progress and unlock virtual labs.