The problem: the edit that does not show
You publish a correction. Signed in, you see it. Signed out, the old page is still there, and it stays there for as long as Varnish chooses to keep it.
The page cache inside WordPress knew about the edit and replaced its copy. Varnish, sitting in front, was never told. Two layers are only as fresh as the one that hears about changes last.
Is it worth running both?
Yes, when each does a different job.
| Layer | Serves | Good at |
|---|---|---|
| Varnish | Signed-out visitors and crawlers | Answering repeat requests without touching PHP at all, and carrying on when the origin is slow |
| Page cache in Redis | Whatever Varnish does not have | Knowing exactly which pages an edit affects, and rebuilding them |
| Object cache in Redis | Signed-in visitors, admin, background jobs | Saving database work on pages that cannot be cached whole |
If you are unsure about the bottom two rows, page cache and object cache explains them.
Rule one: purge both, every time
Wherever the application removes a page from its own cache, it must tell Varnish to drop the same page. Varnish needs a way to accept that, limited to the web server itself:
acl purge {
"localhost";
"127.0.0.1";
}
sub vcl_recv {
if (req.method == "PURGE") {
if (!client.ip ~ purge) {
return (synth(405, "Not allowed"));
}
ban("req.http.host == " + req.http.host + " && req.url ~ " + req.url);
return (synth(200, "Banned"));
}
}
Three details matter here.
- Match the host as well as the path. One Varnish often fronts several sites. A ban on
/news/without a host clears that path on all of them. - Anchor the pattern. A ban for
/news/should not also remove/news/archive/. - Send purges once, at the end of the request. A single save can touch a post, its categories, its author page and the home page. Collect the paths, remove duplicates, and collapse to one site-wide ban when the list covers everything.
Rule two: never store what is not yours to share
The worst cache fault is one visitor seeing another's basket or account. Pass personal requests straight through, and refuse to store a response that sets a cookie or says it is private.
sub vcl_recv {
if (req.http.Cookie ~ "wordpress_logged_in_|wp-postpass_|comment_author_|woocommerce_") {
return (pass);
}
if (req.url ~ "^/(wp-admin|wp-login\.php|cart|basket|checkout|my-account)") {
return (pass);
}
}
sub vcl_backend_response {
if (beresp.http.Set-Cookie || beresp.http.Cache-Control ~ "private|no-store|no-cache") {
set beresp.uncacheable = true;
return (deliver);
}
}
Use your shop's real basket and account paths, not only the defaults. Cache the page, not the visitor covers what is safe to share in the first place.
Rule three: let refreshes through
A page cache that rebuilds pages in the background does it by requesting them. If that request is answered by Varnish, nothing is rebuilt: the old page is fetched and stored again as if it were new.
Give background refreshes a header and pass them:
sub vcl_recv {
if (req.http.X-AceRedis-Prime) {
return (pass);
}
}
Better still, send the refresh straight to the web server so it never meets the outer layers. In Ace Redis Cache that is define( 'ACEREDIS_EARLY_REFRESH_ADDR', '127.0.0.1' );. Without it the refresh goes out by the site's public name, and a CDN or firewall in front may turn it away.
Rule four: do not let Varnish keep a stale copy for long
If the inner cache is serving stale while it rebuilds, the stale page is only correct for a few seconds. Varnish should not then store it for an hour.
sub vcl_backend_response {
if (beresp.http.X-AceRedisCache ~ "stale") {
set beresp.ttl = 10s;
}
}
Rule five: keep browsers out of it
You can purge Varnish. You cannot purge a visitor's browser. Let the shared caches hold HTML for a long time and tell browsers to hold it briefly or not at all, so an edit reaches people on their next page view.
What Ace Redis Cache does for you
The plugin mirrors every page cache purge to Varnish:
| In the plugin | In Varnish |
|---|---|
| Specific paths purged | A ban for each path, anchored |
| Everything cleared | The whole host is banned |
| A background refresh stores a new copy | That page is banned, so visitors get the new copy at once |
Varnish is looked for on 127.0.0.1:6081, at most once every ten minutes, and a site with no Varnish there is unaffected. ACEREDIS_VARNISH turns the integration on, off or back to automatic, and ACEREDIS_VARNISH_HOST points it somewhere else. The VCL above is what the plugin expects, and the Varnish and CDNs wiki page has the full version.
The plugin does not talk to any CDN. It gives you actions to hook your own purge to, which the same wiki page lists.
A trap when reading headers through Varnish
Varnish stores the response headers along with the page. An X-AceRedisCache: MISS seen through Varnish may be days old. It is the header from the request that filled Varnish, replayed.
To see what the origin is doing now, ask it directly:
curl -sI -H 'Host: example.com' http://127.0.0.1:8080/ | grep -i x-ace
Use whichever port your web server listens on behind Varnish. HIT, MISS or BYPASS explains what comes back.
Test the whole chain
- Load a page signed out, twice. The second response should come from Varnish (an
Ageheader above zero). - Edit the page and save.
- Load it signed out again. You should see the edit straight away.
- Load a listing that includes the page, such as its category. That should be updated too.
- Add something to a basket and load the same page. It must not come from the shared cache.
- Clear the whole cache from WordPress and check that Varnish was emptied as well.
Checklist
- Every application purge is mirrored to Varnish, with the host.
- Personal requests and cookie-setting responses are never stored.
- Background refreshes reach PHP.
- Stale copies get a short lifetime in Varnish.
- Browsers are not allowed to hold HTML for long.
- Headers are read at the origin when you are debugging.
For where each layer sits, see how it works and features.