-
Notifications
You must be signed in to change notification settings - Fork 74
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
Fix race condition in multilevel cache replication #232
Fix race condition in multilevel cache replication #232
Conversation
This commit fixes a race condition that is likely to happen when one of the layers for a multilevel cache has to do some networking. The issue arises from the calling `get` to see if a key exists, and then calling `ttl` when replicating the key to other layers. This means that the calls are happening in a non-atomic way, which in turn means that by the time `ttl` gets called the key might have expired already (the race condition). The fix is detecting that the key has expired by checking for `nil` in the `tll` call result, and in this case not replicating the cached entry to the other layers.
|
||
test "does not replicate the data if the cache expires during replication" do | ||
# reading from L2 will take 500ms | ||
Nebulex.TestCache.DelayedReadAdapter.put_read_delay(500) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@costaraphael let's alias the module to avoid the credo warning
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 588d14f
@cabol thanks for reviewing/merging this, and for all the work that went into this library! Truly amazing stuff! ❤️ |
Thanks ❤️ And also thanks for your contributions!! |
This commit fixes a race condition that is likely to happen when one of the layers for a multilevel cache has to do some networking.
The issue arises from the calling
get
to see if a key exists, and then callingttl
when replicating the key to other layers. This means that the calls are happening in a non-atomic way, which in turn means that by the timettl
gets called the key might have expired already (the race condition).The fix is detecting that the key has expired by checking for
nil
in thetll
call result, and in this case not replicating the cached entry to the other layers.