Introduction to Caching 00:00:01 One profile, a thousand visits A reader asks for profile forty-two. The database has the authoritative profile; the cache starts empty. Follow the first request separately from the next nine hundred ninety-nine requests. A reader wants profile 42. The app looks in Redis first. Nothing is there, so the app reads the database. This is the slow part. The row is stored in Redis for 300 seconds, then the reader gets it. The next reader gets the copy. The database does nothing. With one miss, 999 of 1,000 reads were hits: a hit rate of 99.9% for this key. A hit rate describes a measured mix of requests. A single hot profile can make the fleet average look excellent while rare, expensive queries still overwhelm the origin. 00:00:57 Which layer actually answered? A logo may already be on the reader's device. If the device misses, another shared delivery layer may answer before the application is contacted. Count which network boundary each hit avoids. The reader's own device has the logo. Nothing leaves it. Another reader nearby asked for the same public page. The edge answers. A list of countries sits inside the app server. Each server has its own copy. The profile is in the shared cache, one network trip from every server. Only what no layer has reaches the original. Place invalidation and access rules at the boundaries where copies really exist. Purging the application's cache cannot reach data that a browser has already retained. 00:01:43 A price changes behind the copy A catalog editor changes a price while shoppers are still opening product pages. Track the authoritative price and the cached copy as two distinct values throughout the update. Both places say the price is 10. The database now says 12. For a moment Redis is stale. The key is removed. A delete can be repeated safely. The next reader misses and stores 12. Deleting a stale key enables a later miss to refill from the source. Concurrency and propagation can leave a stale interval, so specify which decisions can tolerate it. 00:02:19 Nine percentage points change the database load Suppose the shop receives ten thousand reads every second and the cache handles most of them. Estimate the origin rate at ninety percent hits before comparing it with ninety-nine percent. All 10,000 reads a second reach a database that can do 2,000 (assumed). 1,000 misses a second. The database copes. Only 100 a second. Nine more points of hit rate removed 90% of the remaining reads. Size the origin for residual misses, their burstiness and their cost. A hit-rate improvement matters through the traffic it actually removes from the constrained dependency. 00:02:58 Reopen the shop after an empty restart An empty cache turns yesterday's hot hits into today's compulsory misses. Forecast the first minute of database traffic using the cold state, rather than the healthy average on yesterday's dashboard. 99% of reads are hits. The database has been sized for 100 reads a second, with some room. Every read falls through to the database. The app lets only as many misses through as the database can serve. The rest get an old copy or a short error. The hot keys come back first, because they are asked for first. Admit work gradually or refresh known hot values within origin capacity. The shop now has a plan for the first profile read, the warm day and the morning every copy disappears.