Sub topic 3
Serial and concurrent schedules
This chapter assumes you know what a transaction is and the four ACID properties from the earlier chapters. You should also be comfortable reading simple read and write operations on named data items such as A and B. The chapter defines a schedule, separates serial from concurrent schedules, and shows how fast the number of possible schedules grows. It ends with a small program that runs every interleaving and shows which ones give wrong results.
What a schedule is
A schedule is an ordered list of the operations of a set of transactions, produced by running them together. It has one rule: the operations of each transaction must appear in the same relative order as in that transaction.
The usual notation is:
R1(A)means transaction T1 reads item A.W2(B)means transaction T2 writes item B.C1means T1 commits, andA1means T1 aborts.
GATE questions usually leave out commits and aborts unless the topic is recoverability. This chapter does the same.
Suppose T1 is R1(A) W1(A) and T2 is R2(A) W2(A). The list R1(A) R2(A) W1(A) W2(A) is a valid schedule. The list W1(A) R1(A) R2(A) W2(A) is not, because it swaps two operations inside T1.
Serial schedules
A serial schedule runs the transactions one after another. Every operation of one transaction appears before any operation of the next. The order of transactions can be any permutation.
R1(A) W1(A) R2(A) W2(A)is serial, with order T1 then T2.R2(A) W2(A) R1(A) W1(A)is serial, with order T2 then T1.
Serial schedules have three properties.
- Consistency. If each transaction preserves the database constraints when run alone, any serial schedule does too. Each transaction starts from a consistent state and leaves one behind.
- No interference. Nothing overlaps, so no anomaly can occur.
- Count.
ntransactions have exactlyn!serial schedules.
Different serial schedules can give different final states, and all of them are correct. Let A start at 5, let T1 do A = A + 10, and let T2 do A = A * 2. T1 then T2 gives 30. T2 then T1 gives 20. A database promises that the result matches some serial order. It does not promise a particular one.
Why not just run serially
A serial schedule wastes the machine. A transaction often waits for disk reads, locks, or user input. During that wait the CPU sits idle if nothing else can run. Concurrent execution has two main benefits:
- Throughput. More transactions finish per second because the CPU and disks stay busy.
- Response time. A short transaction does not wait behind a long one.
Concurrent (non-serial) schedules
A concurrent schedule, also called a non-serial or interleaved schedule, mixes operations from different transactions. At least one transaction's operations are split by an operation of another.
The danger is that some interleavings give results that no serial order could produce. Take two debits on the same account. T1 subtracts 100 and T2 subtracts 50, and A starts at 1000.
Both transactions read 1000. T1 writes 900. T2 then writes 950 because it never saw T1's write. Either serial order gives 850. The next chapter names this problem a lost update.
How many schedules exist
Suppose transaction i has n_i operations. The number of interleavings that keep each transaction's internal order is the multinomial coefficient:
(n_1 + n_2 + ... + n_k)! / (n_1! * n_2! * ... * n_k!)Equivalently, choose which positions in the final list belong to T1, then which of the rest belong to T2, and so on. The table shows how quickly the count grows when every transaction has the same number of operations.
| Transactions | Operations each | All schedules | Serial schedules |
|---|---|---|---|
| 2 | 2 | 6 | 2 |
| 2 | 3 | 20 | 2 |
| 3 | 2 | 90 | 6 |
| 3 | 3 | 1680 | 6 |
The non-serial count is the total minus k!. Serial schedules are a tiny fraction of all schedules, which is why a database cannot just list the good ones. It needs a rule that recognizes safe interleavings, and the later chapters on serializability and locking supply it.
Running every interleaving
The program below generates all interleavings, tests whether each is serial, and runs it against a one-item database. Each transaction reads the item into a private variable and writes back that value plus its change.
def interleavings(txns):
names = sorted(txns)
pos = {n: 0 for n in names}
prefix = []
def rec():
finished = True
for n in names:
if pos[n] < len(txns[n]):
finished = False
op, item = txns[n][pos[n]]
pos[n] += 1
prefix.append((n, op, item))
yield from rec()
prefix.pop()
pos[n] -= 1
if finished:
yield list(prefix)
yield from rec()
def is_serial(schedule):
finished = set()
current = None
for t, _, _ in schedule:
if t != current:
if t in finished:
return False
if current is not None:
finished.add(current)
current = t
return True
def execute(schedule, db, change):
db = dict(db)
local = {}
for t, op, item in schedule:
if op == 'R':
local[t] = db[item]
else:
db[item] = local[t] + change[t]
return db
def label(s):
return ' '.join(f'{op}{t[1:]}({x})' for t, op, x in s)
change = {'T1': -100, 'T2': -50}
same = {'T1': [('R', 'A'), ('W', 'A')], 'T2': [('R', 'A'), ('W', 'A')]}
for s in interleavings(same):
final = execute(s, {'A': 1000}, change)
print(f'{label(s):<24} serial={is_serial(s)!s:<5} A={final["A"]}')
apart = {'T1': [('R', 'A'), ('W', 'A')], 'T2': [('R', 'B'), ('W', 'B')]}
all_apart = list(interleavings(apart))
outcomes = {tuple(sorted(execute(s, {'A': 1000, 'B': 1000}, change).items()))
for s in all_apart}
print(len(all_apart), outcomes)The output is:
R1(A) W1(A) R2(A) W2(A) serial=True A=850
R1(A) R2(A) W1(A) W2(A) serial=False A=950
R1(A) R2(A) W2(A) W1(A) serial=False A=900
R2(A) R1(A) W1(A) W2(A) serial=False A=950
R2(A) R1(A) W2(A) W1(A) serial=False A=900
R2(A) W2(A) R1(A) W1(A) serial=True A=850
6 {(('A', 900), ('B', 950))}On the same item, both serial schedules give 850 and all four interleaved schedules give a wrong answer. When the transactions touch different items, all six schedules give the same state. Interleaving is harmful only when operations interact. The next chapters make this precise as conflicting operations.
Serial versus serializable
A concurrent schedule is not automatically wrong. The correct requirement is that its effect equals the effect of some serial schedule.
- Serial describes the shape of a schedule: no interleaving.
- Serializable describes the effect of a schedule: it is equivalent to some serial schedule, even if it is interleaved.
Every serial schedule is serializable, but a serializable schedule need not be serial. In the program above, all six schedules on A and B are serializable. Only two are serial. There are several definitions of "equivalent", and they define the tests later in this tutorial.
Common errors
- Checking only the start. A schedule is serial only if every transaction's operations are contiguous.
R1(A) W1(A) R2(A) W2(A) R1(B) W1(B)starts like a serial schedule, but T1 resumes after T2 has run, so it is not serial. - Counting too many schedules. Do not use
(total ops)!. That formula counts orderings that break a transaction's internal order. Divide by the factorial of each transaction's length. - Assuming serial means a unique result. Different serial orders can give different states, and all are acceptable.
- Assuming non-serial means incorrect. The test is equivalence to a serial schedule, not the lack of interleaving.
Exercise
T1 has 3 operations, T2 has 2, and T3 has 2. How many schedules exist in total, how many are serial, and how many are non-serial?
Show answer
Total = 7! / (3! 2! 2!) = 5040 / 24 = 210. Serial schedules number 3! = 6. Non-serial schedules number 210 - 6 = 204.
Exercise
Is the schedule R1(A) W1(A) R2(A) W2(A) R1(B) W1(B) serial? Is it valid?
Show answer
It is valid, because each transaction's operations keep their own order. It is not serial, because T1's operations R1(B) and W1(B) appear after T2 has started, so T1 is split by T2.
Exercise
A starts at 5. T1 does A = A + 10 and T2 does A = A * 2. Give the final value of A for each serial order. Are both results correct?
Show answer
T1 then T2 gives (5 + 10) 2 = 30. T2 then T1 gives 5 2 + 10 = 20. Both are correct, since each is the result of a serial schedule. A correct database may produce either, but not any other value.