Volume 5 · Chapter 5 — Storage and Databases
Caching with ElastiCache
5.1 Introduction
Databases are often the bottleneck and the most expensive part of a system to scale, so putting a fast cache in front of them is one of the highest-leverage performance techniques. ElastiCache is AWS’s managed in-memory caching service (Redis and Memcached), delivering sub-millisecond reads for frequently-accessed data. Caching is a common interview topic because it directly addresses latency and database load, and because the patterns and pitfalls - invalidation, consistency, what to cache - reveal how well you understand performance trade-offs.
5.2 Why Cache
A cache stores frequently-accessed data in fast in-memory storage so repeat reads are served from memory instead of hitting the database. This does two valuable things: it dramatically reduces read latency (memory is far faster than a database query), and it offloads the database (fewer queries reach it, so it handles more effective load and you can provision less database capacity). For read-heavy workloads - a bank where the same account balances, product data, or reference information are read far more than they change - caching the hot data can cut database load by a large margin and speed responses significantly. ElastiCache provides this as a managed service, so you get in-memory performance without operating the cache infrastructure.
flowchart LR
App[Application] --> Cache{Cache hit?}
Cache -->|hit - sub-ms| Return[Return cached data]
Cache -->|miss| DB[(Database)]
DB --> Store[Store in cache with TTL] --> Return
Note[Frequently-read, rarely-changed data\nis the best cache candidate] -.-> Cache 5.3 Caching Patterns
The most common pattern is cache-aside (lazy loading): the application checks the cache first; on a hit it returns the cached value, and on a miss it reads from the database, stores the result in the cache (usually with a time-to-live), and returns it - so only the first request pays the database cost and subsequent ones are fast. Other patterns include write-through (write to the cache and database together to keep the cache current) and using TTLs so cached data automatically expires and refreshes. The right pattern depends on how fresh the data must be and how it’s written. Cache-aside is the default for read-heavy data that tolerates brief staleness; write-through or short TTLs suit data that must stay closely in sync.
5.4 Invalidation and Consistency
The hard part of caching is keeping cached data acceptably fresh - “there are only two hard things: cache invalidation and naming.” When underlying data changes, the cache can become stale, so you manage this with TTLs (data expires after a set time and is re-fetched), explicit invalidation (remove or update the cached entry when the source changes), or write-through (update the cache on write). The key design decision is how much staleness the data can tolerate: reference and rarely-changing data can cache with long TTLs safely, while frequently-changing or correctness-critical data needs short TTLs, active invalidation, or shouldn’t be cached at all. Crucially, authoritative correctness-critical values - like an account balance used to authorize a transaction - should come from the system of record, not a cache that might be stale.
5.5 What to Cache (and What Not To)
Good cache candidates are data that’s read frequently, changes infrequently, and tolerates brief staleness: reference data, product catalogues, configuration, computed results, and session data. Poor candidates are data that changes constantly (the cache is stale immediately and adds little) or where staleness is unacceptable for correctness. In banking, you might cache product and reference data, customer profile attributes, or precomputed views to speed the UI, while always reading the authoritative balance from the database when authorizing a payment. The judgment is to cache the hot, stable, staleness-tolerant reads for big performance wins, and to keep correctness-critical reads on the source of truth - getting this boundary right is what separates a cache that helps from one that causes subtle bugs.
5.6 Banking Production Scenario
A bank puts ElastiCache (Redis) in front of its database for read-heavy, stable data: product and reference information, customer profile attributes, and precomputed dashboard views are cached with appropriate TTLs using a cache-aside pattern, so the vast majority of these reads are served from memory in sub-milliseconds and the database load drops sharply. Session data lives in the cache for fast access. Critically, when authorizing a payment the bank reads the authoritative balance from the transactional database, never from the cache, because that value must be exactly correct - so caching accelerates the read-heavy, staleness- tolerant paths while the system of record remains the source of truth for correctness-critical decisions. The cache is a managed, replicated ElastiCache cluster, so it’s itself resilient.
5.7 Troubleshooting: Caching Problems
Cache issues are usually about freshness or the wrong data being cached. Users seeing stale data means the TTL is too long or invalidation isn’t happening on writes - shorten the TTL or invalidate on change. A correctness bug from caching an authoritative value (like a balance) means that data shouldn’t have been cached - read it from the source of truth. Low cache hit rates (little benefit) mean you’re caching data that isn’t reused, or TTLs are too short - cache genuinely hot, stable data. A “thundering herd” when many requests miss simultaneously and all hit the database can be mitigated with careful TTL staggering or pre-warming. Cache unavailability should degrade gracefully to the database, not fail. The discipline is to cache the right data (hot, stable, staleness-tolerant), tune TTL/invalidation to freshness needs, and never cache correctness-critical values that must be exact.
Why and how would you add caching with ElastiCache, and what should you be careful about?
I add caching because databases are often the bottleneck and the most expensive part to scale, and putting a fast in-memory cache in front of frequently-read data is one of the highest-leverage performance techniques. ElastiCache is AWS’s managed in-memory caching service (Redis or Memcached), delivering sub-millisecond reads. It helps in two ways: it dramatically reduces read latency because serving from memory is far faster than a database query, and it offloads the database because fewer queries reach it, so the database handles more effective load and you can provision less capacity. For a read-heavy workload - where the same data is read far more often than it changes - caching the hot data can cut database load substantially and speed responses. The most common way to implement it is the cache-aside (lazy loading) pattern: the application checks the cache first; on a hit it returns the cached value; on a miss it reads from the database, stores the result in the cache with a time-to-live, and returns it - so only the first request pays the database cost and the rest are fast. Alternatives include write-through, where you update the cache and database together to keep the cache current, and using TTLs so entries expire and refresh. What I’m careful about is mainly cache invalidation and consistency - keeping cached data acceptably fresh, which is famously one of the hard problems. When the underlying data changes the cache can go stale, so I manage that with TTLs, explicit invalidation on writes, or write-through, and the key decision is how much staleness the data tolerates: reference and rarely-changing data can cache safely with long TTLs, while frequently-changing data needs short TTLs or active invalidation. The most important caution is to never cache correctness-critical values that must be exact - an account balance used to authorize a payment must come from the system of record, not a cache that might be stale, because a wrong cached value there causes real errors. So I cache data that’s read frequently, changes infrequently, and tolerates brief staleness - reference data, profiles, configuration, computed views, sessions - for big latency and load wins, and I keep correctness-critical reads on the source of truth. I also make sure the cache is itself resilient and that the system degrades to the database if the cache is unavailable rather than failing. Getting the boundary right between what to cache and what to keep authoritative is exactly what separates a cache that improves performance from one that introduces subtle consistency bugs.
Chapter Revision Notes
- ElastiCache (Redis/Memcached) is managed in-memory caching with sub-millisecond reads - reduces latency and offloads the database.
- Cache-aside (lazy loading): check cache → on miss read DB, store with TTL, return; write-through and TTLs are alternatives.
- Invalidation/consistency is the hard part - use TTLs, explicit invalidation, or write-through based on staleness tolerance.
- Cache hot, stable, staleness-tolerant data (reference, profiles, config, sessions); don’t cache constantly-changing data.
- Never cache correctness-critical values (e.g., a balance authorizing a payment) - read those from the source of truth.