Sub topic 2
What Goes Wrong When Transactions Run Concurrently
Assume you know basic SELECT, UPDATE, and DELETE, and you have used BEGIN, COMMIT, and ROLLBACK. Also assume you know that a database serves many clients at the same time.
A transaction is a group of database operations treated as one unit. Isolation is the part of ACID that controls what one transaction can see while other transactions are running. Stronger isolation reduces interference, but it can also reduce concurrency, increase waiting, or cause a transaction to fail and require a retry.
A Shared Schedule
Suppose an accounts table contains one account with a balance of 100. Session A and Session B are two clients using the database concurrently.
The database interleaves their statements rather than always running one complete transaction before the other.
flowchart TD
A[Session A starts] --> B[Reads or changes data]
B --> C[Session B starts]
C --> D[Reads or changes overlapping data]
D --> E[Both transactions commit]
E --> F[The final result may violate an application rule]caption: Concurrent transactions can interleave their work alt: A flow from two sessions starting, reading or changing overlapping data, committing, and possibly producing an invalid final result
The Main Anomalies
An anomaly is an unexpected result caused by the interaction of concurrent transactions.
| Anomaly | What happens | Typical consequence |
|---|---|---|
| Dirty read | A transaction reads uncommitted data | It uses a value that may be rolled back |
| Non-repeatable read | The same row returns different values | A decision changes during one transaction |
| Phantom read | A repeated search finds new or missing rows | A set-based rule becomes invalid |
| Lost update | One change overwrites another change | An update silently disappears |
| Write skew | Separate changes together violate a rule | Each transaction looks correct alone |
Dirty Reads
A dirty read occurs when Session A reads a change made by Session B before B commits. If B rolls back, A used data that never officially existed.
For example, B might change an account balance from 100 to 0. A reads 0 and refuses a withdrawal. Then B rolls back, so the real balance remains 100. Isolation levels that prevent dirty reads hide uncommitted changes from other transactions.
Non-Repeatable Reads
A non-repeatable read occurs when a transaction reads the same row twice and gets different committed values.
-- Session A
BEGIN;
SELECT balance FROM accounts WHERE id = 1; -- 100
-- Session B runs at the same time
BEGIN;
UPDATE accounts SET balance = 50 WHERE id = 1;
COMMIT;
-- Session A reads again
SELECT balance FROM accounts WHERE id = 1; -- 50
COMMIT;Session A did not change the row, but its view changed during the transaction. This matters when the first read influences a later calculation or authorization decision.
Phantom Reads
A phantom read affects a set of rows rather than one known row. Session A counts all pending orders. Session B inserts a new pending order and commits. When A repeats the count, it sees an extra row, called a phantom.
The same problem can occur when Session B deletes or updates a row so that it no longer matches A's search condition. A transaction that checks “no matching rows exist” can therefore make a decision that is no longer true.
Lost Updates and Write Skew
A lost update usually comes from a read-modify-write sequence. Both sessions read 100. A calculates 110, while B calculates 120. If each writes its calculated value, the final balance may be 110 or 120, and one change is lost.
Write skew is different. Each transaction updates a different row after checking a shared rule. For example, two doctors each see that two doctors are on duty. Each removes one doctor from the schedule. Both commits succeed, leaving nobody on duty.
Exercise
Exercise
Two transactions both read a stock quantity of 1. Each then inserts a sale and updates the quantity to 0. What anomaly can occur, and what is one way to prevent it?
Show answer
This is a lost update or an overselling race. Use an atomic conditional update such as UPDATE products SET stock = stock - 1 WHERE id = 1 AND stock > 0, then check that one row changed, or lock the row before reading it.