-
Notifications
You must be signed in to change notification settings - Fork 36
Mitigate LRU struct tearing using SeqLock #593
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
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
If the cache entry is a value type larger than the native pointer size (e.g. a Guid), writes are not atomic and if the value is updated and read concurrently, readers may see a torn value.
There are at least two ways to solve this:
Option 1 is preferred, because option 2 can make cache size unstable (stale values consume queue slots, pushing out live cache entries).
SeqLock pros and cons:
ConcurrentLruSoakTests.WhenValueIsBigStructNoLiveLock
vsLruItemSoakTests.DetectTornStruct
). Updating the same item in an extremely tight loop is not the common case. Live lock is mitigated in this improved version, but requires more memory and returns a stale result.Atomic/Scoped etc.
The update code paths for atomic/scoped caches generate new wrapper class instances and call cache update to replace the object. They are therefore not susceptible to torn reads - the structs inside them are not changed after the wrapper is created.
Using a lock statement (be8f0ed)
Naive implementation using a C# lock statement added to read makes LRU roughly the same latency as LFU with a Guid value. Since
LruItem
is already locked on update, torn reads are prevented by the lock. This comes with the overhead of the lock, which results in lock contention for concurrent reads.Read throughput is significantly reduced:

Using SeqLock (87fcd06)
See SeqLock. This is a good fit for our scenario, because we already have a single threaded update, and we can keep reads lock free and fast.