Database Serializability Complete Guide

1. Database Serializability Explained

What is Serializability?

Serializability ensures that concurrent transactions produce the same result as if they were executed one after another (serially). It guarantees data consistency even with multiple transactions running simultaneously.

Practical Example: Banking System

Two transactions (T1 and T2) transferring money between accounts:

Initial balances:

Transaction 1 (T1): Transfer $100 from A to B
BEGIN TRANSACTION;
UPDATE Accounts SET balance = balance - 100 WHERE id = 'A';
UPDATE Accounts SET balance = balance + 100 WHERE id = 'B';
COMMIT;
Transaction 2 (T2): Transfer $200 from B to A
BEGIN TRANSACTION;
UPDATE Accounts SET balance = balance - 200 WHERE id = 'B';
UPDATE Accounts SET balance = balance + 200 WHERE id = 'A';
COMMIT;

Execution Scenarios

Non-Serializable Execution

Time T1 T2 A B
1 UPDATE A = A - 100 (A=900) 900 1000
2 UPDATE B = B - 200 (B=800) 900 800
3 UPDATE B = B + 100 (B=900) 900 900
4 UPDATE A = A + 200 (A=1100) 1100 900

Result: B = $900 (Expected: $1000) ❌ Inconsistency!

Problem: Lost Update - T2's deduction was overwritten by T1's addition.

Serializable Execution

The database ensures transactions run as if in sequence (T1 → T2 or T2 → T1).

Option 1: T1 Runs First, Then T2
Time T1 T2 A B
1 A = A - 100 (A=900) 900 1000
2 B = B + 100 (B=1100) 900 1100
3 COMMIT 900 1100
4 B = B - 200 (B=900) 900 900
5 A = A + 200 (A=1100) 1100 900
6 COMMIT 1100 900

Result: A = $1100, B = $900 ✅ Correct

Enforcement Mechanisms

  1. Two-Phase Locking (2PL): T1 locks A & B until commit → T2 waits
  2. Optimistic Concurrency Control: Checks conflicts at commit time
  3. Snapshot Isolation (MVCC): Each transaction sees a consistent snapshot

2. Conflict Serializability Rules

Fundamental Concepts

Conflict Serializability ensures concurrent execution matches some serial execution of the same transactions.

Key Definition:

A schedule is conflict-serializable if it can be transformed into a serial schedule by swapping non-conflicting operations while maintaining the order of conflicting operations.

Conflict Operation Rules

Two operations conflict if:

Condition Description Example
1 Different transactions T1 and T2 (not same transaction)
2 Same data item Both work on variable 'A'
3 At least one write Read-Write, Write-Read, Write-Write

Types of Operation Conflicts

Operation Pair Conflict? Type Explanation
Read-Read No Non-conflict Multiple reads don't change data
Read-Write Yes Conflict Read might use stale data
Write-Read Yes Conflict Read depends on write order
Write-Write Yes Conflict Final value depends on order

Determining Conflict Serializability

  1. Identify conflicts in the schedule
  2. Build precedence graph:
    • Nodes = transactions
    • Edge T1 → T2 if conflict exists
  3. Check for cycles:
    • No cycles = Conflict-serializable
    • Cycles = Not conflict-serializable

Example Analysis:

Schedule: T1:R(X), T2:W(X), T1:W(X), T2:R(Y)

Conflicts: T1:R(X) → T2:W(X) and T2:W(X) → T1:W(X)

Precedence graph: T1 → T2 → T1 (cycle → not serializable)


3. Conflict-Serializable Schedule Analysis

Given Schedule

Schedule S:

  1. T1: Read(A)
  2. T2: Write(B)
  3. T3: Read(C)
  4. T1: Write(C)
  5. T2: Read(A)
  6. T3: Write(B)

Precedence Graph Construction

Conflict Operation 1 Operation 2 Type Dependency
1 T1: Write(C) T3: Read(C) Write-Read T3 → T1
2 T2: Write(B) T3: Write(B) Write-Write T3 → T2
3 T1: Read(A) T2: Read(A) Read-Read No conflict

Precedence Graph:

T1
←
T3
←
T2
Cycle Detected: T1 → T3 → T2 → T1

Conflict-Serializability Check

The schedule is NOT conflict-serializable because:

Alternative Serializable Schedule

Modified Schedule S':

  1. T2: Write(B)
  2. T2: Read(A)
  3. T3: Read(C)
  4. T3: Write(B)
  5. T1: Read(A)
  6. T1: Write(C)

Equivalent Serial Schedule: T2 → T3 → T1

Verification:


Key Takeaways