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

lock Increment/Decrement to avoid lostupdate #2

Merged
merged 1 commit into from
Nov 22, 2021

Conversation

hidetatz
Copy link
Contributor

@hidetatz hidetatz commented Nov 22, 2021

In current implementation, if multiple goroutines calls Increment/Decrement simultaneously, the lost update will occur. Let's consider below scenario:

  • The record "counter:0" exists in the NumberCache store
  • Goroutine A and B want to increment that by 1
    • After all, the record should be "counter:2"
  • GetItem called by Goroutine A returns "counter:0"
  • GetItem called by Goroutine B returns "counter:0"
  • Goroutine A increments it by 1, so the record is now "counter:1"
  • Goroutine B increments it by 1, but the record is still "counter:1" because Goroutine B incremented stale record "counter:0"

I have written unit tests to make sure above behavior occurs. You will see tests fail if you pull this branch locally and comment out the changes in cache.go, then run gotip test .

@Code-Hex Code-Hex merged commit 2db8578 into Code-Hex:main Nov 22, 2021
@Code-Hex
Copy link
Owner

@hidetatz Thanks!

@hidetatz hidetatz deleted the hidetatz/bug/incr_decr_lostupdate branch November 22, 2021 05:10
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.

None yet

2 participants