Skip to content
StackDecode
System Design
4. Database FundamentalsAnimated

Database Locks

Mechanisms that stop transactions clashing.

The idea

Shared locks allow reads; exclusive locks allow one writer. Locks can be on rows, pages or tables.

Watch it work

Press play, pause, or step through. Some animations have settings you can change.

Txn 1Row ARow BTxn 2lock A
  • current message
  • earlier
  • failure or attack

Txn 1 locks row A.

step:
1/6
from:
Txn 1
to:
Row A
1/6

Time runs downward. Solid arrows are requests, dashed arrows are replies.

Step by step

  1. 1A transaction requests a lock
  2. 2It is granted or waits
  3. 3Work is done
  4. 4Locks release at commit

Where it is used

Concurrent updates.

Watch out for

Lock waits and deadlocks.

Related