Skip to content

fix(object-cache): per-request version tokens and Redis purge - #23

Merged
brianvarskonst merged 3 commits into
mainfrom
fix/object-cache-version-tokens
Nov 23, 2026
Merged

brianvarskonst merged 3 commits into
mainfrom
fix/object-cache-version-tokens

Conversation

@brianvarskonst

Copy link
Copy Markdown
Member

Stacked on #21 — merge #21 first; GitHub retargets this PR to main when #21's branch is deleted (rebase onto main afterwards).

Problem

Findings #2, #4 and #6 from the 2026-11-22 re-audit of the native Redis/Memcached object-cache backends:

  • Fix review findings and prepare v1.0.2 #4 rawKey() read the global and the group namespace version from the backend for every key: one get = 3 round trips, get_multiple(50) = 101 instead of 1.
  • Preserve upgrades with stable signed application caches #6 Versions were counters starting at 1. If a version key was evicted (LRU, slab eviction) or unreadable, it restarted at 1 and entries from before a flush became valid again.
  • Welcome #2 flush()/flush_group() only bump the version; purge() was a no-op. Every flush left the whole previous keyspace in Redis, mostly without TTL. With noeviction and a flush per deploy, Redis eventually rejects writes.

Fix

  • Per-request version cache (Fix review findings and prepare v1.0.2 #4): versions are kept in memory for the adapter's lifetime (= one request in the drop-in) and updated by the adapter's own clear(). Trade-off: a flush from another process reaches a running request only for keys it has not read yet. The proxy's request cache already has that property for every key it has read, and WordPress' own Memcached drop-in caches its flush number per request the same way. Documented in the README.
  • Random version tokens (Preserve upgrades with stable signed application caches #6): bin2hex(random_bytes(6)) instead of counters; flush = SET a new token (no Lua needed any more). A missing version key gets a fresh token (SET NX / add, re-read on race); an unreadable version uses a throwaway token, so the call misses instead of assuming version 1. Existing counter values ("1", "2", …) are valid tokens, so the upgrade does not invalidate the cache.
  • Redis purge (Welcome #2), design choice: the report left SCAN+UNLINK vs. a TTL cap open. I picked SCAN+UNLINK in purge() because it keeps current semantics: flush stays an O(1) version switch, and entries stored without expiration keep living until a flush supersedes them. A TTL cap would expire entries that callers stored with expire = 0, which changes behaviour. purge() runs from the existing sympress.framework.cache_purge cron and wp sympress cache purge. It scans <prefix>:v* (prefix glob-escaped) in batches of 1000, compares each key's global/group token with the current stored ones (request cache is reset first) and UNLINKs stale keys. Deleting is always safe for a cache. A race with a concurrent flush can only delete entries early, never keep stale ones readable.
    • Scope limit (code comment shortcut:): only phpredis-compatible clients (phpredis, Relay) are scanned. Predis and cluster/RedisArray connections keep the previous no-op, because their SCAN signatures differ and that path is untested anyway.
    • Memcached keeps a no-op purge: unreachable entries are evicted by its LRU, and Memcached cannot enumerate keys.

Tests

  • New NativeObjectCacheVersionTest (Redis + Memcached doubles):
    • get = 1 backend read, get_multiple(50) = 1 read after warm-up. Fails on fix(object-cache): native backend and request cache correctness #21's head (3 / 101 reads), passes here.
    • Evicted version keys do not revive pre-flush entries. Fails before (Redis and Memcached), passes here.
    • Redis purge removes superseded global and group entries and keeps current ones and version keys. Fails before (7 keys stay), passes here.
    • Own flush is visible within the request (guard for the version cache).
  • Live smoke (backend-smoke.yml): after clear(), purge() on real Redis leaves only version keys under the prefix. Not run locally (no ext-redis/Redis in the local image); CI covers it.
  • Local: composer validate --strict, composer cs, composer static-analysis, composer tests green (100 tests, 3 live-cache skips).

Release

chore(release): prepare 1.0.12 (patch: bug fixes only; CHANGELOG only, as in the previous release commits). It covers #21, this PR and the unreleased 492db26 ci: let Dependabot update GitHub Actions pins.

Risk to note: version tokens are 12 hex chars instead of small integers, so raw keys get about 20 bytes longer. With the default prefix, Memcached keys stay around 195 bytes (limit 250). A very long custom SYMPRESS_CACHE_PREFIX has less headroom than before.

Replaces #22, which GitHub closed when its stacked base branch was deleted after #21 merged.

The native Redis and Memcached backends read the global and the group
namespace version from the backend for every key: one get cost three
round trips and get_multiple(50) cost 101 instead of one.

Versions are now kept in memory for the adapter's lifetime (one request
in the drop-in) and updated by the adapter's own clear(). This matches
the request cache the proxy already keeps: a flush from another process
becomes visible to a running request only for keys it has not read yet.

Versions also become random tokens instead of counters. A version key
lost to eviction used to restart at 1, which made entries written
before a flush valid again; it now gets a fresh token. If the version
cannot be read, the call uses a throwaway token and misses instead of
assuming version 1. Existing counter values keep working as tokens, so
upgrading does not invalidate the cache.
flush() and flush_group() only switch the namespace version, and
purge() was a no-op, so every flush left the whole previous keyspace in
Redis, mostly without a TTL. With the default noeviction policy and a
deploy script that flushes on every release, Redis eventually rejects
writes, including those of other tenants of the instance.

The scheduled purge (and `wp sympress cache purge`) now SCANs the
adapter's prefix and UNLINKs entries whose global or group version is
no longer current. Flush itself stays an O(1) version switch, and
entries without a TTL keep living until a flush supersedes them, so
cache semantics do not change. A capped TTL on every entry was
rejected because it would expire entries stored with no expiration.

Only phpredis-compatible clients (phpredis, Relay) are scanned; Predis
and cluster connections keep the previous behaviour. Memcached needs no
purge because it evicts unreachable entries through its LRU.
@brianvarskonst
brianvarskonst merged commit 2bb7cd5 into main Nov 23, 2026
4 checks passed
@brianvarskonst
brianvarskonst deleted the fix/object-cache-version-tokens branch November 23, 2026 13:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant