Locks: Foundations of Concurrent Control
Three Problems Locks Solve ๐ฏ
3 Transactions (T1, T2, T3) doing some stock trading on X, Y accounts
๐ซ Problem 1: Correctness
Lost Updates
โ Without Locks:
T2: READ(x)=100
T1: WRITE(x=150)
T2: WRITE(x=130) โ
โ T1's update lost!
โ With Locks:
T2: [waits]โLOCKโREAD(150)โ
โฑ๏ธ Problem 2: Long Calculations
CPU Idle During Complex Logic
โ Serial Execution:
T2: Wait... then process data Y
T3: Wait... wait... then process data Z
โ Only 1 CPU core used!
โ Parallel Execution:
T2: LOCK(Y) โ Process Y (parallel!)
T3: LOCK(Z) โ Process Z (parallel!)
โ All CPU cores utilized!
๐พ Problem 3: IO Lag
CPU Idle During Disk Access
Read Operations:
Rs = Read Start (CPUโDisk)
Re = Read End (DiskโCPU)
Gap = ~5ms of waiting
Write Operations:
Ws = Write Start (CPUโDisk)
We = Write End (Disk confirms)
Gap = ~10ms of waiting
โ
With Locks:
T2 uses CPU during T1's IO gaps
The Lock Cycle: How It Works ๐
3 Transactions (T1, T2, T3) competing for 2 Concert Tickets (seat A, seat B)
Key Insight: T1 and T3 can work simultaneously (different tickets), but T2 must wait for T1.
Legend: Operation Types
Step-by-Step Lock Lifecycle
During IO gaps (RsโRe), locks coordinate access between T1, T2, T3:
1๏ธโฃ Request
Transaction asks for lock on concert ticket
T3: REQ_LOCK(Ticket_B, lock)
2๏ธโฃ Get
Lock manager checks who gets access
Waiting: T2 waits
3๏ธโฃ Use Data
Read ticket data (IO) then process purchase (Logic)
Logic: Calculate total, apply discount
4๏ธโฃ Unlock
Release lock so others can buy
After: We (write confirmed)
Types of Locks: Why We Need Two Kinds ๐
The Opportunity: Reads vs Writes are Different
Key Insight: Reading doesn't change data, writing does. So we can optimize!
๐ Shared Lock (S-lock)
For Reading Data
Real Example: Checking Seat A-15 Availability
T2: S-LOCK(Seat_A15) โ READ status = "available"
T3: S-LOCK(Seat_A15) โ READ status = "available"
โ
All can check availability simultaneously!
Everyone can look at the same seat's status at once without interfering.
โ๏ธ Exclusive Lock (X-lock)
For Writing Data
Real Example: Purchasing Seat A-15
T2: [BLOCKED] Can't even check availability
T3: [BLOCKED] Can't check or buy
๐ซ Only one can buy (or even look) at a time!
During purchase, no one else can read OR write to prevent seeing partial updates.
Why This Design is Smart ๐ง
๐ Maximum Browsing
Multiple transactions can read the same data simultaneously (S + S = โ )
Example: 1000 customers can check seat A-15's availability at the same time
๐ก๏ธ Purchase Integrity
No transaction can read while another is writing (S + X = โ)
Example: When someone is buying seat A-15, others can't even check it (prevents seeing partial updates)
๐ No Double-Booking
Only one transaction can write at a time (X + X = โ)
Example: Only one person can complete purchase of seat A-15
Lock Compatibility Matrix
| ๐ Shared (S) | โ๏ธ Exclusive (X) | |
|---|---|---|
| ๐ Shared (S) | โ
Compatible Many readers OK |
โ Conflict No read during write |
| โ๏ธ Exclusive (X) | โ Conflict No write during read |
โ Conflict Only one writer |
Example: Bank Transfer with IO Timing
-- Transaction: Transfer $100 from Account A to Account B
BEGIN TRANSACTION
-- Before Rs: Request locks
LOCK_EXCLUSIVE(Account_A) -- Granted instantly
LOCK_EXCLUSIVE(Account_B) -- Granted instantly
-- Trigger RsโRe: Read operations
READ balance_A FROM Account_A -- Rs_A โ ... โ Re_A (balance_A = 500)
READ balance_B FROM Account_B -- Rs_B โ ... โ Re_B (balance_B = 200)
-- Process data (CPU time)
balance_A = balance_A - 100 -- Account_A = 400
balance_B = balance_B + 100 -- Account_B = 300
-- Trigger WsโWe: Write operations
WRITE(Account_A, 400) -- Ws_A โ ... โ We_A
WRITE(Account_B, 300) -- Ws_B โ ... โ We_B
-- After We: Release locks
UNLOCK(Account_A)
UNLOCK(Account_B)
COMMIT
Key Point: During the RsโRe and WsโWe gaps, other transactions can acquire locks on different accounts and make progress!