Cache locks and revalidation
Why a cache can still overload an origin
At 09:00 a public catalog entry is absent. Four readers arrive together, before the first origin response is stored. Without coordination, all four may make the application compute the same document. This burst is called a cache stampede. A lock coordinates filling a particular cache key; unrelated keys need not wait for one another. It is not a global request limit or a distributed lock across CDN nodes.
proxy_cache_lock on;proxy_cache_lock_timeout 3s;proxy_cache_lock_age 2s;The fragment belongs inside a cache-enabled location. In the example, waiting clients can time out after three seconds; on modern releases their forwarded response is not stored. A fill taking more than two seconds can allow another origin request. These are separate escape valves, not a guarantee of exactly one request. Choose both using measured fill latency and the client latency budget. Defaults are off for the lock and five seconds for each timer.
0 ms: A starts fill10 ms: B and C wait200 ms: origin returns cacheable documentLater: B and C can use stored documentValidate instead of downloading again
Freshness tells a cache when it can reuse an entry without checking. An expired entry may still match the origin. An ETag identifies a representation; Last-Modified can provide a time-based validator. Nginx can make a conditional origin request for an expired entry. The origin may send 304 if the validator still matches, letting the cache reuse the stored body. A client making an ordinary GET can receive 200 with that body even though the origin responded 304.
proxy_cache_revalidate on;proxy_cache_valid 200 30s;GET /catalog HTTP/1.1Host: catalog.testIf-None-Match: "catalog-v1"
HTTP/1.1 304 Not ModifiedETag: "catalog-v1"Cache-Control: public, max-age=30When content changes
If the origin now has catalog-v2 it must send the new body with its new validator and appropriate cache policy. Do not reuse the old ETag for changed bytes. The upstream status, cache status and origin request counter are separate measurements. Revalidation saves transfer, but still uses an origin request. The lab tests a short freshness window and an unchanged validator, not a full HTTP conditional-request implementation.
Practice
Draw a timeline where the fill exceeds the lock age and another where a waiting client reaches its timeout. Predict why an ordinary client GET can return 200 after an origin 304. Next: choose when an expired copy may be served without waiting.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Cache locks and revalidation
Reduce duplicate origin work on cold cache misses and revalidate expired public content with HTTP validators.
What does a cache lock coordinate?
What can happen when lock age expires?
On modern versions what happens to a forwarded waiter after lock timeout?
What does an unchanged ETag allow?
Does revalidation eliminate all origin requests?