Amazon DynamoDB: Tables, Keys, Capacity Modes, Indexes, DAX, Global Tables and Streams
What Is DynamoDB?
DynamoDB is a fully managed, serverless NoSQL key-value and document database. It gives single-digit millisecond latency at any scale, multi-AZ replication automatically, and no servers to patch. You design tables around how you read the data.
| Concept | Meaning |
| Table | Collection of items. No fixed schema except the key |
| Item | One record (up to 400 KB) |
| Attribute | A field. Types: string, number, binary, boolean, null, list, map, sets |
| Primary key | Partition key alone, or partition key + sort key. Required and unique per item |
| Partition key | Hashed to decide which partition stores the item. Needs high cardinality to spread load |
| Sort key | Orders items with the same partition key and allows range queries |
Capacity Modes
| Mode | How it works | Best for |
| On-demand | Pay per request. Scales instantly with no planning | New tables, unpredictable or spiky traffic |
| Provisioned | You set RCUs and WCUs (can use auto scaling). Cheaper for steady traffic. Reserved capacity can discount further | Predictable, steady load |
| Unit | Equals |
| 1 RCU | One strongly consistent read per second of an item up to 4 KB (or two eventually consistent reads) |
| 1 WCU | One write per second of an item up to 1 KB |
| Transactional read/write | 2x the RCU/WCU |
text
Example: 100 strongly consistent reads/sec of 8 KB items
8 KB / 4 KB = 2 RCU per read → 100 x 2 = 200 RCU
eventually consistent: 200 / 2 = 100 RCU
Example: 50 writes/sec of 3 KB items
3 KB → 3 WCU per write → 50 x 3 = 150 WCU
- Throttling (
ProvisionedThroughputExceededException) happens when you exceed capacity or hit a hot partition. SDKs retry with backoff
- Adaptive capacity shifts capacity to busy partitions, but a bad key design is still the main cause of trouble
Read Consistency
| Type | Behavior |
| Eventually consistent (default) | May return slightly old data, half the cost, higher availability |
| Strongly consistent | Returns the latest write. Costs more and only in the same Region. Not supported on global secondary indexes |
| Transactional | ACID across up to 100 items (TransactWriteItems, TransactGetItems) |
Create and Use a Table
bash
aws dynamodb create-table --table-name Orders \
--attribute-definitions AttributeName=customerId,AttributeType=S AttributeName=orderDate,AttributeType=S \
--key-schema AttributeName=customerId,KeyType=HASH AttributeName=orderDate,KeyType=RANGE \
--billing-mode PAY_PER_REQUEST \
--sse-specification Enabled=true,SSEType=KMS --deletion-protection-enabled
aws dynamodb put-item --table-name Orders --item '{"customerId":{"S":"C100"},"orderDate":{"S":"2026-11-17T10:00:00Z"},"total":{"N":"499.5"},"status":{"S":"NEW"}}'
aws dynamodb query --table-name Orders --key-condition-expression "customerId = :c AND orderDate BETWEEN :a AND :b" \
--expression-attribute-values '{":c":{"S":"C100"},":a":{"S":"2026-11-01"},":b":{"S":"2026-11-30"}}'
python
import boto3
from boto3.dynamodb.conditions import Key
table = boto3.resource("dynamodb").Table("Orders")
table.put_item(Item={"customerId": "C100", "orderDate": "2026-11-18T09:00:00Z", "total": 120, "status": "NEW"})
resp = table.query(KeyConditionExpression=Key("customerId").eq("C100") & Key("orderDate").begins_with("2026-11"))
for item in resp["Items"]:
print(item)
Query vs Scan
| Query | Scan |
| Reads | Items with a given partition key (and optional sort key condition) | Every item in the table or index |
| Speed and cost | Fast and cheap | Slow and expensive on big tables |
| Use | Normal access | Rare tasks, small tables, exports (prefer export to S3) |
Indexes
| Local Secondary Index (LSI) | Global Secondary Index (GSI) |
| Keys | Same partition key, different sort key | Different partition key and optional sort key |
| When created | Only at table creation | Any time |
| Capacity | Shares the table's capacity | Has its own capacity |
| Consistency | Strong or eventual | Eventual only |
| Limit | 5 per table, 10 GB limit per partition key value | 20 per table by default |
- Use a GSI to query by another attribute, for example
orderId or status
- Use sparse indexes (only items with the attribute appear) to keep them small
TTL, Streams, Backups
- TTL: set an attribute with an epoch time; DynamoDB deletes expired items for free in the background (often within hours). Great for sessions and temporary data
- DynamoDB Streams: ordered, 24-hour change log of item changes. Trigger Lambda for audits, search indexing, replication. Also Kinesis Data Streams integration
- Point-in-time recovery (PITR): restore to any second in the last 35 days, to a new table
- On-demand backups and AWS Backup, export to S3 and import from S3 without using table capacity
DAX: DynamoDB Accelerator
- A managed in-memory cache cluster in your VPC, API-compatible with DynamoDB
- Microsecond reads for read-heavy or hot-key workloads
- Item cache and query cache. Write-through
- Not for strongly consistent reads (those pass through) or write-heavy tables
- Alternatives: ElastiCache for general caching of computed results
Global Tables
- Multi-Region, multi-active replication: write in any Region, changes replicate in about a second
- Gives low-latency local access and multi-Region resilience
- Conflict resolution: last writer wins
- Requires DynamoDB Streams (new and old images). Use with Route 53 or Global Accelerator for routing
Data Modeling Rules
- Start from access patterns (list every query), not entities
- Choose a high-cardinality partition key (user ID, order ID), not status or date alone
- Use sort keys to group related items (
ORDER#2026-11-17) and enable range queries
- Use the single-table design carefully for related data, or separate tables if simpler
- Use GSIs for extra access patterns
- Keep items small. Store big objects in S3 and keep the pointer
- No joins: denormalize and duplicate where needed
| DynamoDB is a good fit | Prefer something else when |
| Shopping carts, sessions, user profiles, game state, IoT, leaderboards | You need complex joins, ad hoc SQL, or heavy analytics (use Aurora, Redshift, Athena) |
| Very high scale with predictable latency, serverless apps | You cannot define access patterns up front |
Practice Tasks
- Create a table with partition key
customerId and sort key orderDate, insert items and query by date range
- Add a GSI on
status and query it
- Turn on TTL with an
expiresAt attribute
- Enable a stream and trigger a Lambda that logs changes
- Calculate RCUs for 200 eventually consistent reads per second of 6 KB items
Conclusion
DynamoDB is a serverless key-value database with partition and sort keys, on-demand or provisioned capacity, GSIs for new access patterns, TTL, streams, PITR, DAX for microsecond reads and global tables for multi-Region active-active. Design tables from access patterns and avoid scans.
Quick Summary: keys; capacity modes + RCU/WCU math; consistency; CLI + boto3; query vs scan; LSI vs GSI; TTL/streams/PITR; DAX; global tables; modeling rules.