Problem
Database reads dominate load and latency, yet most reads target a small set of hot records. Putting the cache in the data path couples it to the store, while the application already knows what it needs.
Also known as: lazy-loading-cache, look-aside-cache
Let the application load data into a cache on demand, reading the cache first and the database on a miss.
Database reads dominate load and latency, yet most reads target a small set of hot records. Putting the cache in the data path couples it to the store, while the application already knows what it needs.
Cache-aside, also called lazy loading or look-aside, keeps the cache outside the data path. The application owns the logic: read the cache, fall back to the database, and populate the cache itself.
read(key):
v = cache.get(key)
if v is not null: return v
v = db.query(key)
cache.set(key, v, ttl=300)
return v
write(key, value):
db.update(key, value)
cache.delete(key)
Writers update the database first and then delete the key. Updating the cache value instead invites a race: two writers can commit to the database in one order and to the cache in the other, leaving the cache wrong until the TTL expires. Deleting makes the next reader load whatever the database holds.
Cache negative results too, with a short TTL, so lookups for missing keys do not hit the database repeatedly. Add jitter to TTLs so keys loaded together do not expire together.
Cache cluster memory
Low: monitor hit ratio and evictions
Low: well-known protocol
Race between a reader repopulating and a writer invalidating leaves stale data
Cache outage sends full traffic to the database
Cold cache after restart causes a load spike