8.2 Caching Strategies and ElastiCache Patterns

Key Takeaways

  • Lazy Loading (Cache-Aside) reads from cache first; on a miss, reads from the database, writes to cache, then returns — simple but causes initial cold-start latency.
  • Write-Through writes to cache and database simultaneously — data is always up-to-date in cache but adds write latency and may cache unused data.
  • TTL (Time-to-Live) helps balance between stale data and cache misses — set TTL based on how frequently your data changes.
  • ElastiCache for Redis supports complex data structures, persistence, replication, and pub/sub; Memcached is simpler and multi-threaded for basic caching.
  • Session store pattern: store user sessions in ElastiCache Redis to enable stateless application servers that can scale horizontally.
Last updated: March 2026

Caching Strategies and ElastiCache Patterns

Quick Answer: Lazy Loading = cache-aside (read from cache, on miss read DB + fill cache). Write-Through = write to cache + DB simultaneously. TTL = auto-expire cached data. Use Redis for rich features + persistence. Use Memcached for simple, high-throughput caching. Session stores in Redis enable stateless horizontal scaling.

Caching Strategies

Lazy Loading (Cache-Aside)

How it works:

  1. Application checks cache first
  2. Cache hit: Return cached data (fast)
  3. Cache miss: Read from database → write result to cache → return data
AdvantageDisadvantage
Only requested data is cachedCache miss = slower (extra round trip)
Cache failure does not break the applicationStale data possible (no automatic updates)
Simple to implementCold start: first request always misses

Write-Through

How it works:

  1. Application writes to cache AND database simultaneously
  2. Every read hits the cache (always up-to-date)
AdvantageDisadvantage
Data in cache is never staleEvery write has extra cache write overhead
Reads are always fast (cache hit)Unused data may consume cache space
Cache failure means write failure (if synchronous)

Combined Strategy (Recommended)

Use Lazy Loading + Write-Through + TTL together:

  • Write-Through keeps actively written data up-to-date
  • Lazy Loading fills the cache for reads
  • TTL expires data that has not been updated, preventing unlimited cache growth

TTL (Time-to-Live) Considerations

Data Change FrequencyRecommended TTL
Real-time (stock prices)Seconds (5-30)
Frequently changing (product inventory)Minutes (1-15)
Slowly changing (user profiles)Hours (1-24)
Rarely changing (static content)Days (1-7)

Session Store Pattern

Store user sessions in ElastiCache instead of on application servers:

Without ElastiCache:

  • Sessions stored on individual app servers
  • Sticky sessions required (ALB ties user to specific server)
  • Losing a server loses all sessions on it

With ElastiCache (Redis):

  • Sessions stored in centralized Redis cache
  • Any app server can serve any user
  • Losing an app server does not lose sessions
  • App servers are stateless → scale freely with Auto Scaling

On the Exam: "Make application servers stateless for horizontal scaling" → Store sessions in ElastiCache Redis (or DynamoDB). "Reduce database read latency" → Add ElastiCache with Lazy Loading.

Redis vs. Memcached Decision

Choose Redis WhenChoose Memcached When
Need data persistenceSimple key-value caching only
Need Multi-AZ failoverMaximum simplicity
Need complex data types (lists, sets, sorted sets)Multi-threaded performance needed
Need pub/sub messagingNo replication/failover needed
Need sorted sets for leaderboardsPure caching (no persistence)
Need geospatial data support
Need Lua scripting

Cache Invalidation

MethodHowUse Case
TTLCache entries auto-expireGeneral (most common)
Explicit deleteApplication deletes cache key on updateWhen data changes are known
Event-drivenDynamoDB Streams / SNS triggers cache updateReal-time consistency
Test Your Knowledge

An application has a read-heavy database workload. The team wants to add caching but is concerned about serving stale data. Which caching strategy minimizes stale data?

A
B
C
D
Test Your Knowledge

A web application needs to scale horizontally with Auto Scaling, but users are losing their sessions when they are routed to different servers. How should this be fixed?

A
B
C
D