Sub topic 3
Dirty Reads in Concurrent Transactions
This chapter assumes you can write basic SELECT and UPDATE statements, use BEGIN, COMMIT, and ROLLBACK, and understand that several clients can use one database at the same time.
A dirty read happens when one transaction reads data changed by another transaction before that change is committed. The first transaction might later roll back, so the reader has used a value that never becomes part of the database’s committed history.
A two-transaction example
Suppose an account has a balance of 100. Session A starts a transaction and changes the balance to 50. Session A has not committed yet. Session B reads the balance and sees 50. Session A then rolls back. The real balance returns to 100, but Session B already acted on the temporary value.
Run the following blocks in two database sessions. This example uses SQL Server syntax.
-- Run once to create the example data
CREATE TABLE accounts (
id int PRIMARY KEY,
balance int NOT NULL
);
INSERT INTO accounts (id, balance) VALUES (1, 100);
-- Session A
SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
BEGIN TRANSACTION;
UPDATE accounts
SET balance = 50
WHERE id = 1;
-- Leave this transaction open for now.
-- Do not commit yet.
-- Session B
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
SELECT balance
FROM accounts
WHERE id = 1;
-- May return 50, even though Session A has not committed.
-- Session A
ROLLBACK TRANSACTION;
-- Session B can now run the SELECT again.
-- It returns 100.READ UNCOMMITTED is an isolation level that allows a transaction to read uncommitted changes. The word “uncommitted” matters: another transaction can still change or discard those changes.
A dirty read can produce incorrect decisions. For example, an application might display a payment as successful, reserve inventory, or send a notification based on a value that is later rolled back.
Which levels allow dirty reads?
Under the SQL standard, dirty reads are permitted only at READ UNCOMMITTED.
| Isolation level | Dirty reads permitted by the standard? | Typical meaning |
|---|---|---|
| READ UNCOMMITTED | Yes | Reads may include uncommitted changes |
| READ COMMITTED | No | Reads see committed data |
| REPEATABLE READ | No | Earlier reads remain stable |
| SERIALIZABLE | No | Transactions behave as if run one at a time |
The exact implementation depends on the database engine. PostgreSQL accepts the name READ UNCOMMITTED but treats it as READ COMMITTED, so PostgreSQL does not expose dirty reads through that setting. SQL Server and InnoDB can allow them when READ UNCOMMITTED is selected.
Preventing dirty reads
Use at least READ COMMITTED when an operation must rely on committed data. This prevents a transaction from reading another transaction’s uncommitted update. It does not prevent every concurrency problem. A later chapter covers non-repeatable reads, phantoms, and stronger levels.
Exercise
Session A updates a row but has not committed. Session B reads the new value, and Session A then rolls back. What kind of anomaly occurred, and which standard isolation level permits it?
Show answer
A dirty read occurred. The standard permits it at READ UNCOMMITTED.