Conflict Serializability & Precedence Graphs
π‘ Core Intuitionβ
π³ The Everyday Analogy: The Shared Kitchen Chore Boardβ
Imagine two roommates, Alex () and Blake (), sharing an apartment kitchen:
- In the morning, Alex washes the shared chef's pan (). Immediately afterward, Blake cooks eggs in it (). For this to make sense, Alex must execute before Blake: .
- But later that afternoon, Blake stocks the shared fridge with milk (), and Alex drinks from it (). Here, Blake must execute before Alex: .
Notice the logical deadlock: for the pan chore, Alex must go before Blake (), but for the milk chore, Blake must go before Alex (). 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 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 be a schedule containing transactions .
The Precedence Graph (also called a Serialization Graph) is a directed graph constructed as follows:
- Vertices (): A set of nodes where each node represents an active transaction participating in schedule :
- Directed Edges (): A directed edge () is drawn from transaction to transaction if and only if there exists a pair of conflicting operations and such that:
- executes chronologically before in schedule , AND
- and access the same data item , with at least one operation being a write.
The Three Edge-Generating Conditionsβ
An edge is drawn whenever precedes in any of these three patterns:
- precedes (Read-after-Write flow)
- precedes (Write-after-Read overwrite)
- precedes (Write-after-Write overwrite)
Important Simplification: Multiple conflicting pairs between and in the same direction only produce a single directed edge .
The Conflict Serializability Theoremβ
Foundational Theorem: A concurrent schedule is Conflict Serializable if and only if its precedence graph contains NO directed cycles (i.e. is a Directed Acyclic Graph - DAG).
Why Cycles Break Serializabilityβ
If a cycle exists:
- requires that in any equivalent serial execution, must commit before .
- requires that must commit before .
- requires that must commit before .
This forms a temporal contradiction: must execute before itself. No serial sequence can satisfy all three dependencies simultaneously.
Finding the Equivalent Serial Order: Topological Sortβ
If the precedence graph is acyclic (contains no cycles), we can find one or more valid equivalent serial schedules by performing a Topological Sort on :
- Find a vertex with an in-degree of 0 (no incoming edges).
- Append to the serial schedule sequence.
- Remove and all its outgoing edges from .
- 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 across transactions :
| Step | |||
|---|---|---|---|
| 1 | |||
| 2 | |||
| 3 | |||
| 4 | |||
| 5 | |||
| 6 | |||
| 7 | |||
| 8 |
Step-by-Step Precedence Edge Extractionβ
- Analyze Conflicts on Data Item :
- Step 1: is followed by Step 3: . Edge:
- Step 2: is followed by Step 5: . Edge:
Wait! Let us look at the edges on :
- precedes .
- precedes .
Directed edges formed: This forms an immediate cycle: . Conclusion for : The precedence graph contains a cycle. Schedule is NOT Conflict Serializable!
Step-by-Step Solved Problem 2: Valid Acyclic Scheduleβ
Consider schedule across transactions :
| Step | |||
|---|---|---|---|
| 1 | |||
| 2 | |||
| 3 | |||
| 4 | |||
| 5 | |||
| 6 | |||
| 7 | |||
| 8 | |||
| 9 |
Step-by-Step Edge Extractionβ
- Conflicts on Item :
- precedes and
- precedes and
- precedes and
- Conflicts on Item :
- precedes ? Wait! Let us check: Step 8 is , Step 9 is . precedes ! This would create an edge , creating a cycle with .
Now consider reordering Step 9 before Step 8: If executes at Step 7.5 (before ):
- precedes .
- All edges in graph: .
Precedence Graph:
- In-degree of : 0
- In-degree of : 1 (from )
- In-degree of : 2 (from )
There are zero cycles! Performing topological sort yields the unique equivalent serial schedule:
π 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 (): Deducts unit of inventory from product
SKU-Aon the US-East region node, then queries the warehouse fulfillment queueWH-1in Europe-West. - Pod 2 (): Appends a shipment order to
WH-1in Europe-West, then readsSKU-Astock in US-East.
How Serialization Graphs Prevent Corruptionβ
- In the database serialization manager, the distributed conflict detector tracks data dependencies across nodes.
- The engine detects that modified
SKU-Abefore read it (). - Concurrently, wrote to
WH-1before queried it (). - The distributed transaction coordinator identifies a directed cycle in the global precedence graph:
- Rather than committing corrupt ledger records, CockroachDB automatically picks the younger transaction (), issues an internal
RETRY_SERIALIZABLEabort, rolls back its intermediate state, and re-executes it cleanly behind .
π― Exam & Interview Pitfall Checkβ
Question 1: Schedule over transactions contains the following operations: Construct the precedence graph and determine whether is conflict serializable. If so, find all equivalent serial schedules.
Answer:
- Identify all conflicting pairs between distinct transactions:
- On item :
- precedes nothing that writes except (same transaction).
- precedes
- On item :
- precedes
- precedes (duplicate edge, keep one)
- On item :
- precedes
- On item :
- The set of directed edges is:
- Check for cycles:
- Paths: and .
- There are no cycles! Graph is an acyclic DAG.
- Schedule is Conflict Serializable.
- Derive equivalent serial schedules using Topological Sort:
- In-degree 0 nodes: and .
- Possibility 1: Execute first remaining in-degree 0 is and .
- Possibility 2: Execute first remaining in-degree 0 is .
- 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.
Trap 1: Drawing self-loops when operations of the same transaction conflict. If precedes , candidates often mistakenly draw a self-loop . Precedence graphs strictly model dependencies between distinct transactions (). 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!