Skip to content

Serialize concurrent increments and unless_exist writes - #321

Open
ydah wants to merge 2 commits into
rails:mainfrom
ydah:fix-atomic-cache-writes
Open

ydah wants to merge 2 commits into
rails:mainfrom
ydah:fix-atomic-cache-writes

Conversation

@ydah

@ydah ydah commented Oct 14, 2026

Copy link
Copy Markdown

Concurrent unless_exist writes to a missing key can all succeed, and concurrent increments can lose updates because SELECT ... FOR UPDATE has no row to lock.

Insert a placeholder before locking and reading the entry. Keep it within the transaction, replace it on write, and remove it when the block returns no value. A zero byte size distinguishes placeholders from valid empty values.

ydah added 2 commits October 14, 2026 14:01
Warm schema metadata before the workers start and use a GVL-releasing busy handler for Rails 7.2 SQLite connections. Restore the configured timeout after each worker finishes.
@tkawa

tkawa commented Oct 22, 2026

Copy link
Copy Markdown

This looks like it addresses the same problem as #297: lock_and_write can't serialize access to a missing key, because there is no row for SELECT ... FOR UPDATE to lock. Both PRs take the same approach of inserting a placeholder row before locking and removing it when nothing is written.

The main differences:

I'm fine with either approach being merged. If this one is preferred, I'll close #297.

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.

2 participants