Caching DynamoDB Reads with DynamoDB Accelerator (DAX)
This lesson explains how DynamoDB Accelerator (DAX), a fully managed in-memory cache for DynamoDB, reduces read latency from milliseconds to microseconds with almost no change to application code.
Learning outcomes
After completing this lesson, you will be able to:
- Explain how DAX works as a write-through cache in front of a table.
- Distinguish the item cache from the query cache.
- Identify the access patterns that DAX improves and those it does not, such as strongly consistent reads.
- Choose between DAX and Amazon ElastiCache for a caching requirement.
How DAX works
DAX is an in-memory, write-through cache in front of a table that cuts read latency from milliseconds to microseconds. Applications switch to the DAX client with almost no other code changes. It runs as a cluster inside your VPC, with an item cache and a query cache.
| DAX helps with | DAX does not help with |
|---|---|
| Read-heavy workloads needing microsecond latency | Strongly consistent reads, which pass through to DynamoDB |
| Repeated reads of the same items | Write throughput or throttled writes |
For caching computed or aggregated results rather than items, use ElastiCache instead.
Item cache and query cache
DAX maintains two separate caches, which hold the results of different operations. The following table describes the contents and default TTL of each.
| Cache | Holds results of | Default TTL |
|---|---|---|
| Item cache | GetItem and BatchGetItem | 5 minutes |
| Query cache | Query and Scan, keyed by their parameters | 5 minutes |
Writes go through DAX to DynamoDB and update the item cache, but they do not invalidate query cache entries, so a cached query can return results that are stale until its TTL expires. Applications that need fresh query results use a shorter query cache TTL.
Using DAX
DAX must be deployed and accessed in a particular way. The following list describes how applications use DAX.
- DAX runs as a cluster of nodes inside your VPC, so clients must be in, or connected to, that VPC, and the cluster's security group must allow them.
- Applications use the DAX client for their language, which has the same interface as the DynamoDB client, and point it at the cluster endpoint.
- A cluster with nodes in several Availability Zones stays available if a node fails.
- DAX supports eventually consistent reads only from its caches; strongly consistent reads and transactions pass through to DynamoDB.
from amazondax import AmazonDaxClient
dax = AmazonDaxClient.resource(endpoint_url="daxs://orders-cache.abc123.dax-clusters.eu-west-1.amazonaws.com")
table = dax.Table("Products")
item = table.get_item(Key={"productId": "p-981"})["Item"] # same call as with boto3DAX or ElastiCache?
DAX and ElastiCache both keep data in memory, but they are designed for different jobs. DAX is specific to DynamoDB: it understands the DynamoDB API, so an application replaces its DynamoDB client with the DAX client and gains microsecond reads of items and query results with almost no other code change. DAX caches only what DynamoDB returns.
ElastiCache is a general-purpose cache that the application manages itself. The code decides what to store and when, so it can cache anything: the result of a calculation that combines several sources, a rendered page, a session, or data from a relational database. ElastiCache for Valkey or Redis OSS also provides data structures, such as sorted sets for leaderboards and counters with atomic increments. Neither service speeds up writes: write throttling is solved with more capacity or a better partition key. The following table summarises the choice.
| Requirement | Choice |
|---|---|
| Microsecond reads of DynamoDB items with minimal code change | DAX |
| Caching results computed from several sources, or sessions | ElastiCache |
| Data structures such as sorted sets for leaderboards | ElastiCache for Valkey or Redis OSS |
| Faster writes or relief from write throttling | Neither: add capacity or redesign the keys |
DAX or not?
DAX improves performance only for certain access patterns. The following table assesses several situations and explains whether DAX would help in each.
| Situation | Does DAX help? | Why |
|---|---|---|
| A product catalogue read thousands of times per second during sales | Yes | Repeated eventually consistent reads are served from memory in microseconds |
| A banking balance that must always be current | No | Strongly consistent reads bypass DAX |
| A table throttling on writes during imports | No | DAX writes through to DynamoDB, so write capacity is unchanged |
| A Lambda function outside any VPC | Not without changes | DAX runs inside a VPC, so the function must be attached to it |
| A dashboard query whose results must reflect writes immediately | Only with a short query cache TTL | Writes do not invalidate the query cache |
DAX is the answer when reads dominate, slight staleness is acceptable and minimal code change is required; otherwise capacity changes or ElastiCache are better fits.
Does DAX speed up strongly consistent reads?
Show answer
No. DAX caches eventually consistent reads; strongly consistent reads and all writes pass through to DynamoDB. DAX helps read-heavy workloads that read the same items repeatedly.
Exam Tips
Remember for the exam
- Microsecond read latency with minimal code changes points to DAX; caching computed results points to ElastiCache.
- DAX does not help with write throughput or strongly consistent reads.
- DAX runs inside a VPC, so clients must be able to reach the cluster.
- Query cache entries are not invalidated by writes; lower the query cache TTL if results must be fresh.