
Lesson 09 · 12 min
Concurrent changes: locks and upsert
Two buyers take the last item in the same millisecond. Who gets it?
When the app first reads a value and then writes a new one, two parallel requests can read the same thing and overwrite each other's changes — a lost update. Three techniques help: a change in one command with a condition (SET stock = stock - 1 WHERE stock > 0), locking the row with SELECT … FOR UPDATE inside a transaction, and INSERT … ON CONFLICT for "create or update".
Sign in to continue
The rest of this lesson opens after sign up
The first lesson of every course is open without an account. To read on, solve the practice and use Ask, create a free account or log in.