Module Review: Optimization
[!NOTE] This module explores the core principles of Module Review: Optimization, deriving solutions from first principles and hardware constraints to build world-class, production-ready expertise.
1. Key Takeaways
- Capacity Modes: Use Provisioned for steady, predictable traffic to save money. Use On-Demand for spiky, unpredictable workloads to avoid throttling.
- DAX: A Write-Through cache that reduces read latency to microseconds. Best for read-heavy apps.
- Transactions: ACID guarantees across multiple items using
TransactWriteItems. Costs 2x RCUs/WCUs. - TTL: Free background deletion of expired items. Guaranteed to delete within 48 hours. Use Streams to archive deleted data.
- Cost: Short attribute names save storage. Eventual Consistency saves 50% on reads. Projections save bandwidth.
2. Cheat Sheet
| Feature | Description | Cost Impact |
|---|---|---|
| Provisioned Mode | You specify RCUs/WCUs. Auto Scaling adjusts it. | Pay for what you reserve. |
| On-Demand Mode | No config. Scales instantly to thousands of req/s. | Pay per request (Higher unit cost). |
| TransactWrite | Atomic updates across up to 100 items. | 2x WCU cost. |
| DAX | In-memory cache. Microsecond reads. | Hourly cluster cost. Reduces RCU usage. |
| TTL | Auto-delete expired items based on timestamp. | Free deletion. |
| Eventual Read | Reads that might be stale (<1s). | 50% Discount (0.5 RCU). |
| Strong Read | Reads that reflect latest write. | Standard Cost (1.0 RCU). |
3. Flashcards
Test your knowledge of DynamoDB optimization.
Provisioned vs On-Demand
Which capacity mode is cheaper for a workload that has 0 traffic for 12 hours and massive spikes for 2 hours?
On-Demand
Because you only pay for active requests. Provisioned mode would require you to pay for capacity during the idle 12 hours (unless you scale to zero, which is slow/risky) and might throttle during the massive spikes before Auto Scaling catches up.
Transaction Cost
If a standard PutItem costs 1 WCU, how much does a TransactWriteItem for the same item cost?
2 WCUs
ACID transactions consume double the capacity units compared to standard eventual or strong consistency operations to account for the two-phase commit protocol overhead.
DAX Consistency
If you write an item to DAX and immediately read it back from DAX, is it consistent?
Yes (mostly)
Because DAX is a Write-Through cache, the item is updated in the cache as it is written to DB. However, DAX is strictly Eventual Consistency when reading from other nodes in the cluster or if the item was evicted.
TTL Timing
You set an item's TTL to expire at 10:00 AM. It is now 10:05 AM. Is the item guaranteed to be gone?
No
DynamoDB guarantees deletion within 48 hours of expiration. The item might still be visible in queries unless you filter it out manually in your application.
Attribute Names
Why does renaming "customer_shipping_address" to "addr" save money?
Storage & WCU Savings
DynamoDB stores the attribute name with every single item. Saving 20 bytes per item across 1 billion items saves ~20GB of storage and reduces the WCU cost for every write.
4. What’s Next?
You have completed the DynamoDB Optimization module!