4.4.19Databases

Locking — shared, exclusive, intent locks

2,320 words11 min readdifficulty · medium

WHY do we even need locks?

WHAT we lock: rows, pages, tables (granularity). HOW we enforce correctness: a transaction must acquire the right lock before it reads/writes, and hold it (usually until commit, under Two-Phase Locking).


The two base lock modes

Compatibility — derived from first principles

Ask: can two requests coexist without an anomaly?

Existing →
Requested ↓
S X
S ✅ compatible ❌ conflict
X ❌ conflict ❌ conflict

WHY each cell:

  • S–S ✅: two readers see the same unchanged value → safe.
  • S–X ❌: a writer would change data a reader is depending on → unsafe.
  • X–S ❌: reader would see an uncommitted/changing value → unsafe.
  • X–X ❌: two writers = lost update → unsafe.

Intent locks — the multi-granularity problem

HOW locking a row works (rule): to get S on a row → first get IS on the table. To get X on a row → first get IX on the table.

Full compatibility matrix (multi-granularity)

IS IX S SIX X
IS
IX
S
SIX
X
Figure — Locking — shared, exclusive, intent locks

Worked examples


Common mistakes


Recall Feynman: explain it to a 12-year-old

Think of a coloring book everyone shares. If a few kids just want to look at a page, they can all crowd around — that's a shared lock. But if one kid wants to color a page, she takes it away so no one else can look or color — that's an exclusive lock. Now the book is huge, so before grabbing a page, you stick a flag on the cover saying "Hey, I'm using a page inside!" — that's an intent lock. The flag lets a teacher who wants the whole book instantly know it's busy, without flipping through every page.


Flashcards

What does a Shared (S) lock permit?
Concurrent reads — multiple transactions may hold S on the same item; blocks any X.
What does an Exclusive (X) lock permit?
A single writer; no other S or X on that item.
Which lock-mode pairs are compatible among S and X?
Only S–S. S–X, X–S, X–X all conflict.
What is an Intent Shared (IS) lock?
A coarse-level flag announcing the transaction holds/will hold an S lock on some finer-grained child.
What is an Intent Exclusive (IX) lock?
A coarse-level flag announcing the transaction holds/will hold an X lock on some child.
Why are IX and IX compatible?
They only announce writes below; two transactions can write different rows, so no real conflict at the coarse node.
Why is IX incompatible with S?
S locks the whole node for reading; an IX means someone may be writing a child, which could change data the reader sees.
What problem do intent locks solve?
They avoid scanning all child rows (O(n)O(n)) to detect a conflict; a coarse-level check is O(1)O(1).
What does SIX mean?
Shared lock on the whole node PLUS Intent-Exclusive for some children: read everything, write a few rows.
Before getting an X lock on a row, what must you acquire first?
An IX (intent exclusive) lock on its parent (table), per the multi-granularity protocol.
Name three anomalies locking prevents.
Lost update, dirty read, non-repeatable read.

Connections

  • Two-Phase Locking — when locks are acquired/released to guarantee serializability.
  • ACID Properties — Isolation is largely implemented via locking.
  • Isolation Levels — Read Committed / Repeatable Read / Serializable tune lock duration.
  • Deadlocks — locks held in conflicting order cause cycles; detection & victims.
  • MVCC — alternative to read-locks: readers see snapshots instead of taking S locks.
  • Lock Granularity — row vs page vs table tradeoffs that motivate intent locks.

Concept Map

causes

includes

includes

includes

prevents

split into

split into

compatible with

conflicts with

conflicts with

held until commit under

needs

flags table level for

avoids scanning all rows O n

Concurrent access

Anomalies

Lost update

Dirty read

Non-repeatable read

Lock mechanism

Shared lock S read

Exclusive lock X write

Two-Phase Locking

Multi-granularity locking

Intent locks

Row-level locks

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, database me jab do transactions ek hi data ko ek saath touch karte hain, to gadbad ho sakti hai — jaise dono ne balance padha, dono ne badhaya, par ek update kho gaya (lost update). Isko rokne ke liye lock lagate hain. Do main type hote hain: Shared (S) matlab "main sirf padhna chahta hoon" — bahut saare log ek saath padh sakte hain, no problem. Exclusive (X) matlab "main likhna chahta hoon" — ab pura item akele chahiye, koi aur padh ya likh nahi sakta. Yaad rakho: S–S compatible hai, baaki sab (S–X, X–X) conflict hai. Simple rule: padhna share karo, likhna selfish hai.

Ab problem ye hai ki agar koi transaction ek row pe X lock laga de, aur dusra transaction pure table ko lock karna chahe, to use kaise pata chale ki andar koi row busy hai? Agar har row scan kare to bahut slow ho jayega (O(n)O(n)). Isi liye intent lock aaye. Row pe lock lagane se pehle, transaction table pe ek chhota sa flag laga deta hai — IS (andar S lagega) ya IX (andar X lagega). Ab table-lock chahne wala bas us flag ko check karta hai, O(1)O(1) me kaam ho gaya.

Sabse important confusion: IX–IX compatible hai! Logon ko lagta hai "Exclusive" word hai to conflict hoga, par nahi — IX sirf announce karta hai ki "main neeche kuch likhunga," asli X lock to row level pe lagta hai. Do transactions alag-alag rows likh rahe hain to dono IX le sakte hain, koi dikkat nahi. SIX ka matlab hai poora table padh raha hoon + kuch rows likhne ka iraada — scan karke kuch update karne wale kaam me kaam aata hai.

Ye sab important kyun hai? Kyunki real databases me hazaaron transactions ek saath chalte hain. Sahi locking se aapko isolation (ACID ka I) milta hai bina performance maare. Agar locks galat samjhe to deadlock, slow queries, ya data corruption — sab ho sakta hai. Interview me bhi compatibility matrix bahut poocha jaata hai!

Go deeper — visual, from zero

Test yourself — Databases

Connections