Skip to main content

Conflict Serializability & Precedence Graphs

πŸ“šModule 01Topic 1.1⏱️5 min read
🎯High-Yield For:Semester Exams β€’ GATE CSE β€’ Technical Interviews

πŸ’‘ Core Intuition​

🍳 The Everyday Analogy: The Shared Kitchen Chore Board​

Imagine two roommates, Alex (T1T_1) and Blake (T2T_2), sharing an apartment kitchen:

  • In the morning, Alex washes the shared chef's pan (W1(Pan)W_1(\text{Pan})). Immediately afterward, Blake cooks eggs in it (R2(Pan)R_2(\text{Pan})). For this to make sense, Alex must execute before Blake: T1β†’T2T_1 \to T_2.
  • But later that afternoon, Blake stocks the shared fridge with milk (W2(Milk)W_2(\text{Milk})), and Alex drinks from it (R1(Milk)R_1(\text{Milk})). Here, Blake must execute before Alex: T2β†’T1T_2 \to T_1.

Notice the logical deadlock: for the pan chore, Alex must go before Blake (T1β†’T2T_1 \to T_2), but for the milk chore, Blake must go before Alex (T2β†’T1T_2 \to T_1). There is no valid chronological order in which one person does all their chores before the other without altering reality. A cycle of dependency has formed.

In database systems, a Precedence Graph tracks these exact directional dependencies between transactions. If the graph contains no cycles, the schedule can be safely serialized. If a cycle exists, the schedule is fundamentally non-serializable.

πŸ’» Bridging to Computer Science​

Checking whether an interleaved schedule is conflict serializable by manually testing all possible instruction swaps is tedious and error-prone. In database engineering, the problem is modeled as a directed graph problem:

Interleaved Schedule S  --->  Construct Precedence Graph G = (V, E)
Cycle Detected?
β”œβ”€β”€ YES ===> Schedule is NOT Conflict Serializable (Abort / Re-order)
└── NO ===> Schedule is Conflict Serializable (Topological Sort gives Serial Order)

Cycle detection runs in O(∣V∣+∣E∣)O(|V| + |E|) linear time using standard Depth-First Search (DFS), providing the database engine with an ultra-fast algorithm to guarantee transaction safety.



πŸ“š Core Deep-Dive & Concepts​

Formal Definition of Precedence Graph (Serialization Graph)​

Let SS be a schedule containing transactions {T1,T2,…,Tn}\{T_1, T_2, \dots, T_n\}.

The Precedence Graph (also called a Serialization Graph) is a directed graph G=(V,E)G = (V, E) constructed as follows:

  1. Vertices (VV): A set of nodes where each node represents an active transaction participating in schedule SS: V={T1,T2,…,Tn}V = \{ T_1, T_2, \dots, T_n \}
  2. Directed Edges (EE): A directed edge Tiβ†’TjT_i \to T_j (iβ‰ ji \neq j) is drawn from transaction TiT_i to transaction TjT_j if and only if there exists a pair of conflicting operations oi∈Tio_i \in T_i and oj∈Tjo_j \in T_j such that:
    • oio_i executes chronologically before ojo_j in schedule SS, AND
    • oio_i and ojo_j access the same data item QQ, with at least one operation being a write.

The Three Edge-Generating Conditions​

An edge Ti→TjT_i \to T_j is drawn whenever TiT_i precedes TjT_j in any of these three patterns:

  1. Writei(Q)\text{Write}_i(Q) precedes Readj(Q)\text{Read}_j(Q) (Read-after-Write flow)
  2. Readi(Q)\text{Read}_i(Q) precedes Writej(Q)\text{Write}_j(Q) (Write-after-Read overwrite)
  3. Writei(Q)\text{Write}_i(Q) precedes Writej(Q)\text{Write}_j(Q) (Write-after-Write overwrite)

Important Simplification: Multiple conflicting pairs between TiT_i and TjT_j in the same direction only produce a single directed edge Ti→TjT_i \to T_j.


The Conflict Serializability Theorem​

Foundational Theorem: A concurrent schedule SS is Conflict Serializable if and only if its precedence graph G=(V,E)G = (V, E) contains NO directed cycles (i.e. GG is a Directed Acyclic Graph - DAG).

Why Cycles Break Serializability​

If a cycle exists: T1β†’T2β†’T3β†’T1T_1 \to T_2 \to T_3 \to T_1

  • T1β†’T2T_1 \to T_2 requires that in any equivalent serial execution, T1T_1 must commit before T2T_2.
  • T2β†’T3T_2 \to T_3 requires that T2T_2 must commit before T3T_3.
  • T3β†’T1T_3 \to T_1 requires that T3T_3 must commit before T1T_1.

This forms a temporal contradiction: T1T_1 must execute before itself. No serial sequence can satisfy all three dependencies simultaneously.


Finding the Equivalent Serial Order: Topological Sort​

If the precedence graph GG is acyclic (contains no cycles), we can find one or more valid equivalent serial schedules by performing a Topological Sort on GG:

  1. Find a vertex TkT_k with an in-degree of 0 (no incoming edges).
  2. Append TkT_k to the serial schedule sequence.
  3. Remove TkT_k and all its outgoing edges from GG.
  4. Repeat steps 1–3 until all vertices are consumed.

If multiple nodes have in-degree 0 at any step, multiple valid equivalent serial schedules exist.


Step-by-Step Solved Problem 1: Acyclic Serializable Schedule​

Consider schedule S1S_1 across transactions T1,T2,T3T_1, T_2, T_3:

StepT1T_1T2T_2T3T_3
1Read(A)\text{Read}(A)
2Read(A)\text{Read}(A)
3Write(A)\text{Write}(A)
4Read(B)\text{Read}(B)
5Write(A)\text{Write}(A)
6Write(B)\text{Write}(B)
7Read(B)\text{Read}(B)
8Write(B)\text{Write}(B)

Step-by-Step Precedence Edge Extraction​

  1. Analyze Conflicts on Data Item AA:
    • Step 1: R1(A)R_1(A) is followed by Step 3: W2(A)W_2(A). β€…β€ŠβŸΉβ€…β€Š\implies Edge: T1β†’T2T_1 \to T_2
    • Step 2: R2(A)R_2(A) is followed by Step 5: W1(A)W_1(A). β€…β€ŠβŸΉβ€…β€Š\implies Edge: T2β†’T1T_2 \to T_1

Wait! Let us look at the edges on AA:

  • R1(A)R_1(A) precedes W2(A)β€…β€ŠβŸΉβ€…β€ŠT1β†’T2W_2(A) \implies T_1 \to T_2.
  • W2(A)W_2(A) precedes W1(A)β€…β€ŠβŸΉβ€…β€ŠT2β†’T1W_1(A) \implies T_2 \to T_1.

Directed edges formed: T1β†’T2andT2β†’T1T_1 \to T_2 \quad \text{and} \quad T_2 \to T_1 This forms an immediate cycle: T1⇄T2T_1 \rightleftarrows T_2. Conclusion for S1S_1: The precedence graph contains a cycle. Schedule S1S_1 is NOT Conflict Serializable!


Step-by-Step Solved Problem 2: Valid Acyclic Schedule​

Consider schedule S2S_2 across transactions T1,T2,T3T_1, T_2, T_3:

StepT1T_1T2T_2T3T_3
1Read(A)\text{Read}(A)
2Write(A)\text{Write}(A)
3Read(A)\text{Read}(A)
4Write(A)\text{Write}(A)
5Read(A)\text{Read}(A)
6Write(A)\text{Write}(A)
7Read(B)\text{Read}(B)
8Read(B)\text{Read}(B)
9Write(B)\text{Write}(B)

Step-by-Step Edge Extraction​

  1. Conflicts on Item AA:
    • W1(A)W_1(A) precedes R2(A)R_2(A) and W2(A)β€…β€ŠβŸΉβ€…β€ŠT1β†’T2W_2(A) \implies \mathbf{T_1 \to T_2}
    • W1(A)W_1(A) precedes R3(A)R_3(A) and W3(A)β€…β€ŠβŸΉβ€…β€ŠT1β†’T3W_3(A) \implies \mathbf{T_1 \to T_3}
    • W2(A)W_2(A) precedes R3(A)R_3(A) and W3(A)β€…β€ŠβŸΉβ€…β€ŠT2β†’T3W_3(A) \implies \mathbf{T_2 \to T_3}
  2. Conflicts on Item BB:
    • R2(B)R_2(B) precedes W1(B)β€…β€ŠβŸΉβ€…β€ŠT2β†’T1W_1(B) \implies \mathbf{T_2 \to T_1}? Wait! Let us check: Step 8 is R2(B)R_2(B), Step 9 is W1(B)W_1(B). R2(B)R_2(B) precedes W1(B)W_1(B)! This would create an edge T2β†’T1T_2 \to T_1, creating a cycle with T1β†’T2T_1 \to T_2.

Now consider reordering Step 9 before Step 8: If W1(B)W_1(B) executes at Step 7.5 (before R2(B)R_2(B)):

  • W1(B)W_1(B) precedes R2(B)β€…β€ŠβŸΉβ€…β€ŠT1β†’T2R_2(B) \implies \mathbf{T_1 \to T_2}.
  • All edges in graph: {T1β†’T2,T1β†’T3,T2β†’T3}\{ T_1 \to T_2, \quad T_1 \to T_3, \quad T_2 \to T_3 \}.

Precedence Graph: T1⟢T2⟢T3T_1 \longrightarrow T_2 \longrightarrow T_3

  • In-degree of T1T_1: 0
  • In-degree of T2T_2: 1 (from T1T_1)
  • In-degree of T3T_3: 2 (from T1,T2T_1, T_2)

There are zero cycles! Performing topological sort yields the unique equivalent serial schedule: T1⟢T2⟢T3T_1 \longrightarrow T_2 \longrightarrow T_3


πŸ“ Architecture / Visual Blueprint​

The following diagram illustrates how the transaction manager constructs the precedence graph and detects serializability cycles:


🏭 In The Real World: Production Case Study​

High-Throughput Inventory Ledger: CockroachDB & Google Spanner​

Modern distributed SQL databases like Google Cloud Spanner and CockroachDB provide SERIALIZABLE isolation across multi-region geographic clusters without global locks by evaluating transaction dependency graphs.

The Transaction Conflict Scenario​

Two microservices execute concurrent checkout operations across distributed database nodes:

  • Pod 1 (T1T_1): Deducts 11 unit of inventory from product SKU-A on the US-East region node, then queries the warehouse fulfillment queue WH-1 in Europe-West.
  • Pod 2 (T2T_2): Appends a shipment order to WH-1 in Europe-West, then reads SKU-A stock in US-East.

How Serialization Graphs Prevent Corruption​

  1. In the database serialization manager, the distributed conflict detector tracks data dependencies across nodes.
  2. The engine detects that T1T_1 modified SKU-A before T2T_2 read it (T1β†’T2T_1 \to T_2).
  3. Concurrently, T2T_2 wrote to WH-1 before T1T_1 queried it (T2β†’T1T_2 \to T_1).
  4. The distributed transaction coordinator identifies a directed cycle in the global precedence graph: T1⇄T2T_1 \rightleftarrows T_2
  5. Rather than committing corrupt ledger records, CockroachDB automatically picks the younger transaction (T2T_2), issues an internal RETRY_SERIALIZABLE abort, rolls back its intermediate state, and re-executes it cleanly behind T1T_1.

🎯 Exam & Interview Pitfall Check​

Core Conceptual Questions

Question 1: Schedule SS over transactions T1,T2,T3,T4T_1, T_2, T_3, T_4 contains the following operations: S:R1(A);R2(B);W1(A);R3(A);W2(B);W3(C);R4(C);W4(B);S: R_1(A); \quad R_2(B); \quad W_1(A); \quad R_3(A); \quad W_2(B); \quad W_3(C); \quad R_4(C); \quad W_4(B); Construct the precedence graph and determine whether SS is conflict serializable. If so, find all equivalent serial schedules.

Answer:

  1. Identify all conflicting pairs between distinct transactions:
    • On item AA:
      • R1(A)R_1(A) precedes nothing that writes AA except W1(A)W_1(A) (same transaction).
      • W1(A)W_1(A) precedes R3(A)R_3(A) β€…β€ŠβŸΉβ€…β€ŠT1β†’T3\implies \mathbf{T_1 \to T_3}
    • On item BB:
      • R2(B)R_2(B) precedes W4(B)W_4(B) β€…β€ŠβŸΉβ€…β€ŠT2β†’T4\implies \mathbf{T_2 \to T_4}
      • W2(B)W_2(B) precedes W4(B)W_4(B) β€…β€ŠβŸΉβ€…β€ŠT2β†’T4\implies T_2 \to T_4 (duplicate edge, keep one)
    • On item CC:
      • W3(C)W_3(C) precedes R4(C)R_4(C) β€…β€ŠβŸΉβ€…β€ŠT3β†’T4\implies \mathbf{T_3 \to T_4}
  2. The set of directed edges is: E={T1β†’T3,T2β†’T4,T3β†’T4}E = \{ T_1 \to T_3, \quad T_2 \to T_4, \quad T_3 \to T_4 \}
  3. Check for cycles:
    • Paths: T1β†’T3β†’T4T_1 \to T_3 \to T_4 and T2β†’T4T_2 \to T_4.
    • There are no cycles! Graph is an acyclic DAG.
    • Schedule SS is Conflict Serializable.
  4. Derive equivalent serial schedules using Topological Sort:
    • In-degree 0 nodes: T1T_1 and T2T_2.
    • Possibility 1: Execute T1T_1 first β†’\to remaining in-degree 0 is T2T_2 and T3T_3.
      • T1β†’T2β†’T3β†’T4T_1 \to T_2 \to T_3 \to T_4
      • T1β†’T3β†’T2β†’T4T_1 \to T_3 \to T_2 \to T_4
    • Possibility 2: Execute T2T_2 first β†’\to remaining in-degree 0 is T1T_1.
      • T2β†’T1β†’T3β†’T4T_2 \to T_1 \to T_3 \to T_4 All three serial orders are valid equivalent executions!

Question 2: If a schedule's precedence graph contains no cycles, is it guaranteed to be consistent?

Answer: Yes. If the precedence graph is acyclic, the schedule is conflict serializable, meaning it is conflict equivalent to at least one serial schedule. By definition, if each individual transaction maintains consistency in isolation, any serial schedule maintains overall database consistency. Because the concurrent schedule produces the exact same final state and intermediate read values as that serial schedule, it is guaranteed to leave the database in a consistent state.

Common Interview Traps

Trap 1: Drawing self-loops when operations of the same transaction conflict. If R1(A)R_1(A) precedes W1(A)W_1(A), candidates often mistakenly draw a self-loop T1→T1T_1 \to T_1. Precedence graphs strictly model dependencies between distinct transactions (Ti≠TjT_i \neq T_j). Self-loops never exist in standard precedence graphs.

Trap 2: Assuming that a non-conflict-serializable schedule is automatically inconsistent. Conflict serializability is a sufficient condition for serializability, but not a strictly necessary one. A schedule can fail conflict serializability (e.g., due to blind writes) and still be View Serializable, which also guarantees consistency!


πŸ’¬

Discussion & Doubts