Correctness: Ensuring Safe Concurrent Execution
The Core Question π―
How do we know an interleaved schedule produces correct results?
Key Definitions
Foundation: Building Blocks for Correctness ποΈ
To understand how databases ensure correct concurrent execution, we need three foundational concepts:
β Serializable Schedule
An interleaved schedule that produces the same outcome as some serial schedule, ensuring consistency in the database.
Challenge: Harder to prove correctnessβmust prove same outcome for ALL possible database values
β οΈ Note: "Serializable" β "Serial"
β’ Serial: One transaction completes before next starts
β’ Serializable: Interleaved but equivalent to some serial order
Don't trip up on this distinction!
π Actions (Operations)
Actions in a transaction involve accessing and modifying data in a database through reads and writes.
Note: We only focus on R/W actions, not how data is modified (e.g., A-=1 or B*=10)
π Macro Schedules
These schedules contain only the ordering of actions. We use these for ensuring data consistency.
β’ Micro: Full timing, IO, locks (performance focus)
β’ Macro: Just R/W sequence (correctness focus)
Note: Both serial & interleaved have macro/micro versions. For simplicity, we'll call all these "schedules," and add descriptors only when needed for clarity.
Conflicts: The Heart of Concurrency Control βοΈ
Conflicts arise when transactions compete for the same data. Understanding conflicts is key to ensuring correctness:
βοΈ Conflicting Actions
Pairs of actions from different concurrent transactions where:
- Both actions involve the same data item
- At least one of the actions is a write operation
β’ T1.R(X), T2.W(X) are conflicting actions
β’ T1.W(X), T2.R(X) are conflicts
β’ T1.W(X), T2.W(X) conflict
Note: "conflicting actions," "conflicts," "conflict" all refer to the same thing
π Conflict Graph
A directed graph that represents the conflicts between different transactions in an interleaved schedule.
β’ Nodes represent individual transactions
β’ Directed edge from T1βT2 if T1's action conflicts with and precedes T2's action
Motivation: Simple data structure to help model and analyze conflicting actions
β Conflict-Serializable (CS) Schedules
Schedules where conflicting actions are ordered the same way as they would be in SOME serial execution (we don't care which one!).
Critical Point: Must be equivalent to at least one serial schedule - doesn't matter which serial order
Motivation: The database can choose ANY valid serial ordering that respects conflicts
Cycle Problem: If a cycle exists, there's no way to execute transactions serially without violating conflict order
π‘ How to think:
β’ What creates conflicts? Writes!
β’ Can we find ANY serial order that respects these conflicts?
Refresher on Graph Algorithms π
Understanding conflict graphs requires familiarity with basic graph concepts. Let's review the key algorithms used in conflict-serializability analysis.
π Cyclic vs Acyclic Graphs
A fundamental distinction for determining conflict-serializability:
Cyclic Graph: Contains at least one cycle - a path leads back to itself
CS Implication: Only acyclic conflict graphs represent conflict-serializable schedules
π Topological Sort
Algorithm to find a linear ordering of nodes in a directed acyclic graph:
Key Property: DAGs can have multiple valid topological orderings
CS Application: Each valid topological ordering represents a serial schedule that the conflict-serializable schedule is equivalent to
How to Test for Conflict-Serializability π
The key question: How do we determine if a schedule is conflict-serializable? The answer is elegant and algorithmic.
π The CS Algorithm: Build Graph β Check Cycles
Simple 3-step test: Turn any schedule into a conflict graph and check for cycles
Step 2: Add directed edges for each conflict (Ti conflicts with and precedes Tj)
Step 3: Check for cycles
β Acyclic graph: Schedule is CS (topological sort gives valid serial orders)
β Cycle exists: Schedule is NOT CS (impossible to serialize)
π Reality: What Databases Actually Do
Databases don't check CS after the fact. Instead, they prevent problematic schedules proactively:
β’ Use locking protocols (like 2PL) to control operation order
β’ Block transactions until it's safe to proceed
β’ Guarantee only CS schedules can occur
Example: If T1 holds a lock on A, T2 must wait to access A
β Prevents problematic interleavings before they happen
π The Beautiful Connection: Theory β Practice
Conflict graphs and locking protocols work together as a complete correctness system:
β’ Provides mathematical framework for reasoning about correctness
β’ Gives us algorithmic test: "Is this schedule safe?"
β’ Validates that our enforcement mechanisms work correctly
Locking Protocol Enforcement:
β’ Implements practical mechanism to ensure only safe schedules
β’ No need to check every scheduleβprotocol guarantees safety by design
1. Conflict graphs tell us what "correct" means
2. Algorithms like topological sort let us verify correctness
3. Protocols like 2PL ensure only correct schedules happen
4. Result: Mathematically proven concurrent execution! π―
π For more depth: Transaction Scheduling & Reordering Rules
Example 1: Concert Ticket Booking π«
Now let's apply these concepts to a real scenario with 3 transactions and 2 shared variables.
Setup: The Scenario
πͺ Scenario
Concert venue with 2 sections. Three customers simultaneously try to book tickets during high-demand sale.
β’ A: VIP section tickets (initially: 10)
β’ B: General section tickets (initially: 22)
π Transaction Actions
Each customer performs specific booking operations on the shared ticket counters.
π― Expected Results
After all transactions complete successfully, the final ticket counts should be consistent.
β’ A: 9 tickets
β’ B: 20 tickets
Serial Schedule S1: T1 β T2 β T3

π Execution Analysis
The serial schedule guarantees correctness by running transactions one at a time:
1. T1 completes: A=9, B=22
2. T2 completes: A=9, B=21
3. T3 completes: A=9, B=20
Properties: No conflicts β’ Predictable β’ ACID maintained
Conflict Identification
Conflicts happen when there are shared variables (even for Serial schedules)
βοΈ Conflict Analysis
Example Conflicting Action Pairs:
2. T2.W(B) β T3.W(B) - Write-Write conflict on B (blue)
3. 4 total conflicts to track in our analysis
Key Insight: Conflicts create a chain: T1 β T2 β T3
π Conflict Graph Construction
Step-by-Step Process:
2. Add Edges: For each conflict, draw edge from first β second
3. Check Cycles: If acyclic β Conflict-Serializable β
Result: Acyclic Graph β’ No cycles β’ CS Schedule β’ Equivalent to T1βT2βT3
Example 2: Interleaved Schedule Analysis π
Now let's explore what happens when we interleave transactions. We'll examine two different interleaved schedules to understand the difference between conflict-serializable and non-conflict-serializable schedules.
Schedule S1': Our First Interleaved Schedule
Consider S1', our 1st interleaved schedule. We can interleave the transactions while preserving correctness.


βοΈ Conflicts Identified
We compute the conflicts and build the Conflict Graph:
β’ Notable: B-(#4, #7) from T2 and T3
β’ 4 total conflicts across all transactions
Insight: The conflicts create dependencies between transactions that must be respected.
π Graph Analysis
We see edges for each conflict in the Conflict Graph:
β Conflict-Serializable: CS Schedule
β Multiple Valid Orders: T2βT1βT3 OR T2βT3βT1
Why Both Work: Both are valid topological orderings, meaning either serial execution produces equivalent results.
Schedule S2': Our Second Interleaved Schedule
Consider S2', an interleaved schedule. This demonstrates what happens when interleaving creates problematic conflicts.


βοΈ Conflicts Identified
We compute the conflicting actions and the Conflict Graph:
β’ Critical Issue: Conflicts create circular dependencies
β’ Cycle detected between T2 and T3
Warning: The conflict pattern creates dependencies that cannot be satisfied by any serial ordering.
π Graph Analysis
The Conflict Graph reveals the fundamental problem:
β NOT Conflict-Serializable: Not a CS Schedule
β No Valid Serial Order: Cannot find equivalent serial execution
The Problem: T2 must precede T3 AND T3 must precede T2 - impossible!
π― Important Distinction: CS vs Serializable
A non-CS schedule may still be a serializable schedule, but it's often hard to prove.
The Guarantee: However, a CS schedule will always be a serializable schedule.
Practical Implication: CS is a sufficient (but not necessary) condition for serializability.
Understanding Conflicts: The Venn Diagram
The conflict-venn diagram shows the different types of conflicts that can occur between transaction actions.