LightMySky

Concurrency Control: Locks and Snapshots

Two-phase locking makes transactions wait for each other and can deadlock. Multi-version concurrency control gives each transaction a consistent snapshot instead, so readers never block writers, at the price of keeping old versions around.

No account needed. Progress saves in this browser.

What a learner can do afterwards

  • Explain what the two phases of two-phase locking are and why the order matters
  • Say what a transaction sees under snapshot isolation and when that view was taken
  • Describe a write conflict that a snapshot system has to detect at commit

1 · Read

Isolation says each transaction must behave as if it has the database all to itself, even while transactions overlap. Two strategies keep that promise. Locking makes transactions wait for each other. Snapshots give each transaction a consistent picture instead, so readers never block writers, at the price of keeping old versions around.

Try it together

With two-phase locking, a transaction first grows: it collects every lock it needs and releases none. Then it shrinks: it releases locks and takes no new ones. The order matters. Releasing early and taking another lock later would let a second transaction slip a change in between, and the first would lose its clean private view.

With snapshots, each transaction reads from a picture of the database taken when it started, no matter what others commit meanwhile. A long report can run to the end without ever blocking a writer. The database pays by keeping old row versions around so every open picture stays readable.

Good to know

Waiting has one danger: deadlock. Two transactions each hold a lock the other wants, and then neither moves, so the system must spot the standstill and cancel one side. Snapshots have their own clash: two transactions changing the same row from the same picture must be caught at commit, and only one wins.

Locks make transactions wait in two phases, snapshots let them proceed from private pictures, and each path has its own clash.

2 · Watch

Take it off screen

Print a worksheetA4 with an answer key page for grown-ups. No screen, no internet.

Where it sits

Then practise

8 questions wait behind this lesson, each with its answer explained. Every answer feeds the sky: stars light as they are learned, and dim when it is time to come back.

Spotted a problem on this page? Tell us
Concurrency Control: Locks and Snapshots · Computing, ages 20 to 21 · LightMySky