API Caching 00:00:01 The second public read can be cheaper Two people request the same catalogue page. A shared cache could save the shop a database read. Two people request their private account pages. Reusing a response across that boundary would be a serious privacy failure. The browser and the CDN have no copy, so the request goes on. The service looks in Redis and finds nothing. Only now does the query run. This is the slow part. Each layer keeps the answer for 60 seconds. The browser has a fresh copy. Nothing leaves the device. Their browser is empty, but the CDN already has the answer. A miss fetches a representation from its authority. A cache hit can avoid that trip, but only when the cache key includes every response variation the service promises to distinguish. 00:00:52 Check whether a stored response changed A stale copy is checked instead of fetched again. The response carries ETag abc123 and max-age 60. After 60 seconds the browser may not use it without asking. The request says If-None-Match abc123. The service reads only the version. No body is sent. The stored copy is fresh for another 60 seconds. The price changed, so the version is def456. The answer is 200 with the new body. A validator lets a client ask whether it may keep its stored representation. A not-modified response saves retransmitting the body while still paying a validation trip. 00:01:36 What a longer lifetime buys Three entries with different lifetimes. Request them, let time pass, invalidate one. The product list is new, one product is three minutes from its end, the categories are almost over. 300 seconds left, so it is a hit. Also a hit: its counter goes from 3 to 4. A miss. They are loaded, stored for 3600 seconds, and their counter starts at 0. An editor changed it, so its copy is dropped with 120 seconds still to go. A miss, then 600 seconds of fresh answers. A longer lifetime can increase reuse and staleness together. Popularity and write frequency influence both sides, so a lifetime chosen for a catalogue photograph need not suit a stock level. 00:02:25 A popular entry expires A hot key expires while 100 requests arrive. With and without a lock. It is the most popular key of the shop. All of them look in Redis and miss. All 100 run the same query. The database is overloaded. One request gets the lock and loads. They get the stale copy at once, or wait briefly if there is none. Random lifetimes keep many keys from expiring in the same second. Coordinate concurrent fills for the same key, and bound the pressure allowed through to the origin. Expiring thousands of unrelated keys together can create a second, different burst. 00:03:04 Set policy for each kind of response The same browser asks for four kinds of data. public, max-age=300. The CDN answers for everyone. private, no-cache. The CDN stores nothing; the browser checks each time. A year, immutable. A new image has a new name. no-store. Nobody keeps a copy. Distinguish a browser's private cache from a shared intermediary. Sensitive identity-dependent data needs explicit policy, while truly public immutable assets can often be reused much longer. 00:03:40 A customer sees the wrong account Someone marks every GET as public for an hour. The answer is stored at the edge under /profile, with no person in the key. Same key, so the edge hands over Asha's data. private unless an endpoint is listed as public, and a test with two accounts. Treat the data owner and permissions as part of the caching contract. Remove the unsafe cached representation, correct its policy and verify responses across distinct authenticated users. First decide who may reuse an answer and for how long. A fast cached response is correct only if its contents, freshness and authorized audience all match the new request.