skip to content

schedules and serializability

what correct means for interleaved transactions. conflicts, precedence graphs and cycles.

address
everything.locking.serializability
part
part 15, locks and concurrency control
chapter
chapter 1 of 10
reading time
7 min

a schedule is the order the operations of concurrent transactions actually ran in. it's serializable if it has the same effect as running the transactions one at a time, in some order. the test everyone uses: two operations conflict if they touch the same item from different transactions and one of them writes. draw an edge for every conflict, and the schedule is conflict serializable exactly when that precedence graph has no cycle.

the picture

you know how every isolation level is defined by "anomalies it prevents"? that's a list of symptoms. serializability is the definition of healthy: whatever happened, it must be explainable as some one-at-a-time order.

think of two cooks sharing one recipe card for a sauce. each step is "read the card" or "write a change on it". if one cook does all their steps and then the other, fine. if they interleave, the result is fine only if it matches one of those two orders. the precedence graph is the record of who had to go first, step by step.

here's the classic broken one, with values. A is alice's balance, 100. T1 adds 10, T2 adds 30:

T1
·
T2
·
1
R(A) → 100
the item
A
100

T1 reads A: 100.

step 1/5
drawn by hand · the textbook lost update schedule
a schedule in lanes, values on the side. both read A = 100 before either writes, so T2's write replaces T1's: 130 instead of 140. no one-at-a-time order gives 130. this is the textbook model, not a postgres run (postgres's version of it is in lost updates).

conflicts

only some pairs of operations care about their order. swap two reads of the same item and nobody can tell. swap a read and a write of the same item and the reader sees a different value. swap two writes and a different value survives.

then R(x)then W(x)
R(x)no conflictconflict (rw)
W(x)conflict (wr)conflict (ww)
drawn by hand · the textbook definition
when two operations from different transactions on the same item conflict. operations on different items never do.

two schedules are conflict equivalent if every conflicting pair is in the same order in both. you can turn one into the other by swapping adjacent non-conflicting operations, one swap at a time. a schedule is conflict serializable if it's conflict equivalent to some serial schedule.

the precedence graph

checking every serial order would mean n! of them. instead: one node per transaction, and an edge Ti → Tj whenever an operation of Ti conflicts with a later operation of Tj. Ti had to come first. a cycle says Ti must come before Tj and Tj before Ti, which no serial order can do. no cycle: sort the graph topologically, and that's your equivalent serial order.

play with it. pick a schedule and move operations up and down:

schedule =
  1. 1R1(A)
  2. 2R2(A)
  3. 3W1(A)
  4. 4W2(A)
  5. T1T2
R1(A) before W2(A); W1(A) before W2(A)R(A) → W(A)R2(A) before W1(A)R(A) → W(A)T1T2

not conflict serializable. the graph has a cycle, so no serial order puts every conflicting pair in the same order as this schedule.

move an operation with ↑ ↓. edges come from conflicting pairs: same item, different transactions, at least one write.

Ti → Tj: an op of Ti conflicts with a later op of Tjan edge on a cycle
drawn by hand · the textbook model of conflict serializability
reorder the operations. swapping two that don't conflict never changes the graph. swapping a conflicting pair flips its edge. the verdict below the graph is the cycle test: an edge on a cycle means not serializable, no cycle gives the serial order it's equivalent to.

try "lost update": move T2's read below T1's write and the edge T2 → T1 flips to T1 → T2, the cycle is gone, and the schedule is equivalent to T1 then T2. try "three": a cycle T1 → T2 → T3 → T1 of length three, which no pair of transactions shows on its own.

quick check
schedule: R1(A) W1(A) R2(A) R2(B) R1(B) W1(B). is it conflict serializable?

what databases actually do with this

nobody builds the precedence graph of a running system and checks it for cycles: the graph never stops growing, and by the time you find a cycle, transactions have committed. the graph is the yardstick. the algorithms are ways to guarantee no cycle can form, or to notice cheaply when one might:

  • two-phase locking makes conflicting operations wait, so edges can only point from earlier lock holders to later ones.
  • timestamp ordering fixes the serial order up front (by start time) and aborts any operation that would create an edge pointing backwards.
  • serializable snapshot isolation, postgres's SERIALIZABLE, tracks only the rw edges between overlapping transactions and aborts when two of them line up in a row, a pattern every cycle under snapshot isolation must contain.

postgres's REPEATABLE READ is snapshot isolation, which is not serializable: write skew is a cycle of two rw edges it lets through.

one level deeperconflict vs view serializability

conflict serializability is slightly stricter than the full definition. a schedule is view serializable if every read sees the same write as in some serial schedule and the final writes match. some view-serializable schedules aren't conflict serializable: they involve blind writes (a write with no read before it) that get overwritten anyway. testing view serializability is NP-complete, while the precedence graph test is linear in the number of conflicts, so practical systems aim for conflict serializability and give up the rest.

keylines

  • serializable: same effect as some one-at-a-time order of the same transactions.
  • conflict: same item, different transactions, at least one write.
  • precedence graph: Ti → Tj when Ti's op conflicts with a later op of Tj.
  • no cycle = conflict serializable, and the topological order is the serial order.
  • 2PL, timestamp ordering and SSI are ways to never build a cycle.