Cache
Amazon ElastiCache (Valkey / Redis OSS / Memcached)
An in-memory key-value store that serves hot reads in under a millisecond.
In a monolith
In-process or one Redis
Starts as an in-memory LRU inside the app (lost on restart, different on every instance), then one shared Redis.
In a distributed system
Shared cache cluster
A Redis/Memcached cluster used cache-aside by services — and also for sessions, rate-limit counters, leaderboards and locks.
On AWS
Amazon ElastiCache (Valkey / Redis OSS / Memcached)
Managed Redis-compatible clusters with replicas and Multi-AZ failover. DAX is a separate write-through cache built specifically for DynamoDB.
- ElastiCache for Valkey / Redis OSS$$Data structures, replication, cluster-mode sharding
- ElastiCache Serverless$$No node sizing; scales automatically
- ElastiCache for Memcached$Simple and multi-threaded, no persistence or replication
- DynamoDB Accelerator (DAX)$$Read-through/write-through cache for DynamoDB only
- Reach for it when
- The same data is read far more often than it changes, and DB latency or load is the bottleneck.
- What it costs you
- Invalidation: data is stale until TTL expiry or an explicit delete
- Cache stampede when a hot key expires or the cache restarts cold
- Memory costs money; the eviction policy matters
- When it dies
- Requests fall through to the database. If the DB can’t take 100% of reads, the cache’s death becomes the DB’s death (stampede) — keep caches warm and coalesce requests.
Interview follow-ups
Cache-aside vs write-through vs write-behind?
Cache-aside: read the cache, on a miss load the DB and fill the cache. Write-through: writes update cache and DB together. Write-behind: write the cache, flush to the DB later (fast, risk of loss).
How do you prevent a cache stampede?
Request coalescing (single-flight), a lock while recomputing, jittered TTLs, early refresh of hot keys, and keeping the cache warm.
What TTL would you pick?
Whatever staleness the data allows: seconds for prices and stock, hours for descriptions — plus explicit invalidation on writes.
Connection rules
It talks to
- SQL databaseOnly when: Read-through / write-through caches that load from the DB themselves (e.g. DAX in front of DynamoDB).
- NoSQL storeOnly when: Read-through / write-through caches that load from the DB themselves (e.g. DAX in front of DynamoDB).
Gets traffic from
Never
- Clients → CacheA cache has no auth layer of its own and holds data shaped for your backend.
- CDN → CacheCDN origins are HTTP servers or buckets, never databases or caches.
- Load balancer → CacheA load balancer forwards HTTP to compute. Data access needs code: auth, queries, validation.
- API gateway → CacheA gateway routes to compute; data access needs your code.
- Queue → CacheQueues don’t run code. A consumer reads the message and does the write or call.
- Pub/sub topic → CacheTopics don’t run code. A subscriber receives the event and does the write.
- Event stream → CacheStreams don’t run code. A consumer reads the records and does the write or call.