Databases & System Design•Part 2: Relational Databases (RDBMS / PostgreSQL / MySQL) (6 - 25)
SQL Transaction Isolation Levels:
In Simple Words: Controls how visible uncommitted changes from other transactions are:
- Read Uncommitted: Lowest isolation; allows dirty reads.
- Read Committed (Default in Postgres): Reads only committed rows.
- Repeatable Read (Default in MySQL InnoDB): Guarantees the same row read twice inside a transaction returns identical data.
- Serializable: Highest isolation; transactions execute as if run strictly one after another.
sql
-- Setting isolation level for a sensitive financial transaction:
BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
SELECT balance FROM accounts WHERE id = 42;
UPDATE accounts SET balance = balance - 500 WHERE id = 42;
COMMIT;