Optimistic Concurrency Control
COMP9315 21T1 ♢ Optimistic CC ♢ [0/8]
❖ Optimistic Concurrency Control | |
Locking is a pessimistic approach to concurrency control:
- limit concurrency to ensure that conflicts don't occur
Costs: lock management, deadlock handling, contention.
In scenarios where there are far more reads than writes ...
- don't lock (allow arbitrary interleaving of operations)
- check just before commit that no conflicts occurred
- if problems, roll back conflicting transactions
Optimistic concurrency control (OCC) is a strategy to realise this.
COMP9315 21T1 ♢ Optimistic CC ♢ [1/8]
❖ Optimistic Concurrency Control (cont) | |
Under OCC, transactions have three distinct phases:
- Reading: read from database, modify local copies of data
- Validation: check for conflicts in updates
- Writing: commit local copies of data to database
Timestamps are recorded at points
S, V, F :
COMP9315 21T1 ♢ Optimistic CC ♢ [2/8]
Data structures needed for validation:
- S ... set of txs that are reading data and computing results
- V ... set of txs that have reached validation (not yet committed)
- F ... set of txs that have finished (committed data to storage)
- for each Ti, timestamps for when it reached S, V, F
- RS(Ti) set of all data items read by Ti
- WS(Ti) set of all data items to be written by Ti
Use the
V timestamps as ordering for transactions
- assume serial tx order based on ordering of V(Ti)'s
COMP9315 21T1 ♢ Optimistic CC ♢ [3/8]
Two-transaction example:
- allow transactions T1 and T2 to run without any locking
- check that objects used by T2 are not being changed by T1
- if they are, we need to roll back T2 and retry
Case 0: serial execution ... no problem
COMP9315 21T1 ♢ Optimistic CC ♢ [4/8]
Case 1: reading overlaps validation/writing
- T2 starts while T1 is validating/writing
- if some X being read by T2 is in WS(T1)
- then T2 may not have read the updated version of X
- so, T2 must start again
COMP9315 21T1 ♢ Optimistic CC ♢ [5/8]
Case 2: reading/validation overlaps validation/writing
- T2 starts validating while T1 is validating/writing
- if some X being written by T2 is in WS(T1)
- then T2 may end up overwriting T1's update
- so, T2 must start again
COMP9315 21T1 ♢ Optimistic CC ♢ [6/8]
Validation check for transaction T
- for all transactions Ti ≠ T
- if T∈S & Ti∈F, then ok
- if T∉V & V(Ti) < S(T) < F(Ti),
then check WS(Ti) ∩ RS(T) is empty
- if T∈V & V(Ti) < V(T) < F(Ti),
then check WS(Ti) ∩ WS(T) is empty
If this check fails for any
Ti, then
T is rolled back.
COMP9315 21T1 ♢ Optimistic CC ♢ [7/8]
❖ Validation Check (cont) | |
OCC prevents:
T reading dirty data, T overwriting Ti's changes
Problems with OCC:
- increased roll backs**
- tendency to roll back "complete" tx's
- cost to maintain S,V,F sets
** "Roll back" is relatively cheap
- changes to data are purely local before Writing phase
- no requirement for logging info or undo/redo (see later)
COMP9315 21T1 ♢ Optimistic CC ♢ [8/8]
Produced: 15 Apr 2021