Lock-based and timestamp-based concurrency control
Clear, exam-focused notes comparing lock-based (2PL, strict 2PL) and timestamp-based (basic timestamp ordering, Thomas write rule, MVTO) concurrency control, with proofs, algorithms, worked traces and common exam pitfalls.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Databases must allow many users to run transactions at the same time without corrupting data. Concurrency control protocols (locks and timestamps) are used by database engines and distributed systems to ensure correctness (serialisability), performance, and recoverability while maximising parallelism.
Key ideas
Serial schedule: a schedule that runs transactions one after another with no interleaving.
Serialisability: a schedule is serialisable if it has the same effect (on all database states) as some serial schedule. Serialisability is the correctness criterion most DBMS use for concurrency control.
Conflict: two operations conflict if they are by different transactions, act on the same data item, and at least one is a write. (Read–Read do not conflict.)
Conflict-serialisable: a schedule is conflict-serialisable if it can be transformed into a serial schedule by swapping non-conflicting adjacent operations. Equivalent condition (below) is used for proofs.
Precedence graph (serialisability graph): a directed graph whose nodes are transactions; add an edge Ti -> Tj if an operation of Ti precedes and conflicts with an operation of Tj on the same data item in the schedule. Exact theorem:
- Theorem: A schedule is conflict-serialisable if and only if its precedence graph is acyclic. (Assume all conflicts are observable; operations are instantaneous.)
- How to draw: draw one node per transaction Ti. For each pair of conflicting operations op_i then op_j on the same data item, draw directed edge Ti -> Tj. If graph has a cycle, schedule is not conflict-serialisable.
Two-Phase Locking (2PL): locking protocol where each transaction has two phases: a growing phase (can acquire locks, not release any) and a shrinking phase (can release locks, not acquire any). Lock types: shared (S) lock for read, exclusive (X) lock for write.
- Exact statement (theorem): Any schedule produced under 2PL is conflict-serialisable.
- Assumptions: locks are acquired before first use of item; locking requests block or cause abort according to chosen policy; locks are respected system-wide.
- Proof sketch (standard exam proof): assume a cycle in precedence graph; follow conflicting operation order to get contradiction with growing/shrinking phases because a later edge implies lock acquisition after release; therefore no cycle; so graph acyclic.
- Variants:
- Strict 2PL: a transaction holds all exclusive locks until it commits or aborts (i.e., release X locks only at commit/abort). Strict 2PL guarantees both conflict-serialisability and recoverability (no cascading aborts). Definitions: A schedule is recoverable if each transaction commits only after all transactions from which it has read have committed; cascadeless if transactions read only committed data (so no cascading aborts); strict if transactions neither read nor write items written by uncommitted transactions.
- Rigorous 2PL: hold both S and X locks until commit; stronger than strict 2PL.
Deadlock: cycle of waiting among transactions for locks. Lock-based systems must detect and resolve deadlocks (deadlock detection: cycle in wait-for graph; resolution: abort one transaction) or prevent them (timeouts, ordering, wait-die/wound-wait schemes).
Timestamp-based concurrency control: each transaction T gets a unique timestamp TS(T) (logical increasing number; lower means older). For each data item X maintain:
- R-timestamp(X) = largest TS of any transaction that successfully read X so far.
- W-timestamp(X) = largest TS of any transaction that successfully wrote X so far.
Basic Timestamp Ordering (TO) rules:
Read rule: When T wants to read X: if TS(T) < W-timestamp(X) then T must abort (it is too late to read a newer write); otherwise allow the read and set R-timestamp(X) = max(R-timestamp(X), TS(T)).
Write rule: When T wants tou write X: if TS(T) < R-timestamp(X) then T must abort (a younger transaction has already read X, so T's write would violate timestamp order); else if TS(T) < W-timestamp(X) then T must abort (a later write already happened); otherwise allow the write and set W-timestamp(X) = TS(T).
Thomas' write rule (optimization): If a write by T has TS(T) < W-timestamp(X) then the write is obsolete and can be ignored (not cause abort), because a later write already supersedes it. This relaxes the strict write rule in some cases.
Multiversion Timestamp Ordering (MVTO): Keep multiple versions of each item, each version tagged with write timestamp. Reads can pick the latest version with timestamp <= TS(T). This increases concurrency (older transactions can read older versions) and avoids some aborts.
Comparison of properties to check (you will be asked in exams):
- Does the protocol guarantee conflict-serialisability? (2PL: yes; TO: yes—equivalent to a serial order by timestamps; MVTO: not necessarily in single-version conflict terms; it guarantees multiversion serializability ordered by timestamps.)
- Is the schedule recoverable? (Strict 2PL: yes; 2PL not necessarily; basic TO: not necessarily—reads can see uncommitted writes, so cascading aborts and non-recoverable schedules are possible unless augmented with commit-ordering or delayed-write/commit checks.)
- Does protocol allow cascading aborts? (Strict 2PL avoids; plain 2PL may allow; basic TO may allow—reads are not delayed.)
- Deadlocks? (Locking: possible; timestamp: deadlock-free but can cause starvation and many aborts.)
Formulas
(Syntax and rules to remember)
TS(T) — timestamp of transaction T (logical integer). Unitless; order-only.
R-timestamp(X) — largest timestamp of a transaction that has successfully read X; unitless.
W-timestamp(X) — largest timestamp of a transaction that has successfully written X; unitless.
Read rule (Timestamp Ordering):
if TS(T) < W-timestamp(X) then abort(T) else perform read; R-timestamp(X) = max(R-timestamp(X), TS(T)).
Write rule (Timestamp Ordering):
if TS(T) < R-timestamp(X) then abort(T) else if TS(T) < W-timestamp(X) then abort(T) else perform write; W-timestamp(X) = TS(T).
Thomas' write rule (optimization):
if TS(T) < W-timestamp(X) then ignore write (no abort) else apply write.
2PL constraint (informal syntax to remember):
Each transaction: acquire locks (S or X) — growing phase; release locks — shrinking phase; cannot acquire after first release.
Wait-die (non-preemptive timestamp deadlock prevention):
if TS(Ti) < TS(Tj) (Ti older) then Ti waits for lock held by Tj else Ti aborts and restarts.
Wound-wait (preemptive):
if TS(Ti) < TS(Tj) (Ti older) then Ti preempts (wounds/aborts) Tj else Ti waits.
Worked examples
Example 1 — Precedence graph and conflict-serialisability (step-by-step):
Given schedule S:
- T1: R(A)
- T2: R(A)
- T1: W(A)
- T2: W(A)
- T1: Commit
- T2: Commit
Step 1: List conflicting pairs (different transactions, same item, at least one write):
- T1 W(A) at step 3 conflicts with T2 R(A) at step 2? Yes. R(A) at step 2 comes before W(A) at step 3 and they are R vs W by different transactions, so they conflict — edge is T2 -> T1 because T2's read is before T1's write.
- T1 W(A) at step 3 conflicts with T2 W(A) at step 4: step3 before step4, so edge T1 -> T2.
Step 2: Build precedence graph:
- Nodes: T1, T2.
- Edges: from step 2 vs 3 we get T2 -> T1. From step 3 vs 4 we get T1 -> T2.
Graph has cycle T1 -> T2 -> T1 so schedule is not conflict-serialisable. Final answer: not conflict-serialisable.
Example 2 — Basic Timestamp Ordering trace (numerical timestamps):
Assume TS(T1)=1, TS(T2)=2. Initially for item X: R-ts(X)=0, W-ts(X)=0.
Schedule:
- T2: R(X)
- T1: W(X)
- T2: W(X)
Step trace:
- T2 reads X: check TS(T2)=2 vs W-ts(X)=0 => 2 < 0? No. Allow read. Set R-ts(X)=max(0,2)=2.
- T1 writes X: TS(T1)=1. Check TS(T1) < R-ts(X)? 1 < 2 -> yes => abort T1 (because an older transaction tries to write after a newer transaction has read). So T1 aborts and will restart with a new timestamp in most systems.
- T2 writes X: after T1 aborted, T2 can write. Check TS(T2)=2 < R-ts(X)=2? No. TS(T2)=2 < W-ts(X)=0? No. Allow write and set W-ts(X)=2.
Result: T1 aborted, T2 succeeded. Explanation: because T1 older tried to write after T2 had already read, TO aborts the older writer to prevent inconsistency.
Example 3 — 2PL locking and deadlock (trace):
Transactions T1 and T2 operating on A and B. Sequence (locks shown):
- T1: lock-S(A)
- T2: lock-S(B)
- T1: upgrade to lock-X(A) (succeeds)
- T2: request lock-X(A) -> blocked (waiting for T1 to release)
- T1: request lock-X(B) -> blocked (waiting for T2)
We have wait-for graph T1 -> T2 (T1 waiting for T2) and T2 -> T1, so deadlock. Resolution options: detect cycle and abort a transaction (say abort T2), or use preemptive policy (wound-wait) to avoid deadlock.
If using wait-die (timestamps TS(T1)=1 older, TS(T2)=2 younger): when T2 requests X(A) held by T1, TS(T2)>TS(T1) so T2 aborts (wait-die forces younger to die); deadlock avoided by aborting T2.
Common mistakes
Applying Thomas' write rule without stating the condition (it only ignores obsolete writes when timestamp < W-timestamp(X)).
Confusing serialisability (equivalence to some serial schedule by effect) with conflict-serialisability (equivalence by swapping non-conflicting operations); many exam questions expect conflict-serialisability tests using precedence graphs.
Not distinguishing abort vs ignore in timestamp rules: some cases cause abort, some can be ignored (Thomas' rule).
Incorrectly drawing precedence graph edges: edge direction is from transaction doing the earlier conflicting operation to the one doing the later conflicting operation.
For your exam
Question types you'll see on this topic:
- Definitions and short notes (define 2PL, strict 2PL, timestamp ordering, Thomas' rule, MVTO).
- Derivations/proofs (prove 2PL => conflict-serialisable; show why strict 2PL prevents cascading aborts).
- Traces and algorithms (simulate a given schedule under 2PL or timestamp ordering; show which transactions abort or commit).
- Comparisons and essays (advantages/disadvantages of lock-based vs timestamp-based protocols; how deadlocks are handled).
- Diagrams (draw precedence graph; draw wait-for graph to show deadlock).
What to include for full marks:
- Precise definitions (bold or underlined in exam notes helps), state assumptions.
- For proofs: clear assumption, step-by-step logic, conclude link to serialisability (show acyclicity) or recoverability.
- For traces: list initial values (timestamps, R/W-ts), show each operation, the rule applied, updates to timestamps or locks, and final status (commit/abort).
- For diagrams: clearly label nodes (transactions), edges, and indicate why a cycle exists or not. Describe what the cycle implies.
Quick check
- State the two phases of 2PL.
- Under basic timestamp ordering, what happens when TS(T) < W-timestamp(X) on a read of X by T?
- What graph do you build to test conflict-serialisability and what makes the schedule non-conflict-serialisable?
Answers: 1) Growing phase (acquire locks only) and shrinking phase (release locks only). 2) The read is disallowed and T must abort (it is too old to read a newer write). 3) Build the precedence (serialisability) graph; if the graph has a directed cycle the schedule is not conflict-serialisable.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?