MVCC, Isolation Anomalies, Locking, and Deadlocks
Multi-Version Concurrency Control (MVCC) allows transactions to read versions of records according to a snapshot while writers create newer versions. This reduces some read/write blocking, but old versions need cleanup. Long-running transactions may prevent cleanup and increase storage pressure.
Isolation levels describe which anomalies may occur. Read committed prevents dirty reads but may allow non-repeatable reads. Snapshot isolation provides repeatable snapshots but can still permit write skew in some systems. Serializable isolation aims for behavior equivalent to serial execution, often using locks or conflict detection. Exact semantics differ between database engines, so consult the implementation's guarantees.
Deadlocks occur when transactions hold resources while waiting for one another. Reduce risk by keeping transactions short, acquiring resources in consistent order, and indexing predicates. Retry an aborted transaction only when its logic is safe to repeat. Avoid holding locks while waiting on remote services.
- Isolation level names do not mean identical behavior across databases.
- Long-running snapshots can delay cleanup of old versions.
- Keep transactions short and make retries safe.