Skip to Content

Caching strategies

The places to cache along a request on AWS - CloudFront, API Gateway, ElastiCache and DAX - and how TTL, network, computation, cost and latency change as the cache moves closer to the user.

Where to cache

Caching layers along a request: CloudFront caches at the edge, API Gateway caches responses, the application caches in ElastiCache or DAX in front of the database; caching near the user is faster and cheaper but may serve stale data, caching near the data is fresher but costs more work Caching layers along a request: CloudFront caches at the edge, API Gateway caches responses, the application caches in ElastiCache or DAX in front of the database; caching near the user is faster and cheaper but may serve stale data, caching near the data is fresher but costs more work

Every layer can answer before the database is asked.

A request travels from the client to the database. Each layer on the way can keep a copy of the answer:

  • CloudFront at the edge, closest to users, for static files and cacheable API responses. Static content in S3 can go straight from CloudFront to S3.
  • API Gateway caching, per stage, in the Region: whole responses for a TTL (default 300 seconds, up to 3,600).
  • Application caches: ElastiCache (Valkey, Redis OSS or Memcached) for any database or computed result, DAX for DynamoDB.
  • The database itself: read replicas spread the reads but aren't a cache.

The trade-offs

The further toward the user the cache sits, the bigger the savings, but the more care it needs:

  • Latency: an edge hit answers in milliseconds near the user; a database query may cross the world.
  • Cost and computation: every hit at an outer layer is a request the app servers and database don't pay for.
  • Network: data served from the edge doesn't travel from the origin Region.
  • TTL and freshness: a long TTL means more hits but staler data; dynamic or per-user data needs short TTLs or no cache. Invalidations (CloudFront) or write-through updates (ElastiCache) help.
  • Security: never cache per-user responses under a shared key.

Real-world examples

  • A sports results site caches the scoreboard API at CloudFront for 5 seconds: millions of fans, but the origin sees one request per edge location every 5 seconds.
  • A shop caches product pages for an hour at the edge, product data in ElastiCache for 10 minutes, and never caches the cart.

Exam tips

  • "Static content, global users, reduce load on the origin" → CloudFront.
  • "Same API responses requested repeatedly" → API Gateway caching (or CloudFront in front of the API).
  • "Database read load, sub-millisecond reads" → ElastiCache; "DynamoDB, microseconds, no code change" → DAX.
  • "Users see old data after an update" → shorter TTLs, CloudFront invalidations, or write-through caching.

Common traps

  • "Read replicas are a cache." They copy the whole database and still run queries; a cache stores answers.
  • "Cache everything at the edge." Per-user and fast-changing data must not be cached, or only very briefly.

AWS documentation

On this page

Open notes

Spotted a mistake?

These notes are open source. Suggest a fix, or star the repo if they helped you.

Suggest an edit

For AI agents: llms.txt · llms-full.txt