Sub topic 12
Timestamp ordering and the Thomas write rule
This chapter assumes you know what a conflict serializable schedule is and how a precedence graph works. It also assumes you know the read-write, write-read and write-write conflicts, and the basics of recoverability. Two-phase locking forces a serial order by making transactions wait. Timestamp ordering fixes the serial order in advance and aborts any transaction that breaks it.
The idea
Each transaction T gets a unique timestamp TS(T) when it starts, taken from a counter or the system clock. A smaller timestamp means an older transaction. The protocol guarantees that the schedule is equivalent to the serial schedule that runs transactions in increasing timestamp order. If an operation would contradict that order, the protocol rolls back the transaction that issued it. Nobody waits for a lock, so locks and lock-based deadlocks do not exist here.
Each data item Q carries two values:
R_TS(Q)is the largest timestamp of any transaction that has successfully readQ.W_TS(Q)is the largest timestamp of any transaction that has successfully writtenQ.
Both start at 0, and real timestamps start at 1.
The basic timestamp-ordering rules
Transaction T issues read(Q):
- If
TS(T) < W_TS(Q), thenTwould read a value that a younger transaction has already overwritten. Reject the read and roll backT. - Otherwise, execute the read and set
R_TS(Q) = max(R_TS(Q), TS(T)).
Transaction T issues write(Q):
- If
TS(T) < R_TS(Q), then a younger transaction has already read the old value thatTis about to replace. Reject the write and roll backT. - If
TS(T) < W_TS(Q), thenTwould write a value that a younger transaction has already overwritten. Basic timestamp ordering rejects the write and rolls backT. - Otherwise, execute the write and set
W_TS(Q) = TS(T).
A rolled-back transaction restarts with a new, larger timestamp. Equality never triggers a rejection. A transaction can read and then write the same item, because TS(T) is not less than its own R_TS.
Why the rules work
Every conflicting pair of operations (read-write, write-read, write-write) must occur in timestamp order. Rule 1 for reads blocks a younger write followed by an older read. Rule 1 for writes blocks a younger read followed by an older write. Rule 2 for writes blocks a younger write followed by an older write. Any schedule the protocol accepts therefore has all precedence-graph edges pointing from older to younger transactions. Such a graph cannot contain a cycle, so the schedule is conflict serializable, with the serial order equal to timestamp order.
Properties to remember for GATE
| Property | Basic timestamp ordering |
|---|---|
| Serializability | Conflict serializable, equivalent to timestamp order |
| Deadlock | Impossible, because transactions never wait |
| Starvation | Possible, because a transaction can be aborted repeatedly |
| Recoverability | Not guaranteed. A transaction may read an uncommitted write and then see the writer abort |
| Cascading rollback | Possible, for the same reason |
| Schedules allowed | Some schedules that 2PL rejects, and some that 2PL accepts are rejected here. The two protocols are incomparable |
Two-phase locking produces schedules that timestamp ordering can reject, and the reverse also holds. Neither protocol's set of schedules contains the other's.
A worked example
Let TS(T1) = 1, TS(T2) = 2, TS(T3) = 3. The schedule is:
r1(A), w2(A), w1(A), w3(A)
Initially R_TS(A) = 0 and W_TS(A) = 0.
| Step | Operation | Check | Result | R_TS(A), W_TS(A) |
|---|---|---|---|---|
| 1 | r1(A) | 1 < W_TS = 0? No | Execute | 1, 0 |
| 2 | w2(A) | 2 < R_TS = 1? No. 2 < W_TS = 0? No | Execute | 1, 2 |
| 3 | w1(A) | 1 < R_TS = 1? No. 1 < W_TS = 2? Yes | Basic: roll back T1. Thomas: ignore write | 1, 2 |
| 4 | w3(A) | 3 < 1? No. 3 < 2? No | Execute | 1, 3 |
Step 3 compares TS(T1) = 1 with R_TS(A) = 1. They are equal, so the test < fails and the first rule does not fire. Only the second rule rejects the write.
The Thomas write rule
Look at step 3 again. T1 wants to write A, but T2 (younger) has already written A, and T3 is about to overwrite it again. In the timestamp order T1, T2, T3, the value T1 writes would be overwritten by T2 immediately. Nobody read it, because no younger transaction read A between T1's position and T2's write. If one had, R_TS(A) would be larger than TS(T1) and rule 1 would already have fired. So the write is obsolete, and skipping it changes nothing visible.
The Thomas write rule replaces rule 2 for writes:
- If
TS(T) < R_TS(Q), roll backT. - If
TS(T) < W_TS(Q), ignore the write and letTcontinue. Do not updateW_TS(Q). - Otherwise, execute the write and set
W_TS(Q) = TS(T).
Reads keep their original rule.
The schedule above has the conflicts r1(A) → w2(A) and w2(A) → w1(A). The precedence graph has edges T1→T2 and T2→T1, a cycle, so the schedule is not conflict serializable. With the Thomas rule, w1(A) is dropped. The effective schedule is view equivalent to the serial schedule T1, T2, T3: T1 reads the initial value of A, and T3 makes the final write. So the Thomas write rule admits schedules that are view serializable but not conflict serializable. This is the standard GATE statement of the result.
The rule has a cost. Ignored writes mean T1 has done work that vanishes, which is acceptable only because no one could have observed it.
Simulating the protocol
This program applies both variants to a schedule. It uses 0 as the starting value for R_TS and W_TS, and skips the later operations of an aborted transaction. Run it as is.
def simulate(schedule, ts, thomas=False):
"""schedule: list of (txn, op, item), op is 'R' or 'W'.
ts: dict mapping txn -> timestamp (>= 1)."""
r_ts, w_ts = {}, {}
aborted = set()
log = []
for txn, op, item in schedule:
label = f"{op.lower()}{txn[1:]}({item})"
if txn in aborted:
log.append((label, "skipped, already aborted"))
continue
t = ts[txn]
r = r_ts.get(item, 0)
w = w_ts.get(item, 0)
if op == 'R':
if t < w:
aborted.add(txn)
log.append((label, "abort"))
else:
r_ts[item] = max(r, t)
log.append((label, "ok"))
else:
if t < r:
aborted.add(txn)
log.append((label, "abort"))
elif t < w:
if thomas:
log.append((label, "ignored"))
else:
aborted.add(txn)
log.append((label, "abort"))
else:
w_ts[item] = t
log.append((label, "ok"))
return log
sched = [('T1', 'R', 'A'), ('T2', 'W', 'A'),
('T1', 'W', 'A'), ('T3', 'W', 'A')]
stamps = {'T1': 1, 'T2': 2, 'T3': 3}
print("basic ", simulate(sched, stamps))
print("thomas", simulate(sched, stamps, thomas=True))The basic run aborts w1(A). The Thomas run reports ignored for it, and every other operation is ok.
Common errors
- Using
<=instead of<. A transaction does not conflict with itself. Equal timestamps must pass. - Updating
W_TSon an ignored write. Under the Thomas rule an ignored write leavesW_TS(Q)unchanged. Setting it to the smaller value would lose information. - Applying the Thomas rule to the read-write check. If
TS(T) < R_TS(Q), the write is not obsolete, because a younger transaction read the old value. The rule still rollsTback. - Updating
R_TSwith the latest reader instead of the maximum. An older transaction reading after a younger one must not lowerR_TS. - Assuming timestamp ordering is recoverable. Consider
w1(A), r2(A), commit2, abort1withTS(T1) < TS(T2). The protocol accepts it, but it is not recoverable. Fixes include strict timestamp ordering, where a read or write on an item waits until the transaction that last wrote it commits, or commit dependencies. Strict timestamp ordering reintroduces waiting, but not deadlock, since waits always go from younger to older. - Believing aborts mean failure. An abort under timestamp ordering means the transaction arrived "too late". It restarts with a new timestamp and normally succeeds.
Comparison with locking
Timestamp ordering suits workloads with few conflicts, because it has no lock overhead and no waiting. Under heavy contention, the aborted work is wasted, and a long transaction can starve. Two-phase locking suits heavier contention but needs deadlock handling. The multiversion and optimistic schemes covered later in this tutorial build on timestamps to reduce the number of aborts.
Exercise
Items A and B start with RTS = WTS = 0. TS(T1) = 5 and TS(T2) = 8. The schedule is r2(A), w1(A), w2(B), r1(B). Apply basic timestamp ordering. Which operations fail, and does the Thomas write rule change the outcome?
Show answer
r2(A) executes and sets RTS(A) = 8. w1(A): TS(T1) = 5 < RTS(A) = 8, so T1 is rolled back. The Thomas rule does not help, because the failed check is the read-timestamp check, not the write-timestamp check. T1's later operation r1(B) is not executed, because T1 is already aborted. w2(B) executes and sets W_TS(B) = 8. In both variants the outcome is identical.
Exercise
TS(T1) = 1, TS(T2) = 2. The schedule is w2(A), w1(A), r1(A). Initial RTS(A) = WTS(A) = 0. What happens under basic timestamp ordering and under the Thomas write rule?
Show answer
w2(A) executes and sets WTS(A) = 2. w1(A): TS(T1) = 1 is not less than RTS(A) = 0, but 1 < WTS(A) = 2. Basic ordering rolls back T1, so r1(A) is skipped. Under the Thomas rule, w1(A) is ignored and T1 continues. Then r1(A): TS(T1) = 1 < WTS(A) = 2, so the read is rejected and T1 is rolled back anyway. The Thomas rule saves the write but cannot save T1, because T1's later read would see a value from the future.
Exercise
Why can basic timestamp ordering never produce a deadlock, and what problem replaces it?
Show answer
Transactions never wait for each other. A conflicting operation either executes at once or its transaction is rolled back. A wait-for cycle therefore cannot form. The replacement problem is starvation: a transaction can be rolled back and restarted repeatedly if conflicts with younger transactions keep occurring.