Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Cache] Prevent stampede at warmup using apcu_entry() for locking#27028

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed

Conversation

@nicolas-grekas
Copy link
Member

@nicolas-grekasnicolas-grekas commentedApr 24, 2018
edited
Loading

QA
Branch?master
Bug fix?no
New feature?yes
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed tickets-
LicenseMIT
Doc PR-

When a cache is cold, all concurrent processes will end up recomputing the same value, which can be slow at best and can create a cascading failure at worst.

By leveragingapcu_entry(), we can ensure only one process per front is actually computing the value.

In theory, you might wonder if this could be implemented in a generic way using e.g. the Lock component. This would allow using distributed locking, thus allow locking the computation across a cluster of fronts.

But in practice,apcu_entry() is the only primitive that works, because it knows when tonot compute the callback when it waited for a concurrent process, which is something really hard to achieve with other locking mechanisms (if possible at all without all sort of race conditions.) Also, computing only once per-front is already a significant improvement. People having more advanced needs could still create a proxy to handle the distributed lock.

For core, relying onapcu_entry() to provide this behavior is what I propose here.

@webnet-fr
Copy link
Contributor

apcu_entry seems a good choice. Anyway, what do you think about using Symfony's lock component? Something like:

$store = new \Symfony\Component\Lock\Store\FlockStore(sys_get_temp_dir());$factory = new \Symfony\Component\Lock\Factory($store);$lockId = 'cache';$lock = $factory->createLock($lockId);$needToGenerate = $lock->acquire(); // not blockingif ($needToGenerate) {    sleep(5); // heavy work. Calls passed generator function    echo 'Heavy generator is launched. Result is cached.<br>';} else {    $lock->acquire(true); // blocking    echo 'Did not launch heavy generator. Result is got from a cache.<br>';}$lock->release();

@nicolas-grekas
Copy link
MemberAuthor

nicolas-grekas commentedApr 30, 2018
edited
Loading

@webnet-fr that'd be great if it could work, but I don't think that's possible: this code is full of race conditions, and thesleep(5) (or any other value) is a no-go also...

@nicolas-grekasnicolas-grekasforce-pushed thecache-apcu-lock branch 3 times, most recently fromc60f631 to1789e42CompareJune 11, 2018 13:11
fabpot added a commit that referenced this pull requestJun 11, 2018
…lculations (nicolas-grekas)This PR was merged into the 4.2-dev branch.Discussion----------[Cache] Use sub-second accuracy for internal expiry calculations| Q             | A| ------------- | ---| Branch?       | master| Bug fix?      | no| New feature?  | not really| BC breaks?    | no| Deprecations? | no| Tests pass?   | yes| Fixed tickets | -| License       | MIT| Doc PR        | -Embeds#26929,#27009 and#27028, let's focus on the 4th commit for now.This is my last significant PR in the Cache series :)By using integer expiries internally, our current implementations are sensitive to abrupt transitions when time() goes to next second: `$s = time(); sleep(1); echo time() - $s;` *can* display 2 from time to time.This means that we do expire items earlier than required by the expiration settings on items.This also means that there is no way to have a sub-second expiry. For remote backends, that's fine, but for ArrayAdapter, that's a limitation we can remove.This PR replaces calls to `time()` by `microtime(true)`, providing more accurate timing measurements internally.Commits-------08554ea [Cache] Use sub-second accuracy for internal expiry calculations
@nicolas-grekasnicolas-grekasforce-pushed thecache-apcu-lock branch 4 times, most recently frome31510c to6ed5db7CompareJune 11, 2018 19:43
@nicolas-grekas
Copy link
MemberAuthor

I'm closing because I'm not convinced we can plug APCu at this central place.

fabpot added a commit that referenced this pull requestJun 18, 2018
…las-grekas)This PR was merged into the 4.2-dev branch.Discussion----------[Cache] Prevent stampede at warmup using flock()| Q             | A| ------------- | ---| Branch?       | master| Bug fix?      | no| New feature?  | yes| BC breaks?    | no| Deprecations? | no| Tests pass?   | yes| Fixed tickets | -| License       | MIT| Doc PR        | -Replaces#27028This PR protects against cache stampede by wrapping the computation of items in a pool of locks.For each apps, there can be at most 20 concurrent processes that compute items at the same time and only one per cache-key.Commits-------0ac2777 [Cache] Prevent stampede at warmup using flock()
@nicolas-grekasnicolas-grekas modified the milestones:next,4.2Nov 1, 2018
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Projects

None yet

Milestone

4.2

Development

Successfully merging this pull request may close these issues.

3 participants

@nicolas-grekas@webnet-fr@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp