school Technical Deep Dive

Concurrency Control Protocols

Imagine a bank account shared by you and your partner. If you both try to withdraw money at the exact same millisecond, who gets the cash? Concurrency control is the "traffic police" of database systems that prevents chaos when multiple transactions access the same data simultaneously.

Traffic Jam Analogy

lightbulb Real World Analogy

Without protocols, a database is like a 4-way intersection without traffic lights or stop signs—crashes (data corruption) are inevitable. Protocols act as the signals, roundabouts, or police officers ensuring everyone gets through safely, even if they have to wait a bit.

lock

Lock-Based Protocols (2PL)

Two-Phase Locking (2PL) is the most common protocol. It ensures serializability by forcing transactions to acquire locks in two distinct phases. It prevents "interleaving" errors by physically locking data items.

  • 1
    Growing Phase:

    Transaction may obtain locks, but cannot release any lock.

  • 2
    Shrinking Phase:

    Transaction may release locks, but cannot obtain any new locks.

Visualizing 2PL

Time # Locks Growing Lock Point Shrinking

Analogy: The Library Rule

Imagine you are researching a paper.
Growing Phase: You run around the library grabbing all 5 books you need. You cannot sit down and read until you have them all.
Lock Point: You have all 5 books.
Shrinking Phase: As you finish a chapter, you return the books one by one to the shelf for others to use. You cannot grab new books once you start returning them.

Interactive Lock Simulator

Try to have Transaction B write to Item X while Transaction A holds a lock.

Transaction A Idle
Transaction B Idle
database Item X
System Ready.
schedule

Timestamp Ordering

Instead of locks, every transaction gets a unique timestamp when it starts (TS). The protocol ensures that for every pair of conflicting operations, the order of execution matches the order of timestamps. Older transactions (smaller TS) have priority.

Logic Flow

T1 (TS=10) Older / Senior T2 (TS=20) Younger / Junior Object A Access OK If T2 accessed A first... T1 is aborted (Restarted)
Queue Analogy

Analogy: The Ticket Queue

Think of a deli counter. Everyone pulls a numbered ticket (Timestamp).

If Person #10 wants to order a sandwich, but sees that Person #20 has already been served that last bagel, Person #10 cannot order it.

In databases, if an "older" transaction tries to read/write data that a "younger" transaction has already touched, the older one is too late. It must restart (get a new ticket).

Thomas Write Rule (Optimization) If an older transaction tries to Write a value that a younger transaction has already Overwritten, we can simply ignore the older write. Why? Because the younger value is the one that should persist anyway ("future" overwrites "past").
check_circle

Optimistic Control (Validation)

"Ask for forgiveness, not permission." Optimistic protocols assume conflicts are rare. They let transactions execute freely without locks, and check for conflicts only at the very end before committing.

R

1. Read Phase

Execute on local copies. No global writes.

V

2. Validation Phase

Check if my local changes conflict with others.

W

3. Write Phase

If validation passes, copy changes to DB.

group_work

Analogy: Collaborative Coding

Like using Git.

Read: You clone the repo and work locally on your laptop. You don't lock the main server.
Validate: You try to push (Merge Request). Git checks if anyone else modified the same lines while you were working.
Write: If no conflicts, your code merges. If conflicts, you fix (restart) and try again.

history

Multi-Version Concurrency Control (MVCC)

Used by PostgreSQL, Oracle, and MySQL (InnoDB). The system keeps multiple versions of data items.
Readers don't block Writers, and Writers don't block Readers.

Database State (Visualized)

Row ID: 101 Value: 50 Created by T1
Ver 1
Row ID: 101 Value: 80 Created by T5
Ver 2 (Current)
visibility Reader T3

Started before T5 finished.

Reads Ver 1

star

Interview Quickfire

Common Questions