Caching & CDNs · Lesson 4 of 6 · 4 min read
Application caching with Redis and Valkey
Caching patterns
| Pattern | How it works | Trade-off |
|---|---|---|
| Cache-aside (lazy loading) | App reads the cache; on a miss, reads the database and writes the result to the cache with a TTL | Simple and the most common; first request is slow; data can be stale until TTL |
| Read-through | The cache library loads from the database itself on a miss | Same as cache-aside, logic in one place |
| Write-through | Every write goes to the cache and the database together | Cache always fresh; writes are slower; caches data that may never be read |
| Write-behind (write-back) | Write to the cache, flush to the database later | Fast writes; risk of losing data if the cache fails |
On update, the safer default is to write the database, then delete the cache key (not update it), so the next read reloads the current value. Updating the cache directly can race with a concurrent read and leave an old value cached.
Enlarge diagram
Swipe sideways to see all of it, or tap to open it full screen.
def get_product(pid):
key = f"product:{pid}"
if (hit := r.get(key)) is not None:
return json.loads(hit)
row = db.fetch_product(pid)
r.set(key, json.dumps(row), ex=300 + random.randint(0, 60)) # TTL with jitter
return row
Memory, eviction and TTLs
Redis keeps everything in memory. When it reaches maxmemory, the maxmemory-policy decides what happens:
allkeys-lru/allkeys-lfu: evict the least recently / least frequently used keys. The right choice for a pure cache.volatile-lru/volatile-ttl: evict only keys that have a TTL.noeviction(the open-source default): refuse writes with an error. Right for data you can't lose, wrong for a cache.
Always set TTLs on cache entries, add random jitter so keys created together don't all expire together, and watch evicted_keys, hit rate (keyspace_hits / keyspace_misses), memory fragmentation and latency.
Running it on AWS
- ElastiCache runs Redis OSS, Valkey (the Linux Foundation fork created after Redis changed its licence in 2024) or Memcached. AWS prices Valkey lower than Redis OSS, so it is the usual choice for new clusters.
- Cluster mode shards keys across several primaries (16,384 hash slots); each shard has replicas in other AZs, and a failed primary is replaced by automatic failover in seconds.
- ElastiCache Serverless scales capacity automatically and bills per use.
- MemoryDB is a durable, Redis-compatible database (multi-AZ transaction log), for when the "cache" is really the primary store.
- Memcached is simpler (no replication or persistence, multi-threaded) and fine for a pure, disposable cache.
- DAX is an in-memory cache in front of DynamoDB that needs no code changes beyond the client.
Keep caches in private subnets, require TLS and authentication (AUTH token or IAM), and treat them as disposable: the system must survive a full cache flush, just more slowly.