Cache-Aside Pattern 00:00:01 The second request takes a shorter route An account page opens twice. The first request has to find a profile in its database. The second may never touch that database. Follow the profile identifier through each trip and ask who owns the durable email address. The app is asked for the key user:123. The key is not there. The database sends the user's row. The app keeps a copy in Redis for 300 seconds. Somebody asks for user:123 again. The copy is returned. The database is not involved. A hit reused a replaceable copy. Counting hits alone misses the work paid by the first reader and the cost when many copies disappear at once. 00:00:46 One write, two places that can disagree The user edits an email address while an old profile is still cached. The application commits the authoritative update and then invalidates the copy. Ask what happens if it stops between those operations. The user changes their email. Redis still holds the old row. The new email is committed. The old copy is removed. A miss brings the new email in. The stale copy lives until the TTL ends, unless the delete is retried from a queue or the change log. Give that failed invalidation its own recovery path. A time limit bounds the lifetime of this stored copy, but the next reader can still see old data before it expires. 00:01:30 An old read returns late Here is the less obvious race. A reader begins fetching an old value before another server writes the new value. Completing the database write and deleting the cache entry does not stop that first reader finishing late. Server A finds nothing in Redis. A fetches the old price, ten, but stalls before it can fill the missing cache entry. B commits the new price, twelve. There is no cached value to delete. A lease-aware cache can still cancel the earlier permission to fill. There is nothing to delete yet. In the lease version, A's lease is cancelled here. Without protection the old price is now cached. Redis refuses the set because the lease was cancelled. The next reader fetches 12. A fill needs evidence it still has the right to populate this key. That is the purpose of the lease in this example, rather than an assumption that every network arrival is in write order. 00:02:26 Remember a missing profile briefly Somebody repeatedly requests an identifier that does not exist. A cache that only stores real profiles keeps sending the same unsuccessful lookup to its origin. Can an absence itself be cached? user:999 is asked for. It does not exist. Nothing is stored, so each request becomes a query. A small marker says: we looked, there is no such user. The marker answers for 30 seconds. Give a negative entry a deliberately short lifetime and invalidate it when the profile is created. Otherwise a genuine new account can look absent to its first visitors. 00:03:05 Remove the cache in rehearsal Now disconnect Redis while the account page is busy. The original data still exists. The unanswered question is whether the database can accept a workload that usually disappears into memory. Calls fail or time out after 50 ms. If capacity permits, the application falls back to database reads. A miss now costs a slower trip and extra origin work. Only as many reads as the database can serve are let through. It is empty. Missed keys fill it again, hottest first. Limit the traffic allowed through, observe origin pressure and rebuild hot copies under control. A cache-aside design earns the word replaceable only when removing it leaves a recovery path.