Caching
Where to cache (CloudFront, API Gateway, ElastiCache, DAX), how to raise the CloudFront cache hit ratio, and how to make a cache layer fault tolerant.
Exam tasks: 2.5 (design for performance: caching strategy), 3.3 (improve the performance of an existing solution)
The decision: which layer should answer the request so the expensive thing behind it (origin, API, database) does less work, and what happens to users when that cache fails or goes stale?
The layers
| Layer | Caches | Best for | Watch out for |
|---|---|---|---|
| CloudFront | HTTP responses at edge locations | Static assets, cacheable API GETs, video | Cache key choices decide the hit ratio |
| API Gateway cache | Responses per stage and method | Repeated identical REST API calls | REST APIs only, billed per hour by size |
| ElastiCache | Anything your code puts in it | Database query results, sessions, leaderboards | Your code handles misses and invalidation |
| DAX | DynamoDB items and query results | Read-heavy DynamoDB with microsecond needs | Eventually consistent reads only |
| Aurora or RDS read replicas | Not a cache, but offload reads | Read-heavy SQL with complex queries | Replica lag |
CloudFront
Cache keys, policies, Origin Shield and TTL tuning now live on the Amazon CloudFront page.
Service pages
- Amazon ElastiCache: engines, Multi-AZ, Global Datastore and caching strategies.
- Amazon DynamoDB: DAX and global tables.
- Amazon CloudFront: cache keys and Origin Shield.
Scenarios
Cookies and irrelevant query strings split each article into thousands of cache variants. Keeping only 'edition' in the key collapses them, and the origin request policy still delivers the logging header without affecting the key. Origin Shield helps a little but doesn't fix a fragmented key. Adding the header to the key makes things worse. Frequent invalidations reduce hits.
Valkey supports replication with automatic failover, so sessions survive a node failure, and sorted sets suit a leaderboard. Global Datastore then gives low-latency reads in the other Regions. More Memcached nodes spread the data but don't replicate it. DAX only works with DynamoDB. Auto Discovery helps clients find nodes but adds no durability.
DAX is API-compatible with DynamoDB, so only the client changes, and it returns hot items in microseconds. A GSI doesn't cache anything. ElastiCache works but needs more code. Capacity mode changes throughput limits, not latency.
Related
- Purpose-built databases: DAX and ElastiCache sit in front of these.
- Performance and rightsizing: Finding the bottleneck before caching.
- DDoS and edge security: CloudFront as an edge shield.
- Amazon ElastiCache, DynamoDB and Amazon CloudFront: the dedicated service pages.
Further reading
Amazon ElastiCache
Valkey, Redis OSS and Memcached compared, replication and failover, Global Datastore, security, eviction and caching strategies.
Amazon S3
Storage classes and lifecycle, versioning, replication, Object Lock, encryption, performance, events and transfer options, with the limits the exam asks about.