267
optimistic concurrency
also version column, ETag, compare-and-swap
Write only if the version you read is still current. Zero rows updated means someone else wrote.
What is optimistic concurrency?
Optimistic concurrency lets readers proceed, and the write checks a version, an updated_at, or an ETag. The update is `where id = ? and version = ?`. A count of zero is a conflict, returned as 409, not a success. The client refetches and retries. This is not optimistic UI, which only changes the screen before the response.
Why does optimistic concurrency matter when vibe coding?
Two edits load the same row. Both save. The second form replaces the first person’s fields with blanks they never saw. The draft had no version in the WHERE clause.
How do you do optimistic concurrency?
Send the version you read. Update with it in the predicate. Increment it on success. On conflict, return 409 and the current row. Do not last-write-wins a form.
How do you ask a model for optimistic concurrency?
Update (row) only if version is the one the client read. Increment the version. If no row changes, return 409 with the current resource. Do not overwrite with a stale form.
What goes wrong with optimistic concurrency?
Checking the version in application code, then updating without it in the SQL. Two requests both pass the check.