see also concurrency
A sequence of read/write
concurrency
inter-leaved processing: concurrent exec is interleaved within a single CPU.
parallel processing: process are concurrently executed on multiple CPUs.
ACID
- atomic: either performed in its entirety (DBMS’s responsibility)
- consistency: must take database from consistent state to
- isolation: appear as if they are being executed in isolation
- durability: changes applied must persist, even in the events of failure
Schedule
definition
a schedule of transaction is an ordering of operations of the transactions subject to the constrain that
For all transaction that participates in , the operations of in must appear in the same order in which they occur in
For example:
- serial schedule: does not interleave the actions of different transactions
- equivalent schedule: effect of executing any schedule are the same
serialisable schedule
a schedule that is equvalent to some serial execution of the set of committed transactions
serial
serialisable schedule
conflict
operations in schedule
said to be in conflict if they satisfy all of the following:
- belong to different operations
- access the same item
- at least one of the operations is a
write(A)
Concurrency Issue | Description | Annotation |
---|---|---|
Dirty Read | Reading uncommitted data | WR |
Unrepeatable Read | T2 changes item that was previously read by T1, while T1 is still in progress | RW |
Lost Update | T2 overwrites item while T1 is still in progress, causing T1’s changes to be lost | WW |
conflict serialisable schedules
Two schedules are conflict equivalent if:
- involves the same actions of the same transaction
- every pair of conflicting actions is ordered the same way
Schedule is conflict serialisable if is conflict equivalent to some serial schedule
If two schedule and are conflict equivalent then they have the same effect by swapping non-conflicting ops
Every conflict serialisable schedule is serialisable
on conflict serialisable
only consider committed transaction
schedule with abort
However, if T2 did not commit, we abort T1 and cascade to T2
need to avoid cascading abort
- if writes an object, then can read this only after commits
recoverable and avoid cascading aborts
Recoverable: a commits only after all it depends on commits.
ACA: idea of aborting a can be done without cascading the abort to other
ACA implies recoverable, not vice versa
precedent graph test
is a schedule conflict-serialisable?
- build a graph of all transactions
- Edge from to if comes first, and makes an action that conflicts with one of
if graphs has no cycle then it is conflict-serialisable
strict
if a value written by is not read or overwritten by another until abort/commit
Are recoverable and ACA
Lock-based concurrency control
think of mutex or a lock mechanism to control access to a data object
transaction must release the lock
notation
Li(A)
means acquires lock for A, where asUi(A)
releases lock for A
Lock Type | None | S | X |
---|---|---|---|
None | OK | OK | OK |
S | OK | OK | Conflict |
X | OK | Conflict | Conflict |
lock compatibility matrix
overhead due to delays from blocking; minimize throughput
- use smallest sized object
- reduce time hold locks
- reduce hotspot
exclusive lock
for write/read
strict two phase locking (Strict 2PL)
- Each must obtain a S lock on object before reading, and an X lock on object before writing
- All lock held by transaction will be released when transaction is completed
only schedule those precedence graph is acyclic
recoverable and ACA
Example:
T1 | T2 |
---|---|
L(A); | |
R(A), W(A) | |
L(A); DENIED... | |
L(B); | |
R(B), W(B) | |
U(A), U(B) | |
Commit; | |
...GRANTED | |
R(A), W(A) | |
L(B); | |
R(B), W(B) | |
U(A), U(B) | |
Commit; |
implication
- only allow safe interleavings of transactions
- and access different objects, then no conflict and each may proceed
- serial action
two phase locking (2PL)
lax version of strict 2PL, where it allow to release locks before the end
- a transaction cannot request additional lock once it releases any lock
implication
- all lock requests must precede all unlock request
- ensure conflict serialisability
- two phase transaction growing phase, (or obtains lock) and shrinking phase (or release locks)
isolation
Isolation Level | Description |
---|---|
READ UNCOMMITTED | No read-lock |
READ COMMITTED | Short duration read locks |
REPEATABLE READ | Long duration read/lock on individual items |
SERIALIZABLE | All locks long durations |
Deadlock
cycle of transactions waiting for locks to be released by each other
usually create a wait-for graph to detect cyclic actions
-
wait-die: lower transactions never wait for higher priority transactions
-
wound-wait: is higher priority than then is aborted and restarts later with same timestamp, otherwise waits