Database locks and concurrent updates 00:00:01 Last copy at the lending desk The lending desk has one copy of The Art of Code. Two patrons decide to borrow it at the same moment. Each may have seen one available copy, but the library owes at most one completed loan. Both requests address the same stock row. Their modifications require coordination; one can proceed while the other may need to wait, abort or see a changed result. Requests for different stock rows can often proceed independently as far as these particular row locks are concerned. The competing borrowers want the same scarce copy. A second title with its own row usually does not need that particular write lock. Other shared bottlenecks, including indexes and worker pools, can still limit both requests. 00:00:46 Check and decrement together Transaction A runs a single statement that checks if stock is greater than zero and then decrements it, all in one step. A changes the stock from one to zero. B may wait on A, receive a conflict, or have to retry according to the database and the chosen isolation contract. When B retries, it sees zero rows were affected, meaning the copy is already gone, so it knows it lost the race. Read the database’s affected-row result after the appropriate wait or retry. A statement which changes zero rows is a normal losing outcome here, not proof that the service or the network has failed. 00:01:25 Read the result, not a stale display The winning transaction now has one recorded loan, and the stock count is correctly set to zero. The losing transaction can offer a wait list, since the book is still unavailable, but it won't try again immediately. If either had checked stock in a separate query first, both would have seen one copy and falsely believed they could proceed. Record the winning loan and its stock decrement within the same protected transaction. Otherwise a crash can leave a perfectly decremented stock row with no corresponding borrower or loan to explain it. 00:01:59 What actually conflicts? Many MVCC engines allow an ordinary consistent read to use a suitable version while a writer holds a conflicting write lock. A locking read or another update has different requirements. Some engines also lock ranges for particular queries or isolation levels. A request to skip already-locked rows is a query behavior, not an alternative kind of database lock. This means only the hot title is a bottleneck—other titles can be processed in parallel without conflict. Lock behavior depends on the storage engine, chosen isolation level, indexed predicate and concurrent operation. State the assumptions for this one stock row before extending the result to search ranges or multirow reservations. 00:02:44 Operate during a rush To avoid long waits, keep transactions short and always acquire locks in the same order across all operations. Set bounded retries and timeouts so systems don’t hang while waiting for locks that may never be released. Always verify stock and committed loans from the database, not from HTTP success counts, to avoid false assumptions. Replay the rush for one popular title, then spread the same number of requests over independent titles. The difference in wait distributions helps separate contention for one row from a generally undersized database.