Make Reset atomic to avoid race conditions in concurrent operations#265
Open
SurbhiAngelite wants to merge 1 commit intoulule:masterfrom
Open
Make Reset atomic to avoid race conditions in concurrent operations#265SurbhiAngelite wants to merge 1 commit intoulule:masterfrom
SurbhiAngelite wants to merge 1 commit intoulule:masterfrom
Conversation
Previous (DEL-based) Reset Reset: Deletes the key with DEL. Increment: Atomically increments the key and sets TTL using a Lua script. Race Condition: If Reset and Increment happen at the same time, the following can occur: Increment reads the key, sees it exists, increments. Reset deletes the key. Increment writes the new value, but the key may have just been deleted, or vice versa. Result: The counter may be unexpectedly deleted or recreated, leading to inconsistent state (e.g., a request after Reset may still see a non-zero count). Current (Lua-based) Atomic Reset Reset: Uses a Lua script to set the key to zero and set the TTL in a single atomic operation. Increment: Still uses a Lua script to increment and set TTL atomically. Race Condition: Both Increment and Reset are now single atomic Lua scripts. Redis guarantees that only one script runs at a time for a given key. If Increment and Reset are called concurrently, Redis will execute one script fully before the other starts. Result: No partial state: the key will either be incremented or reset to zero, never both at the same time. The final value will be either 0 (if Reset wins) or incremented (if Increment wins), but never a corrupted or missing key.
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.
Previous (DEL-based) Reset
Reset: Deletes the key with DEL.
Increment: Atomically increments the key and sets TTL using a Lua script. Race Condition:
If Reset and Increment happen at the same time, the following can occur: Increment reads the key, sees it exists, increments. Reset deletes the key.
Increment writes the new value, but the key may have just been deleted, or vice versa. Result: The counter may be unexpectedly deleted or recreated, leading to inconsistent state (e.g., a request after Reset may still see a non-zero count).
Current (Lua-based) Atomic Reset
Reset: Uses a Lua script to set the key to zero and set the TTL in a single atomic operation. Increment: Still uses a Lua script to increment and set TTL atomically. Race Condition:
Both Increment and Reset are now single atomic Lua scripts. Redis guarantees that only one script runs at a time for a given key. If Increment and Reset are called concurrently, Redis will execute one script fully before the other starts. Result:
No partial state: the key will either be incremented or reset to zero, never both at the same time. The final value will be either 0 (if Reset wins) or incremented (if Increment wins), but never a corrupted or missing key.