0Pricing
SQL Interview Prep · Lesson

Deadlocks, Locking and MVCC

How databases avoid conflicts and the locking vs snapshot trade-offs.

How Databases Actually Enforce Isolation

Isolation levels are the promise; locking and MVCC are the mechanisms that deliver it. Interviewers ask about these to see if you understand what happens under the hood when transactions collide.

There are two broad strategies:

  • Pessimistic (locking): block conflicting access until a lock is released.
  • Optimistic / MVCC: let everyone read a consistent snapshot and detect conflicts at commit.

This lesson covers locks, deadlocks, and MVCC, plus the trade-offs between them.

Shared vs Exclusive Locks

Classic locking uses two main modes:

  • Shared (S) lock for reads. Many transactions can hold a shared lock on the same row at once.
  • Exclusive (X) lock for writes. Only one transaction can hold it, and it blocks all other locks on that row.

The rule: S is compatible with S, but X is compatible with nothing. A writer must wait for all readers, and readers must wait for a writer.

All lessons in this course

  1. ACID Properties Explained
  2. The Four Isolation Levels
  3. Dirty, Non-Repeatable and Phantom Reads
  4. Deadlocks, Locking and MVCC
← Back to SQL Interview Prep