The problem with a cache that just expires
The simple way to run a page cache is to give every page a lifetime and delete it when the time is up. The next visitor finds nothing stored, so WordPress builds the page while they wait.
On a quiet site that is one slow page view. On a busy one it is worse. A popular page expires, fifty people ask for it in the same second, and all fifty requests start building the same page. That pile-up is usually what takes a site down on its biggest day.
Keep the old copy a little longer
Serving stale changes one thing. An expired page is not deleted straight away. For a short grace period it is still handed out, marked as stale, and one background request is sent to rebuild it. When the new copy is ready it replaces the old one.
Every visitor gets an answer at cache speed. The only request that waits for WordPress is one nobody is watching.
Edits should refresh, not wipe
The same idea applies when you publish. Clearing the whole cache after an edit sends every visitor to WordPress at once. In Ace Redis Cache an edit marks the affected pages stale and queues them for a rebuild, so the old page keeps going out until the new one exists.
A background refresh needs a budget
A refresh is still real work, and it runs on the same servers as your visitors. Left unchecked, it can become the pile-up it was meant to prevent. These are the limits I settled on after getting it wrong on a small server:
- One background render per block every 20 seconds, however many visitors ask.
- The request that triggers a refresh hands it over and lets go after 3 seconds. An early version waited up to 20, which tied up a PHP worker for every refresh.
- The render carries on after that hand-off, so a slow page still finishes.
- If the page sits behind a CDN or firewall, send the refresh straight to the web server. Otherwise your own firewall may turn it away.
What to check on your own site
Load a page, wait for it to expire, and load it again while watching the response headers. With ARC a stale answer says HIT (advcache, stale), and the request after it should be a fresh HIT. If you see a MISS and a slow response instead, a visitor did the waiting.