Preparing your workspace
Database concurrency controls help multiple transactions work safely without producing incorrect results.
Production applications receive many requests at the same time. Two transactions may try to read or update related rows concurrently. MySQL uses locking and transaction isolation mechanisms to control these interactions. Understanding concurrency helps prevent issues such as lost updates, inconsistent reads, and race conditions at the database level. The correct solution depends on the business operation, transaction design, isolation level, and database engine configuration.
If two students try to claim the final available seat in a course at the same time, the database must protect the capacity update from producing an impossible result.
START TRANSACTION; SELECT seats_available FROM courses WHERE id = 5 FOR UPDATE; UPDATE courses SET seats_available = seats_available - 1 WHERE id = 5 AND seats_available > 0; COMMIT;
The transaction locks the selected course row while checking and updating available seats.