Database Transactions and Concurrency
Database transactions group operations into atomic units that either fully complete or fully roll back. Concurrency control mechanisms — locks, MVCC, isolation levels — coordinate simultaneous access so that transactions execute correctly without corrupting shared data.
itDatabases and data storage | OpenSkills.info
Course pathWalk it in order
Look it upDip in anytime
Go furtherLeaves this page
Don't Panic
Don't Panic — Database Transactions and Concurrency
A transaction is the database admitting that several statements are one business action wearing a trench coat. Moving money, reserving stock, and recording an order need every related change or none of them. Commit keeps the completed unit. Rollback removes its unfinished scribbles.
The awkwardness begins when two sessions have the same excellent idea at once. Each can read a quantity of one, each can conclude that one item remains, and together they can manufacture two purchases from one unit of inventory. The important thing is not the SQL statement in isolation. It is the invariant, the condition committed data must preserve.
That is why the transaction boundary matters. Start after external input is ready. End when the database work is done. A boundary held open for a person, an HTTP call, or a prolonged think produces locks, old row versions, and waiting sessions. A short boundary does not solve every conflict, but it keeps the disagreement confined to the part built to handle it.
Isolation decides which concurrent observations and outcomes are allowed. Read Committed, Repeatable Read, and Serializable are useful names, but they are not a pocket-sized guarantee brochure. Different products use locks, snapshots, row versions, or conflict detection in different combinations. Check the version-specific contract before deciding that a familiar label protects a particular rule.
There are three dependable moves. Use one atomic update when the rule fits in one statement. Lock current rows when the action must reserve them before a later change. Or use an optimistic version check when collisions are uncommon and a zero-row update is a useful conflict signal. None makes contention evaporate; they turn it into a result that can be observed and handled.
A deadlock is not a database tantrum. It is a wait cycle, so the database ends one transaction to let the rest continue. Retry the whole unit from fresh reads when the action is safe to repeat. Rollback can undo database writes, but it cannot unsend an email or unring a remote API. Durable intent and idempotency keys keep a retry from becoming a duplicate action.
Read the intro when the mechanisms need a fuller map. Use the slides for the sequence from invariant to concurrent test, and keep the cheatsheet nearby when choosing a boundary, classifying an error, or checking a retry path. The practical work starts with two sessions, one contested row, and enough patience to let the interleaving reveal itself.
Where this skill leads
Relevant careers
See how this topic contributes to broader role-level skill maps.
Sources
- https://www.postgresql.org/docs/current/tutorial-transactions.html
Supports
- Transactions group multiple steps into an atomic all-or-nothing operation
- Commit makes a completed transaction durable under PostgreSQL's guarantees
- Other transactions do not see intermediate states
- Rollback discards the transaction's updates
- Savepoints support partial rollback inside a transaction
- https://www.postgresql.org/docs/current/transaction-iso.html
Supports
- The SQL isolation levels are defined through prohibited concurrency phenomena
- Dirty reads, nonrepeatable reads, phantom reads, and serialization anomalies have distinct definitions
- Serializable execution has an effect consistent with some serial order
- PostgreSQL Read Committed uses a new snapshot for each command
- PostgreSQL Repeatable Read and Serializable transactions can require complete retries
- https://www.postgresql.org/docs/current/mvcc-intro.html
Supports
- PostgreSQL uses MVCC to provide statements with snapshots of data
- Reading and writing do not conflict in PostgreSQL's MVCC model
- Explicit locks remain available where application-controlled conflict points are needed
- https://www.postgresql.org/docs/current/explicit-locking.html
Supports
- PostgreSQL provides table, row, page, and advisory locking facilities
- Conflicting lock modes cannot be held by different transactions on the same resource
- Row-level locks block writers and lockers rather than ordinary readers
- Deadlocks can arise from explicit or row-level locks
- PostgreSQL detects a deadlock and aborts one participating transaction
- Consistent lock acquisition order is the primary defense against deadlocks
- https://www.postgresql.org/docs/current/sql-update.html
Supports
- UPDATE supports conditions based on current column values
- UPDATE reports the number of rows affected
- A version predicate and affected-row count can implement an application-level optimistic check
- https://dev.mysql.com/doc/refman/8.4/en/innodb-transaction-isolation-levels.html
Supports
- InnoDB implements Read Uncommitted, Read Committed, Repeatable Read, and Serializable
- InnoDB defaults to Repeatable Read
- InnoDB isolation levels change snapshot and locking behavior
- https://dev.mysql.com/doc/refman/8.4/en/innodb-locking-reads.html
Supports
- InnoDB provides FOR SHARE and FOR UPDATE locking reads
- Locking reads protect selected current rows until commit or rollback
- NOWAIT returns an error instead of waiting for a lock
- SKIP LOCKED omits locked rows and is unsuitable for a general consistent view
- https://dev.mysql.com/doc/refman/8.4/en/innodb-deadlocks.html
Supports
- InnoDB detects deadlocks and rolls back a victim transaction
- Applications must handle complete transaction retries
- Short transactions, consistent operation order, and useful indexes reduce deadlock likelihood
- https://dev.mysql.com/doc/refman/8.4/en/innodb-error-handling.html
Supports
- InnoDB deadlocks roll back the entire transaction
- Lock wait timeout rollback scope differs from deadlock rollback scope by default
- Applications should classify deadlocks and lock timeouts and retry at the correct scope
- https://learn.microsoft.com/en-us/sql/relational-databases/sql-server-transaction-locking-and-row-versioning-guide?view=sql-server-ver17
Supports
- SQL Server combines locking and row-version-based isolation
- Isolation levels control allowed concurrency effects and read behavior
- Row versioning can provide statement-level or transaction-level snapshots
- Row versioning reduces shared read locks but consumes version-store resources
- Higher isolation can increase blocking and resource use
- https://learn.microsoft.com/en-us/sql/relational-databases/sql-server-deadlocks-guide?view=sql-server-ver17
Supports
- Consistent access order and short transactions reduce deadlock likelihood
- User interaction inside transactions increases lock duration and blocking
- Deadlock victims should be handled by application retry logic
- https://learn.microsoft.com/en-us/azure/architecture/databases/guide/transactional-out-box-cosmos
Supports
- A transactional outbox stores a business object and its event in the same transaction
- A worker publishes pending events after the transaction commits
- Consumers should be idempotent because message delivery can repeat
- https://www.postgresql.org/
Supports
- PostgreSQL is a database product in the landscape.
- https://www.mysql.com/
Supports
- MySQL is a database product in the landscape.
- https://www.microsoft.com/sql-server/
Supports
- SQL Server is a database product in the landscape.
- https://www.oracle.com/database/
Supports
- Oracle Database is a database product in the landscape.
- https://www.cockroachlabs.com/product/
Supports
- CockroachDB is a database product in the landscape.
- https://www.yugabyte.com/yugabytedb/
Supports
- YugabyteDB is a database product in the landscape.
- https://www.pingcap.com/tidb/
Supports
- TiDB is a database product in the landscape.
- https://www.mongodb.com/
Supports
- MongoDB is a database product in the landscape.
- https://dl.acm.org/doi/10.1145/320128.320138
Supports
- The 1978 paper documented predicate-lock concepts.
- https://dl.acm.org/doi/10.1145/322234.322242
Supports
- The 1981 paper described optimistic concurrency control.
- https://www.microsoft.com/en-us/research/publication/a-critique-of-ansi-sql-isolation-levels/
Supports
- The 1995 paper analyzed ANSI SQL isolation definitions.
- https://www.postgresql.org/docs/8.0/release-8-0.html
Supports
- PostgreSQL 8.0 was released in 2005 and added savepoints and NOWAIT locking.
- https://www.postgresql.org/docs/9.1/release-9-1.html
Supports
- PostgreSQL 9.1 was released in 2011 and added true Serializable isolation.
- https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-1.html
Supports
- MySQL 8.0.1 added NOWAIT and SKIP LOCKED locking reads.
