🗄️ DynamoDB Optimization
DynamoDB is AWS's fully managed NoSQL database. For optimization in Domain 4, the key topic is DynamoDB Streams — which captures every change to your table in real time and enables powerful event-driven patterns.
🔑 Covers: DynamoDB Streams · Event-Driven Processing · Data Consistency · Change CaptureDynamoDB Streams — Capture Every Change
DynamoDB Streams is a feature that captures a time-ordered sequence of every item-level modification (insert, update, delete) that happens in a DynamoDB table. Each change creates a "stream record" that describes what changed.
📋 Stream Record View Types — What Data is Captured
| View Type | What's in the stream record | Use when |
|---|---|---|
| KEYS_ONLY | Only the key attributes of the modified item | You just need to know what changed, not the values |
| NEW_IMAGE | The entire item as it appears AFTER the modification | You want the current state of changed items |
| OLD_IMAGE | The entire item as it appeared BEFORE the modification | You want to know what was deleted/overwritten |
| NEW_AND_OLD_IMAGES | Both the old and new versions of the item | You want to compute what changed (diff the before/after) |
🎯 Use Cases for DynamoDB Streams
📧 Downstream Notifications
When a new order is added to DynamoDB → Stream → Lambda → Send confirmation email via SES. Lambda is triggered automatically on every new item.
🔍 Search Index Sync
Keep an OpenSearch index in sync with DynamoDB. Every change to DynamoDB → Stream → Lambda → Update OpenSearch. Users can then search DynamoDB data using full-text search.
📊 Aggregation and Analytics
Compute aggregates as data changes. Example: when items are inserted, update a running total. Maintains a separate "summary table" that's always up to date.
🔄 Cross-Region Replication
Stream changes → Lambda → Write to DynamoDB in another region. Keep two tables in sync. (Today, Global Tables do this automatically — but streams enable custom replication logic.)
Answer: Enable DynamoDB Streams + Lambda trigger. Streams capture EVERY modification reliably and trigger Lambda for each change. The trigger ensures zero missed events (unlike polling).
"Missing updates/events" from DynamoDB → the answer is DynamoDB Streams.
DynamoDB DAX — In-Memory Caching
🗄️ DynamoDB Direct (Reads)
- Single-digit milliseconds per read
- Scales to millions of requests/sec but has cost at scale
- Every read hits the DynamoDB storage nodes
⚡ DynamoDB + DAX (Reads)
- Microseconds per cached read
- Reduces DynamoDB read traffic by 90%+
- Ideal for read-heavy, repetitive access patterns (gaming leaderboards, product catalog)
Data Consistency with Streams
DynamoDB Streams provides at-least-once delivery — each stream record is delivered to consumers at least once. Your Lambda function must be idempotent (handle the same record twice without side effects).