Lesson 8 of 10 Amazon DynamoDB

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 withDAX does not help with
Read-heavy workloads needing microsecond latencyStrongly consistent reads, which pass through to DynamoDB
Repeated reads of the same itemsWrite 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.

CacheHolds results ofDefault TTL
Item cacheGetItem and BatchGetItem5 minutes
Query cacheQuery and Scan, keyed by their parameters5 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 boto3

DAX 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.

RequirementChoice
Microsecond reads of DynamoDB items with minimal code changeDAX
Caching results computed from several sources, or sessionsElastiCache
Data structures such as sorted sets for leaderboardsElastiCache for Valkey or Redis OSS
Faster writes or relief from write throttlingNeither: add capacity or redesign the keys
Worked example

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.

SituationDoes DAX help?Why
A product catalogue read thousands of times per second during salesYesRepeated eventually consistent reads are served from memory in microseconds
A banking balance that must always be currentNoStrongly consistent reads bypass DAX
A table throttling on writes during importsNoDAX writes through to DynamoDB, so write capacity is unchanged
A Lambda function outside any VPCNot without changesDAX runs inside a VPC, so the function must be attached to it
A dashboard query whose results must reflect writes immediatelyOnly with a short query cache TTLWrites 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.

Check yourself

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.