Sub topic 14
Choosing an Isolation Level for Your Workload
Assume you know basic SQL, primary keys, and transaction commands such as BEGIN, COMMIT, and ROLLBACK. This chapter focuses on choosing a useful guarantee without paying for stronger isolation than your workload needs.
Start with the business rule
Isolation is a trade-off. Stronger isolation makes concurrent transactions behave more like a queue, but it can increase waiting, deadlocks, and transaction aborts. Weaker isolation allows more concurrency, but application code must tolerate more changes between statements.
Ask these questions:
- Can a transaction read data that another transaction has not committed?
- Must two reads in one transaction return a consistent view?
- Does a rule involve several rows, such as “at least one doctor must remain on duty”?
- Can the application retry a transaction after a serialization failure?
- Is a short lock on one row simpler than a stronger isolation level for the whole transaction?
A practical decision table
| Workload or requirement | Starting choice | Additional action |
|---|---|---|
| Independent reads and simple writes | Read committed | Use atomic `UPDATE` expressions |
| A report needs one consistent view | Repeatable read or snapshot | Keep the transaction short |
| Several rows must satisfy one rule | Serializable | Retry serialization failures |
| One known row is being edited | Read committed | Use `SELECT ... FOR UPDATE` |
| Long analytical query | Database-specific snapshot mode | Avoid holding write locks |
Read committed is often the default because each statement sees committed data available when that statement begins. A later statement can see a newer committed version. This works well when each statement is independent.
Repeatable read or snapshot isolation gives a transaction a stable view. It is useful when several queries must describe the same point in time. It does not always protect a rule involving rows that the transaction did not update.
Serializable is the safest general choice when correctness depends on the combined result of concurrent transactions. The database may reject one transaction rather than allow an invalid outcome. The application must then retry the complete transaction.
Prefer narrow protection when possible
Do not raise the isolation level to solve a problem that concerns one row. An atomic update often gives a clearer and cheaper guarantee:
UPDATE accounts
SET balance = balance + 10
WHERE id = 1;This statement does not read a balance into application memory and later overwrite another transaction's change.
When the application needs to inspect a row before changing it, lock that row explicitly:
BEGIN;
SELECT balance
FROM accounts
WHERE id = 1
FOR UPDATE;
-- Validate the balance in application code, then update it.
UPDATE accounts
SET balance = balance - 10
WHERE id = 1;
COMMIT;The lock is held until the transaction ends. Keep the transaction short, and acquire multiple locks in a consistent order to reduce deadlocks.
Use serializable for multi-row invariants
Suppose two transactions each check that at least one doctor is on duty, then each turns a different doctor off. A row lock on only the changed row does not necessarily protect the rule. Use serializable isolation, or explicitly lock a row that represents the shared resource.
Retries are part of the design
Serializable transactions can fail with a serialization error. Deadlocks can also abort a transaction. Retry the entire transaction, including all reads, writes, and validation. Do not retry only the statement that failed, because the transaction's earlier assumptions may no longer be valid.
Use a small limit on retries and record failures for monitoring. If retries remain frequent, shorten transactions, reduce contention, access rows in a consistent order, or redesign the workflow.
Exercise
A transaction checks a multi-row rule, changes one row, and must prevent another transaction from making the rule false. What is a suitable starting choice?
Show answer
Use serializable isolation and retry the complete transaction after a serialization failure. If retries are impossible, use explicit coordination or redesign the workflow.