The problem: one site's clear button empties everyone's cache
Running a Redis server per site is tidy and wasteful. Most small and medium sites use a sliver of one, so sharing an instance between the sites on a server is common and sensible.
It goes wrong in a specific way. Somebody presses "clear cache" on one site, and every other site on the server slows down at the same moment, because the clear removed their warm caches too. Or two sites collide on a key name and one shows the other's menu.
Both come from the same cause: some operation did not carry the site boundary.
Give every site a namespace
Each site's keys need a prefix that no other site shares. In WordPress the standard way is a salt in wp-config.php, which a well-behaved object cache puts in front of every key:
define( 'WP_CACHE_KEY_SALT', 'mysite_live:' );
- Make it unique per site and per environment. A staging copy that shares the live site's salt and Redis will happily overwrite live data.
- Set it before anything is cached. Changing it later abandons the old keys, which then need lifetimes to clear themselves out.
- On multisite, the blog ID has to be part of the key as well, apart from the groups that are meant to be global.
A separate Redis database number per site (SELECT 1, SELECT 2) also works on a single server, but database numbers are not available on cluster-mode services, so I prefer prefixes and treat database numbers as a bonus.
Follow every operation, not only reads and writes
This is where the leaks are. A cache has more than get and set. List every path that touches Redis and check each one uses the same definition of "this site's keys".
| Operation | What to check |
|---|---|
| Read and write | Keys carry the site prefix |
| Full clear button | Removes this site's keys only |
| Purge on publish | Scoped to the site, and to the page where possible |
| Entry counts and memory figures | Count this site's keys, and say so when a figure is for the whole server |
| Background refresh queue | Queue names and locks are per site |
| Early page serving | Code that runs before WordPress loads builds the same keys with the same prefix |
| Indexes and metadata | Cleared along with the entries they describe |
The early layer deserves a second look. Code that answers before WordPress has loaded cannot call WordPress functions, so it often builds its keys separately. If it disagrees with the main plugin about the prefix or the host name, a site can write one family of keys through normal traffic and read another.
A flush is a very large operation
FLUSHDB removes every key in the database. On a shared instance that is every site, plus anything else stored there: locks, queues, rate limits.
Wildcard deletes carry the same risk in a quieter form. A pattern that looks specific because it has a plugin's name in it will match every site that runs that plugin.
- Never call
FLUSHDBorFLUSHALLfrom site code on shared Redis. - Scope every pattern to the site prefix first, then to the family.
- Better still, avoid patterns. A per-site generation counter clears one site's object cache with one command and cannot touch another's.
Sites share more than keys
Even with perfect boundaries, sites on one Redis share its memory and its time.
- Memory. One site writing month-long transients fills the instance and causes evictions for the others. A lifetime cap on each site keeps cold keys from crowding out everyone's working set.
- Background work. If five sites all start warming their caches after a deploy, they compete for the same CPU. Let them take turns.
- Failure. When Redis is down, every site is uncached together. Each one needs to fail open on its own.
A prefix is not access control
A namespace prevents accidents between sites that respect it. It does not stop a process with full Redis access from reading or deleting anything.
If the sites belong to different customers, or one of them runs code you do not trust, use Redis ACLs to give each site a user restricted to its own key pattern, or give them separate instances. Decide this as part of the hosting design, not after an incident.
What Ace Redis Cache does
- Object cache keys are built from a site-salted namespace, a per-site generation number and the blog or global group scope.
- A flush moves that site's generation. If the counter cannot be used, it falls back to a scan and delete limited to the site's own namespace. It never flushes the database.
- Page cache keys include the host, so two sites cannot answer for each other's URLs, and purges reject URLs for hosts that are not the site's own.
- Page warming can take a shared lock file, set with
ACEREDIS_BACKGROUND_LOCK, so several sites on one server warm in turn.
The multisite wiki page covers networks, and where ARC sits shows the layers.
Test with two sites, not one
A single-site test cannot find a boundary fault. This takes ten minutes on a staging server.
- Set up two sites on the same Redis, each with its own salt.
- Warm both: load a few pages on each and write a known transient on each.
- Clear the first site's cache with its admin button.
- Check the first site's pages, counters and transient are gone.
- Check the second site still returns cache hits and still has its transient.
- Repeat step 3 by publishing a post instead of pressing the button, since that is a different code path.
redis-cli --scan --pattern 'siteone_live:*' | wc -l
redis-cli --scan --pattern 'sitetwo_live:*' | wc -l
Checklist
- A unique salt per site and per environment.
- Every operation scoped: clear, purge, count, queue, early serving.
- No database-wide flush from site code.
- Lifetime caps so one site cannot fill the instance.
- ACLs or separate instances where the sites do not trust each other.
- A two-site test through both the button and the publish path.