Databasemaxxing: Grasping MVCC

on 2026-11-07

One of the great aspects of MVCC is allowing readers and writers to operate concurrently without unnecessarily blocking each other, which is achieved by versioning rows. Readers can see a consistent snapshot of the data whilst writers create new versions of it, generally avoiding the need for readers and writers to block each other.

Isolation levels in MVCC are particularly fascinating due to a "mix 'n' max" approach you could take with them based on your business problems. Postgres, for example, defaults to READ COMMITTED as its base isolation level unless explicitly specified.

Isolation levels

  • READ UNCOMMITTED -> allows reading uncommitted changes made by another transaction
  • READ COMMITTED -> only allows committed changes to be read. Subsequent statements within the same transaction can see changes committed by other transactions
  • REPEATABLE READ -> all queries within the transaction operate against the same snapshot, so changes committed by other transactions after that snapshot was established aren't visible
  • SERIALIZABLE -> guarantees that the outcome of concurrently executing transactions is equivalent to some serial execution of those transactions

Note: implementations of isolation levels may vary across databases. For example, Postgres doesn't provide a READ UNCOMMITTED implementation and treats it as READ COMMITTED instead.

Everything I've written here is from the heart so if anything is wrong, please do correct me! I'll also be using Postgres as the example too.

What's interesting about READ COMMITTED to me is that it's a reasonable default. Some applications may not be affected by anomalies such as read skew or phantom reads. READ COMMITTED can offer greater concurrency and avoid some of the overhead associated with stronger isolation guarantees depending on the workload.

Txn B waiting on Txn A

Txn B waiting on Txn A

Two competing transactions looking to update the same row will have lock contention, resulting in one winning and doing its work whilst the other halts once its update command was submitted, and must wait until it can proceed.

An interesting detail here that I learnt from Postgres is that once the waiting transaction can proceed, Postgres re-evaluates the WHERE condition against the updated row version. If the row no longer satisfies the condition, the update won't affect it.

Right side transaction fails to update due to predicate failing

Right side transaction fails to update due to predicate failing

Now let's say we have some timeouts configured based on our requirements, this is where things get interesting and what you'd want to pay special attention to.

lock_timeout -> Aborts a statement if it waits too long

idle_in_transaction_session_timeout -> Terminates sessions that remain idle within an open transaction for too long

deadlock_timeout -> Determines how long Postgres waits before checking for a deadlock when a transaction is blocked. If detected, one of the transactions will be aborted to break it

deadlock example

deadlock example

The idle transaction timeout is important to understand because during a transaction, the database will be waiting for something your application is doing e.g. making an API call to something that's taking longer than expected and could also timeout. Therefore, it's protecting the database as a safety net against application behaviour.

lock_timeout limits how long a statement can wait to acquire a lock, whilst deadlock_timeout determines when Postgres checks whether blocked transactions have formed a deadlock.

Read skew and write skew are another two cool problems since they're anomalies that come from the interaction between transaction isolation and application behaviour.

  • Read skew: a transaction observes data from inconsistent points in time, potentially seeing a combination of values that never existed together in a consistent database state
  • Write skew: two concurrent transactions independently make valid decisions based on their snapshots, but their combined writes violate a business constraint

This is where you'd consider bumping up the isolation level to REPEATABLE READ so that Txn B has a consistent snapshot regardless of when Txn A commits.

To re-emphasise, READ COMMITTED allows successive statements within a transaction to observe different committed states, whereas REPEATABLE READ retains the same snapshot for the transaction's duration.

Write skew involves application business constraints such that you will most likely need to lean on SERIALIZABLE, or database constraints if they can be added.

Unlike read skew, where REPEATABLE READ maintains a consistent snapshot for the transaction's lifetime, it doesn't prevent two transactions from independently updating different rows in ways that collectively violate a business constraint.

My example is very simple (A or B must be 1, both cannot be 0). READ COMMITTED could theoretically prevent this particular outcome if the execution order happened to line up, with Txn A committing before Txn B takes its snapshot, but I wouldn't gamble on that.

REPEATABLE READ isn't going to be good enough in this case. SERIALIZABLE uses serialisable snapshot isolation (SSI) to track read/write dependencies and detect dangerous dependency structures that could result in a non-serialisable execution.

Write skew in 'Read Committed'

Write skew in 'Read Committed'

Write skew avoided in 'Serialisable'

Write skew avoided in 'Serialisable'

The database doesn't understand the business constraint itself, but it can detect dangerous concurrent read/write dependencies that could result in a non-serialisable execution. Postgres aborts one of the transactions to preserve serialisability, allowing the application to retry the entire transaction if appropriate.

There's a lot more that I haven't mentioned here (e.g. phantoms) but these are all good to know.