A transient stampede on a cold cache

A transient holding an expensive query result, expiring at a fixed time, and four hundred requests recomputing it simultaneously.

$value = get_transient( $key );

if ( false === $value ) {
    if ( ! turkerdev_acquire_lock( $key, 30 ) ) {
        // somebody else is computing it. serve the
        // stale copy rather than waiting or recomputing.
        return get_transient( $key . ':stale' ) ?: turkerdev_empty_result();
    }

    $value = turkerdev_compute( $key );

    set_transient( $key, $value, HOUR_IN_SECONDS );
    set_transient( $key . ':stale', $value, DAY_IN_SECONDS );
    turkerdev_release_lock( $key );
}

The stale copy with a longer lifetime is what makes the losing requests cheap, and without it they either block or recompute — both of which exhaust the worker pool under the load that caused the expiry to matter. WordPress has no lock primitive, so the lock is an option with a timeout, which is racy in a way that does not matter here because a duplicate computation is merely wasteful.