Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TTL "jitter" #129

Closed
joshuaadickerson opened this issue Jan 23, 2014 · 2 comments
Closed

TTL "jitter" #129

joshuaadickerson opened this issue Jan 23, 2014 · 2 comments

Comments

@joshuaadickerson
Copy link

When you do a cache warmup, people often use a single TTL for a lot of keys. When the keys are related, this can cause the callback to be stampeded with requests. The solution is to make those TTLs have a slight variation. Usually that is done with a percentage of the original TTL.

I propose adding a method which will add jitter to the TTL. Something like

/**
 * @param int $ttl
 * @param float $jitter
 * @return int
 */
function jitter($ttl, $jitter)
{
    if ($jitter >= 1 || $jitter =< 0)
    {
        throw new Exception('$jitter must be between 1 and 0');
    }

    $variation = $ttl * $jitter;
    $min = ceil($ttl - $variation);
    $max = ceil($ttl + $variation);

    return rand($min, $max);
}
@tedivm
Copy link
Member

tedivm commented Jan 23, 2014

Check it out- https://github.com/tedivm/Stash/blob/master/src/Stash/Item.php#L326

We currently subtract a random amount of time from the TTL, capped at 15%. This is a little different than your method, because our method does not allow the TTL to be extended past it's original time time, it only allows the ttl to be lowered.

@joshuaadickerson
Copy link
Author

I completely missed that. Different from mine, but the purpose is the same. Closing.

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

No branches or pull requests

2 participants