Serializable Schedule

Definitions

An action is

  • r(X) - read database element X or
  • w(X) - write database element X
  • we abstract away from the actual values that are read/written, we are not interested in them

A transaction T is a sequence of action

  • r(A),r(B),w(A),w(B)

A schedule is a sequence of actions that belong to different transactions

  • r1(A),w1(A),r2(A),w2(A),r1(B),w1(B),r2(B),w2(B)
  • ri(X) denotes that this action belongs to transaction Ti


Serializability

a serial schedule is a schedule in which transactions are executed consequently, not concurrently

  • e.g. first all actions of T1 and then all actions of T2
  • r1(A),w1(A),r1(B),w1(B)all actions of T1,r2(A),w2(A),r2(B),w2(B)all actions of T2

Serializable Schedule

  • a schedule is called serializable is there exists an equivalent serial schedule
  • even though we may execute actions concurrently, the effect is guaranteed to be the same as if it was run in isolation
  • S1=r1(A),w1(A),r2(A),w2(A),r1(B),w1(B),r2(B),w2(B)
  • S2=r1(A),w1(A),r1(B),w1(B),r2(A),w2(A),r2(B),w2(B)
  • S1S2, S2 is serial, so S1 is serializable

Not serializable:

  • Write-Read conflict:
    • r1(A),w1(A),r2(A),w2(A),r2(B),w2(B),r1(B),w1(B)
    • write by T2 happens before read by T1, but T1 started earlier
    • it's not equivalent to any serial schedule

We want to schedule our actions in such a way that the result is serializable

Conflict-Serializability

Serializability is very hard to achieve

two actions are in conflict if

  1. they belong to the same transaction
  2. both deal with the same element and one of the actions is a write

A schedule is conflict-serializable if

  • we can obtain a serial schedule by swapping actions that are not in conflict

Example:

  • S1=r1(A),w1(A),r2(A)(1),w2(A)(2),r1(B)(3),w1(B)(4),r2(B),w2(B)
    • (1)+(2) are not in conflict with (3)+(4): they use different DB items
    • can swap them and get a serial schedule
  • S2=r1(A),w1(A),r1(B),w1(B),r2(A),w2(A),r2(B),w2(B)
  • i.e. S1 is conflict-serializable

NB:

  • you never can reorder actions of the same transaction
  • otherwise you may change the behavior of this transaction
  • what is more, by reordering actions within same transactions it's not possible to get something serial from not serializable schedule


Conflict-Serializability Serializability

but converse is not true

  • S1=w1(Y),w2(Y),w1(X),w3(X)
  • S2=w1(Y),w1(X),w2(X),w3(X)
  • S1S2: in Y there's a value written by T2, and in X the value written by T3
  • but S_1 is not Conflict-Serializable, but Serializable: we cannot swap w2(X) and w1(X)

Precedence Graph

There is an algorithm that checks if a schedule is conflict-serializable

construct a precedence graph:

  • suppose you have a schedule S with several transactions
  • create a node for each transaction
  • connect Ti with Tj if
    • there actions aiTi,ajTj s.t.
    • ai precedes aj and
    • ai are in conflict aj and

Example 1:

  • S1=r2(A),r1(B),w2(A),r3(A),w1(B),w3(A),r2(B),w2(B)
  • pred-graph-1.png

Example 2:

  • S2=w1(Y),w2(y),w2(X),w1(X),w3(X)
  • pred-graph-2.png


Thm If the precedence graph G of a schedule S is a DAG then

  • S is conflict-serializable
  • otherwise it's not

Suppose we have a cycle T1...TnT1 in G

  • it means there action a1T1 on some database item X that follows anTn on the same X
    • we cannot move a1 by conflict-free swapping to the front
    • i.e. cannot put a1 before all actions of Tn
  • there also an that follows some action a1T1
    • but we cannot move an either because of some action an1Tn1
  • i.e. we cannot have a conflict-serializable schedule


But if G is a DAG, we can obtain an equivalent conflict-serializable schedule by Topological Ordering

  • if there are no cycles in G then
    • there a transaction TA that has no incoming edges in G
    • otherwise there would be a cycle
  • let a1 be the first action of TA
  • can move a1 by conflict-free swapping to the front
    • since TA has no incoming edges, there are no actions that conflict with a2
  • let a2 be the second action of TA
    • also can move it to the front
  • repeat for all actions aiTA
  • we end up with the following schedule:
    • S=a1,a2,...,akall actions TA,b1,c1,...,zpall actions TA
  • now remove all actions of TA, let G be the precedence graph of this schedule and repeat

If there are 2 or more nodes with no incoming edges, just pick one of them

  • the result in any case will be serial
  • only the order may be different

Schedulers

A Scheduler is a component of Transaction Manager that schedules read/write requests.

The following schedulers produce Conflict-Serializable Schedules:

Sources