Prevent Concurrent Rebuilds of an Expensive WordPress Transient
Several requests can see the same transient miss at once and all start a costly rebuild. Use a harmless, instrumented staging fixture to measure how often this happens before adding coordination.
Reproduce two misses at the same time
Choose one derived public value, such as a count from a test catalog. Record rebuild starts and ends with a fixture request ID. Start two concurrent requests after removing only the fixture key. If each request begins the expensive calculation, you have a repeatable concurrency case.
Coordinate at the correct scope
Use a short-lived, atomic add operation in the persistent backend shared by all workers that handle this key. WordPress wp_cache_add adds a value only if the key is absent, but its normal in-memory cache is request-local. Confirm the installed drop-in gives the required cross-worker atomic behavior. Use matching site, group and data dimensions for the value and coordination key.
Design for expiration and failure
After acquiring the key, recheck the cached value. Keep competing requests bounded using a safe stale copy or a brief fallback, as the workload allows. Test a stalled writer, an expired provisional key and a failed rebuild. Use backend-supported ownership when releasing a lease to avoid deleting a newer worker's key after expiration. Never serve stale permissions, prices or similarly sensitive data without an explicit product rule.
Expected result
The evidence shows one rebuild in the overlapping case and recovery after a stalled worker. Preserve the original simpler behavior for workloads where concurrent rebuilds are inexpensive.
Related guides
- How to Handle WordPress Transient Cache Misses
- How to Tell a WordPress Object Cache from a Page Cache
Official references
Ferkesh Hosting. Last reviewed .
Спасибі за відгук!