Prevent CDN Cache Stampedes with Origin Shield and Stale-While-Revalidate

by Fahim

Your site is happily serving 800 requests per second across 45 edge locations without breaking a sweat. Then your homepage cache TTL hits zero. Within 50 milliseconds, every edge PoP discovers an expired object, fires concurrent fetches back to your backend, and drops your database connections into a lockup that takes five minutes to reboot.

I learned this the hard way during a product launch. We had great edge hit rates right up until the exact second things expired. We fixed it by setting up a two-layer defense against cache stampedes: combining HTTP stale-while-revalidate headers with a CDN Origin Shield. This combo lets edge nodes serve cached content instantly while fetching fresh data in the background, funneling all that edge revalidation through a single collapsing proxy.

Close-up view of server rack networking gear handling high-throughput web caching
Close-up view of server rack networking gear handling high-throughput web caching

The Thundering Herd: How Expired Edge Caching Kills Origins

A cache stampede (or thundering herd) happens when an intensely requested key expires under heavy traffic. Without defensive caching directives, every incoming request that arrives between the moment of expiration and the moment the new response is written bypasses the cache completely.

Say you run a dynamic API endpoint or an uncached WordPress render that takes 400ms to compute. If you’re handling 250 requests per second and your edge cache TTL is 60 seconds, everything runs smooth for 59 seconds. But at second 60, the cache invalidates.

During the 400ms it takes your origin to build the fresh page, 100 incoming requests hit the edge. None of them find a valid cached object. All 100 slam straight into PHP-FPM, Node, or PostgreSQL at the exact same instant. CPU spikes to 100%, response times balloon from 400ms to 4,000ms, and the next 1,000 incoming requests queue up behind them until Nginx serves a 504 Gateway Timeout.

Throw a globally distributed CDN into the mix and it gets worse. If your CDN runs 40 Points of Presence (PoPs), each PoP manages its own isolated cache storage. When an object expires globally, all 40 PoPs independently ping your origin at the same time to revalidate the asset.

Layer 1: Serve Asynchronous Updates with stale-while-revalidate

The first line of defense is defined in RFC 5861: the stale-while-revalidate HTTP Cache-Control extension. This tells the CDN edge (and modern browsers) to immediately return an expired response to the visitor while quietly firing a background request to fetch fresh data from your origin.

Instead of the client waiting 400ms for your backend to rebuild HTML, they get the 60-second-old cached page in 12ms. Meanwhile, exactly one background worker hits your server, updates the edge cache, and makes the fresh page ready for subsequent visitors.

Add this header to your upstream server config. If you’re running Nginx as your reverse proxy or web server, open your site config:

sudo nano /etc/nginx/sites-available/your-domain.conf

Add the add_header Cache-Control directive inside your primary location block or fastcgi block:

location / { try_files $uri $uri/ /index.php?$args; # Cache for 60s, serve stale for up to 300s while refetching in background add_header Cache-Control "public, max-age=60, stale-while-revalidate=300, stale-if-error=86400" always;
}

Here is what these parameters actually do:

  • max-age=60: The response is completely fresh for 60 seconds. Edge nodes serve it directly without touching the backend.
  • stale-while-revalidate=300: For the next 300 seconds after expiration, the edge immediately serves the expired copy while dispatching a non-blocking background request to update the asset.
  • stale-if-error=86400: If your backend crashes or throws a 500, 502, or 503, the CDN keeps serving the stale version for up to 24 hours instead of showing an error screen to users.

If you’re on Apache instead of Nginx, drop this into your .htaccess or virtual host using mod_headers:


Header set Cache-Control "public, max-age=60, stale-while-revalidate=300, stale-if-error=86400"

Check the MDN Cache-Control documentation to verify browser and proxy quirks before you tweak these window lengths.

Layer 2: Funnel PoP Traffic Using an Origin Shield

stale-while-revalidate removes the latency penalty for end users, but there’s still a catch. If 40 CDN PoPs all hit the 60-second expiration mark around the same time, all 40 PoPs will still trigger background requests back to your origin. That’s 40 simultaneous render calls hitting your server at once.

An Origin Shield fixes this. An Origin Shield is a designated caching node positioned directly between your origin server and the rest of the CDN edge network.

When you turn on shielding, edge PoPs (Tokyo, London, Sydney, Frankfurt) stop talking directly to your VPS. Instead, they treat the Origin Shield (usually set up in the same cloud region or datacenter as your server) as their parent cache.

If you haven’t enabled shielding on your edge yet, walk through my guide to configure Origin Shield in StackPath CDN for traffic spikes. Once it’s running, requests flow through a single choke point:

  1. Tokyo edge PoP receives a request for an expired page.
  2. Tokyo asks the Origin Shield PoP for the updated content.
  3. London edge PoP receives a request for the same page 10ms later and asks the Origin Shield PoP.
  4. The Origin Shield consolidates those incoming queries into a single backend request to your VPS.
  5. The shield updates its local cache and fans out the response back to Tokyo and London simultaneously.

Your origin server handles 1 request instead of 40. For sites running high-load databases alongside object caching, pairing this with Redis Object Cache keeps execution times flat even when traffic spikes hard.

Locking Down Origin Nginx: Collapsing Local Misses

Even with an Origin Shield in place, your local Nginx reverse proxy can run its own cache layer to protect backend workers like PHP-FPM or upstream Node servers. Nginx has a directive called proxy_cache_lock that executes native request collapsing.

Without it, if two background requests slip past the CDN and hit Nginx simultaneously, Nginx spawns two upstream processes. With proxy_cache_lock on;, Nginx lets the first request through to your app server and pauses the second one until the first has finished populating the local proxy cache.

Open /etc/nginx/nginx.conf or your site config and set up your cache zones and lock parameters:

# Define cache path in http block
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=STATIC_EDGE:50m max_size=1g inactive=60m use_temp_path=off;
server { server_name isitdev.com; location / { proxy_pass http://127.0.0.1:3000; proxy_cache STATIC_EDGE; # Only 1 request can fetch from origin at a time proxy_cache_lock on; proxy_cache_lock_timeout 5s; proxy_cache_lock_age 5s; # Return stale cache while lock is held or error occurs proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504; proxy_cache_background_update on; # Cache keys proxy_cache_key "$scheme$request_method$host$request_uri"; add_header X-Cache-Status $upstream_cache_status; }
}

Pay attention to proxy_cache_use_stale updating;. When Nginx detects that a local cache entry expired, it lets incoming queries read the stale object while an asynchronous background subrequest refreshes the disk entry.

Test the syntax and restart Nginx to activate the rules:

sudo nginx -t && sudo systemctl reload nginx

Testing Under Simulated Load with Hey

Don’t just assume your stampede protection works without stressing the edge. You can use hey, a tiny HTTP load testing CLI tool, to simulate 100 concurrent users hitting an expiring resource.

Install hey on your terminal or a remote test box:

# macOS (Homebrew)
brew install hey # Linux (Go binary)
go install github.com/rakyll/hey@latest

Before running the test, tail your origin server’s access log in real time so you can watch actual hits land on your backend:

tail -f /var/log/nginx/access.log | grep -E "GET /(test-endpoint|index.php)"

Now fire 2,000 requests with 100 concurrent workers directly at your CDN URL:

hey -n 2000 -c 100 -m GET https://isitdev.com/api/products

Here is what the output looks like when stale-while-revalidate and request collapsing are working properly:

Summary:
Total: 1.4218 secs
Slowest: 0.0892 secs
Fastest: 0.0084 secs
Average: 0.0141 secs
Requests/sec: 1406.66
Response time histogram:
0.008 [1] |
0.016 [1870] |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
0.025 [112] |■■
0.033 [12] |
0.089 [5] |
Status code distribution:
[200] 2000 responses

Now check your origin log window. Even though 2,000 requests hit your CDN in 1.4 seconds, your origin log should only display 1 single log entry with an upstream response code. Every other request was resolved at the edge or served as stale cache while that single origin request was finishing up.

The Gotchas: Headers That Silently Break Stale Cache

I ran into an issue where stale-while-revalidate completely refused to engage, and every request kept hammering MySQL. After digging through raw cURL headers, I spotted the culprit: dynamic cookies and unhandled Vary headers.

If your application framework outputs a Set-Cookie header (even something harmless like a session ID or CSRF token for guest visitors), most CDN edges automatically treat the response as uncacheable private data.

Inspect your endpoint’s raw headers using curl:

curl -I https://isitdev.com/api/products

If you see headers like this, your edge will never collapse requests:

HTTP/2 200
set-cookie: PHPSESSID=9a8d7f8e...; path=/
vary: Accept-Encoding, User-Agent, Cookie
cache-control: max-age=60, stale-while-revalidate=300

Two major problems here:

  • Set-Cookie forces the CDN to bypass the cache to avoid leaking session tokens between visitors.
  • Vary: User-Agent creates a distinct cache bucket for every browser version on earth. A mobile Safari user won’t share the cache populated by a desktop Chrome user, multiplying origin requests by hundreds.

Strip these tracking headers for public endpoints at the Nginx level before emitting them to the CDN:

location /api/public/ { proxy_pass http://127.0.0.1:3000; # Strip session cookies from public responses proxy_hide_header Set-Cookie; # Sanitize Vary to prevent cache fragmentation proxy_hide_header Vary; add_header Vary "Accept-Encoding" always; add_header Cache-Control "public, max-age=60, stale-while-revalidate=300, stale-if-error=86400" always;
}

Once you clean those response headers, lock down your firewall so no one can discover your real server IP and bypass the edge entirely. Follow our tutorial to restrict origin server access to StackPath Origin Shield IPs in Nginx using native allow/deny rules.

Frequently Asked Questions

Will stale-while-revalidate display outdated content to my users?

Yes, but usually only for a fraction of a second during active revalidation. If an editor updates a post, the next visitor sees the version cached 60 seconds ago while the edge kicks off a background fetch. Within 200–500ms, the new cache entry writes to disk, and everyone after that gets the fresh copy. If real-time accuracy is non-negotiable (like inventory counts at checkout), set Cache-Control: private, no-store on that endpoint.

What is the difference between stale-while-revalidate and stale-if-error?

stale-while-revalidate runs during normal operation when an asset naturally reaches its TTL, fetching fresh data asynchronously while serving the expired copy. stale-if-error only kicks in when your origin server fails completely (returning 500, 502, 503, 504 errors or timing out), serving the cached copy as a fallback safety net.

Can I use stale-while-revalidate without an Origin Shield?

Yes. stale-while-revalidate works on individual edge nodes and browsers on its own. But without an Origin Shield, every CDN PoP around the world will fire its own background fetch independently. Origin shielding collapses those multiple regional background updates into one single request to your origin.

Does proxy_cache_lock increase latency for waiting requests?

Only for the few requests that land while that first request is actively compiling. Instead of all requests overloading the database simultaneously, the locked requests wait a few milliseconds for Nginx to write the cache, then read straight from memory or disk at full speed.

Next Steps

Now that your edge and reverse proxy handle cache invalidation cleanly without overloading your origin, take a hard look at your server’s initial response speed. If your backend takes longer than 600ms to produce that single revalidation hit, look into optimizing your local caching stack by reading how to reduce WordPress TTFB with LiteSpeed Cache.

all_in_one_marketing_tool