A cache entry expiring under load means every concurrent request recomputes it, and a lock makes them queue instead.
// XFetch: recompute early, with a probability that rises
// as expiry approaches
$delta = $computeCostSeconds;
$now = microtime(true);
if ($now - $delta * $beta * log(mt_rand() / mt_getrandmax()) >= $expiry) {
$value = $this->recompute();
$this->store($value, $now, $delta);
}
return $value;
One request recomputes slightly early while everybody else is served the still-valid entry, which avoids both the stampede and the queue a lock produces. The recomputation cost has to be stored alongside the value, because the probability scales with it — an expensive entry starts refreshing earlier, which is exactly right and is the part that makes this better than a fixed early refresh.