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

Fix race condition in multilevel cache replication #232

Merged
merged 2 commits into from
Oct 15, 2024

Conversation

costaraphael
Copy link
Contributor

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.

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.
cabol
cabol previously approved these changes Oct 12, 2024

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)
Copy link
Owner

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 588d14f

@coveralls
Copy link

Coverage Status

coverage: 100.0%. remained the same
when pulling 588d14f on costaraphael:fix-multilevel-cache-race-condition
into d47e568 on cabol:master.

@cabol cabol merged commit 7df6677 into cabol:master Oct 15, 2024
7 checks passed
@costaraphael
Copy link
Contributor Author

@cabol thanks for reviewing/merging this, and for all the work that went into this library! Truly amazing stuff! ❤️

@cabol
Copy link
Owner

cabol commented Oct 15, 2024

Thanks ❤️ And also thanks for your contributions!!

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

Successfully merging this pull request may close these issues.

3 participants