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
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
Event processing
Event-driven patterns on AWS - Lambda with SQS, SQS FIFO and SNS and their dead-letter queues, the SNS fan-out, S3 event notifications and EventBridge, intercepting API calls with CloudTrail, and API Gateway writing straight to Kinesis.
Blocking an IP address
Where to block a client IP address on AWS - network ACLs, security groups, firewall software, an ALB or NLB, AWS WAF IP sets and CloudFront geo restriction - and why a NACL stops helping behind CloudFront.