Lecture 7: Concurrency Control and Recovery
Lecture 7: Concurrency Control and Recovery
Lecture 7: Concurrency Control and Recovery
Recovery
Slide 2
Transaction
Slide 3
Example Transaction
Transfer $100 from one account to another:
BEGIN transaction
read balance from first account
add $100 to first account balance
write balance to first account
read balance from second account
verify balance to see if it contains at least $100
if not, ABORT transaction
subtract $100 from second account
write balance to second account
COMMIT transaction
Slide 4
Transaction (cont.)
User (application developer) must indicate:
Begin transaction
read/write/modify statements intermixed with other
programming language statements such as verify
plus either
commit - indicates successful completion or
abort - indicates program wants to roll back (erase the
previous steps of the transaction)
In order to ensure the correctness of the database,
the DBMS and the programmer must guarantee four
properties of transactions, called the ACID properties.
Slide 5
The ACID Properties of Transactions
Atomicity: A transaction happens in its entirety or not at all
What if the OS crashed after $100 was deposited to the first account?
The recovery manager of the DBMS must assure that the $100 is withdrawn from the first
account.
Consistency: If the DB starts in a consistent state, (this notion is defined by the user;
some of it may be enforced by integrity constraints) the transaction will transform it into
a consistent state.
What if a transaction just deposited $100 into an account?
The programmer must ensure that all transactions are consistent.
Isolation: Each transaction is isolated from other transactions.
The effect on the DB is as if the transaction executed by itself.
What if another transaction computed the total bank balance after $100 was deposited to the
first account?
The concurrency control subsystem must ensure that all transactions run in isolation, unless
the DBA chooses a less strict level of isolation.
Durability:If a transaction commits, its changes to the database state persist (changes
are permanent)
What if, after the commit, the OS crashed before the withdrawal was written to disk?
The recovery manager must assure that the withdrawal was at least logged.
Slide 6
Concurrency
First we will study isolation of transactions, ensured by
the concurrency control subsystem.
Slide 7
Serial Schedules
Time
S1 T1: A+=100, B-=100
T3: A=1.06*A,
B=1.06*B
S2 T1: A+=100, B-=100
T3: A=1.06*A, B=1.06*B
S3 T1: A+=100, B-=100
T2: C=A+B
S4 T1: B-=100, A+=100
T2: C=A+B
Slide 9
Allowable Concurrency*
We want to allow interleaved schedules like S2&S3,
otherwise the DBMS becomes a (very slow) batch
system.
S2 Looks good. Why?
Slide 12
Schedules as reads and writes
The expression A =1.06*A in the previous schedule
means
Read A from disk
Set A equal to 1.06*A
Write A to disk
Only the read and write to disk matter to the DBMS.
We use the notation R(A), W(A)
So henceforth we write schedules in terms of reads
and writes to the DBMS
This will be less intuitive but we will be able to
capture the DBMS activity better
Slide 13
Proving Serializability, ctd*
S2 T1: A+=100, B-=100
T3: A=1.06*A, B=1.06*B
Here is S2 with our new notation:
S2 T1: R(A),W(A), R(B),W(B)
T3: R(A),W(A), R(B),W(B)
Why is S2 serializable?
Why is S5 serializable?
S5 T4: R(C),W(C), R(D),
T5: R(D), R(E),W(E)
Slide 14
Nonconflicting Actions
S2 and S5 have a special structure that makes it
possible to prove that they are serializable. Lets
formalize this structure.
Definition: Two actions are nonconflicting if they are
in different transactions and either they access
different data items or both are reads.
The green actions in S2 and S5 actions are nonconflicting
Observation: If nonconflicting actions are commuted
then the new schedule gives the same result in the
DB as the old one
Slide 15
Conflict serializability
Definition: Two schedules are conflict equivalent if
One can be transformed into the other by commuting
nonconflicting actions
Theorem: Conflict equivalent schedules produce the
same result in the database.
Proof: Use the preceding observation
Definition: A schedule is conflict serializable if it is
conflict equivalent to a serializable schedule
Theorem: every conflict serializable schedule is
serializable
Proof: By the previoius theorem, a conflict serializable
schedule produces the same result as a serializable
schedule.
Slide 16
Precedence graphs
So far the only way we have to prove serializability is
to verify conflict serializability, and that is laborious.
But there is an easier way.
Precedence graph: One node per transaction. Edge
from Ti to Tj if an action in Ti occurs before an action
in Tj and they conflict.
Actions conflict if they act on the same data item and one of
them is a write.
Theorem: A schedule is conflict serializable if and
only if its precedence graph is acyclic (not cyclic).
Draw the precedence graph for the following
schedules to see if they are conflict serializable and
therefore serializable.
Slide 17
LO7.2: Which of these is serializable?*
S6 T1: R(A),W(A), R(B),W(B)
T2: R(A),W(A), R(B),W(B)
Slide 19
Conclusions
Serializable
Conflict
Serializable
Serial
Acyclic Precedence
Graph
Slide 20
Serializability in the real world
So far we have dealt with the theory, which shows us
how to tell if a schedule is serializable.
But a real DBMS is not presented with schedules, it
sees only a stream of transactions.
What can a real DBMS do to enforce serializability
and thus achieve the isolation ACID property?
Slide 21
Locking: Used by most DBMSs to enforce
serializability
Slide 22
How Locks Work
-- ok ok ok
Slide 23
Strict Two Phase Locking Protocol
(S2PL)
Strict 2PL is a way of managing locks during a transaction
T gets (S and X) locks gradually, as needed
T hold all locks until end of transaction (commit/abort)
All locks
5
# of locks are released
4
held by a 3 at the end,
transaction T 2 upon commit or abort
1
0
time
Slide 24
Strict 2PL guarantees serializability
Slide 25
Deadlock in DBMSs
What is a deadlock?
A cycle of transactions, T1, T2, ... , Tn=T1 where each Ti is waiting
for Ti-1 to release a lock.
Causes these transactions to sleep forever.
A Deadlock can happen whenever you allow a transaction to
wait for a lock, even with strict two phase locking.
Simple example:
T1: R(B), W(A)
T2: R(A), W(B)
Users can eliminate deadlocks by accessing resources in a
fixed order.
DBMSs typically detect deadlocks and abort the transaction
that (it thinks) has used the least resources.
Slide 26
Isolation levels*
Developers can choose how much isolation (protection)
they want There are four isolation levels defined in
the SQL standard. They involve concepts that we will
not cover in this course:
READ UNCOMMITTED allows dirty read,
unrepeatable read, and phantoms
READ COMMITTED* allows unrepeatable reads
and phantoms
REPEATABLE READ allows phantoms
SERIALIZABLE* full isolation
*These are the only levels available in Postgres
Slide 27
Review: The ACID properties
Recovery
Atomicity: All actions in the transaction happen in their
System entirety or none of them happen.
Consistency:
If each transaction is consistent, and the DB
Programmers starts in a consistent state, it ends in a
consistent state.
Concurrency
Isolation: Execution of one transaction is isolated from
Control that of other transactions.
System
Recovery Durability: If a transaction commits, its effects persist.
System
Slide 28
The Crash Recovery Manager's
Problems
Recall the typical transaction
$100 deposited to A, then $100 withdrawn from B
Recall the Recovery Manager's problems:
OS crashes after the deposit
Deposit was written to the disk
Bank loses $100
Violates Atomicity
OS crashes after commit
Neither deposit nor withdrawal was written to the disk
Transaction is committed but does not really happen
Violates Durability
Any ideas about how to solve these problems?
Slide 29
The solution
The crash recovery subsystem of every DBMSs uses
a Write Ahead Log (WAL) to manage crash recovery
(and aborts also).
The WAL is put on a separate disk from the data
(why?). It begins after each backup, which might be
taken each night.
A log record is written for every insert, update, delete,
begin-trans, commit, abort and checkpoint.
A log record contains
<XID, ID of the DB record, action, old data, new data>
before after
image image
Slide 30
Write Ahead Log (WAL)
To be a write ahead log, the log must obey these rules
The Atomic Rule: The log entry for an insert, update or
delete must be written to disk before the change is
made to the DB
The Durability Rule: All log entries for a transaction
must be written to disk before the commit record is
written to disk.
Slide 31
Practice with a T1,A,update,100,200
log* T2,C,update,1000,500
T2,D,update,500,1000
T2,commit
CRASH
Which WAL rule guarantees that your solution (REDO) will work?
Slide 32
T1,A,update,abc,de
LO7.3: Use a log* T1,A,update,de,ab
What must a recovery T2,D,update,10,0
T2,D,update,0,5
manager do after a T2,commit
crash to ensure the CRASH
atomic and durability
properties of ACID?
What are the final
values of A and D?
Does the recovery
manager return the DB
to its state at the time
of the crash?
Slide 33
Comments
Why is UNDO done in reverse and REDO done
forward?
Think of how you put on, and take off, your socks and shoes.
Instead of doing "Write 0 to D, then Write 5 to D", why
not simplify by just doing "Write 5 to D"
That will work in this simple case but not in general when
there are many data items in the log to keep track of.
Slide 34
Real recovery is more complicated
We have ignored many complexities in crash recovery
Managing normal aborts, some of which may be in progress at
the time of the crash
Managing inserts and deletes
Supporting multiple lock levels
Managing updates to structures like B+ trees when pages split
Handling crashes that happen in the middle of recovery
In the early days of DBMSs many inflexible, inefficient
and even incorrect recovery algorithms were
implemented.
Slide 35
ARIES
In the early 1990s, C. Mohan of IBM proposed a
relatively simple recovery algorithm called ARIES*
ARIES differs from our simple model in a few ways
It redoes every update, not just those of committed
transactions. Surprisingly, this simplifies the algorithm.
It logs changes when normal aborts are undone. This
handles recovery for normal aborts.
It logs undos during recovery. This handles crashes during
recovery.
And moresee CS410/587
Slide 36
Using the WAL to manage aborts
We have seen that a Write Ahead Log makes
atomicity and durability easy to achieve.
A Write Ahead Log also makes transaction abort
simple. A transaction does not have to keep track of
the changes it has made to the DB so it can undo
them in case of abort. It just uses at the Write Ahead
Log!
Slide 37
Aborting a transaction*
T1,A,update,ABC,DEF
T2,C,update,1000,500
T2,D,update,500,1000
T1,B,update,300,400
T1,A,update,DEF,GHI
T1,abort
Note that this is normal processing no crash in sight
What actions must the DBMS take to abort T1?
In what order should these actions be taken?
What guarantees that all of T1s changes to the DB
have been undone?
What if the update to B was not written to disk?
Slide 38
Exercise #1: Which of these schedules is
serial, serializable, or conflict serializable?*
S10 T1: R(C),W(C)
T2: R(C),W(C),
W(A)
T3: W(C), R(A)
T1: R(B),W(C)
S11 R(E),W(E)
T2: R(B),R(C), R(D),W(D)
T3:R(A),W(B)
T4 W(C),W(B)
R(E),W(E)
S12 T1: R(B),W(C) R(E),W(E)
T2: R(B),R(C), R(D),W(D)
T3:R(A),W(B), R(D)
T4 W(C),W(B) R(E),W(E)
Slide 39
Exercise #2: Recovery Manager*
T1,A,update,5,10
Consider this log:
T2,B,update,2,4
Show all the actions T1,A,update,10,20
T2,B,update,4,5
of a recovery manager
T3,C,update,2,3
after the crash T3,commit
T2,commit
CRASH
Slide 40
Exercise #3: Aborting a transaction*
T1,A,update,100,200
T1,A,update,200,100
T2,D,update,500,1000
T1,B,update,ABC,DEF
T1,abort
Slide 41