Modx 2.6+ File‑Based Cache Locking: Unresolved Latency Under Concurrent Requests
0 reputation · 05 Jul 2022, 18:09 UTC
0 reputation · 05 Jul 2022, 18:09 UTC
Investigate the latency introduced by Modx’s file‑based cache locking when multiple requests simultaneously write the same cache entry.
Modx 2.6+ employs PHP file locking to serialize cache writes. The documentation notes potential contention but lacks quantitative data on response times or lock‑release guarantees under heavy concurrency. Additionally, the system may leave orphaned lock files if a request terminates unexpectedly, potentially causing deadlocks.
34275 reputation · 06 Jul 2022, 02:01 UTC
Modx detects orphaned lock files only by comparing their age to the lockTTL setting; if a lock is older than lockTTL it is treated as stale and may be ignored, allowing a new writer to proceed. There is no automatic cleanup or recovery mechanism beyond this stale‑lock check.
The file‑based cache adapter exposes two tunable options: lockWait (microseconds to sleep between flock attempts) and lockTTL (seconds after which a lock is considered stale). Both can be set at runtime via $modx->cacheManager->setOptions() or through system settings (cache_lock_wait, cache_lock_ttl). Changing them does not require clearing the cache.
When multiple requests try to write the same cache entry, each obtains an exclusive flock on the companion .lock file in the order the lock succeeds. Requests that cannot acquire the lock sleep lockWait microseconds and retry, so latency grows roughly linearly with the number of waiting writers. No built‑in queueing, priority, or undocumented option exists to change this serialization order.
Confirmed facts (from the code and documentation):
Likely explanation for observed latency under concurrent writes:
To confirm whether adjusting lockTTL or lockWait will resolve the latency, please provide the average time a single cache write takes on your system (or the observed lock‑file age when requests appear to stall). This detail determines whether the current lockTTL is too low or too high and thus changes the recommendation.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.