transactions and failure recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-recovery-p2.pdf ·...

80
Transactions and Failure Recovery Instructor: Matei Zaharia cs245.stanford.edu

Upload: others

Post on 19-Jun-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Transactions and Failure Recovery

Instructor: Matei Zahariacs245.stanford.edu

Page 2: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Outline

Recap from last time

Redo logging

Undo/redo logging

External actions

Media failures

CS 245 2

Page 3: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Outline

Recap from last time

Redo logging

Undo/redo logging

External actions

Media failures

CS 245 3

Page 4: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Defining Correctness

Constraint: Boolean predicate about our DB (both logical and physical data structures)

Consistent DB: satisfies all constraints

CS 245 4

Page 5: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Transaction: Collection of Actions that Preserve Consistency

Consistent DB Consistent DB’T

CS 245 5

Page 6: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Our Failure Model

processor

memory disk

CPU

M D

Fail-stop failures of CPU & memory, but disk survives

CS 245 6

Page 7: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

T1: Read (A,t); t ¬ t´2 A=BWrite (A,t);Read (B,t); t ¬ t´2Write (B,t);Output (A);Output (B);

A:8B:8

A:8B:8

memory disk log

Undo Logging (Immediate modification)

CS 245 8

Page 8: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

T1: Read (A,t); t ¬ t´2 A=BWrite (A,t);Read (B,t); t ¬ t´2Write (B,t);Output (A);Output (B);

A:8B:8

A:8B:8

memory disk log

Undo Logging (Immediate modification)

1616

<T1, start><T1, A, 8>

CS 245 9

Page 9: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

T1: Read (A,t); t ¬ t´2 A=BWrite (A,t);Read (B,t); t ¬ t´2Write (B,t);Output (A);Output (B);

A:8B:8

A:8B:8

memory disk log

Undo Logging (Immediate modification)

1616

<T1, start><T1, A, 8>

16 <T1, B, 8>

CS 245 10

Page 10: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

T1: Read (A,t); t ¬ t´2 A=BWrite (A,t);Read (B,t); t ¬ t´2Write (B,t);Output (A);Output (B);

A:8B:8

A:8B:8

memory disk log

Undo Logging (Immediate modification)

1616

<T1, start><T1, A, 8>

16 <T1, B, 8>16

CS 245 11

Page 11: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

T1: Read (A,t); t ¬ t´2 A=BWrite (A,t);Read (B,t); t ¬ t´2Write (B,t);Output (A);Output (B);

A:8B:8

A:8B:8

memory disk log

Undo Logging (Immediate modification)

1616

<T1, start><T1, A, 8>

<T1, commit>16 <T1, B, 8>16

CS 245 12

Page 12: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Downsides of Undo Logging

Have to do a lot of I/O to commit (write all updated objects to disk first)

Hard to replicate database to another disk (must push all changes across the network)

CS 245 13

Page 13: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Redo Logging

First send Gretel up with no rope,then Hansel goes up safely with rope!

CS 245 14

Page 14: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Redo Logging (deferred modification)

T1: Read(A,t); t ← t´2; write (A,t);Read(B,t); t ← t´2; write (B,t);Output(A); Output(B)

A: 8B: 8

A: 8B: 8

memory DB

LOG

CS 245 15

Page 15: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Redo Logging (deferred modification)

T1: Read(A,t); t ← t´2; write (A,t);Read(B,t); t ← t´2; write (B,t);Output(A); Output(B)

A: 8B: 8

A: 8B: 8

memory DB

LOG

1616

<T1, start><T1, A, 16><T1, B, 16>

<T1, commit>

CS 245 16

Page 16: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Redo Logging (deferred modification)

T1: Read(A,t); t ← t´2; write (A,t);Read(B,t); t ← t´2; write (B,t);Output(A); Output(B)

A: 8B: 8

A: 8B: 8

memory DB

LOG

1616

<T1, start><T1, A, 16><T1, B, 16>

<T1, commit>

output1616

CS 245 17

Page 17: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Redo Logging (deferred modification)

T1: Read(A,t); t ← t´2; write (A,t);Read(B,t); t ← t´2; write (B,t);Output(A); Output(B)

A: 8B: 8

A: 8B: 8

memory DB

LOG

1616

<T1, start><T1, A, 16><T1, B, 16>

<T1, commit><T1, end>

output1616

CS 245 18

Page 18: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Redo Logging Rules

1. For every action, generate redo log record (containing new value)

2. Before X is modified on disk (DB), all log records for transaction that modified X (including commit) must be on disk

3. Flush log at commit4. Write END record after DB updates

flushed to disk

CS 245 19

Page 19: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Recovery Rules: Redo Logging

(1) Let S = set of transactions with<Ti, commit> (and no <Ti, end>) in log

(2) For each <Ti, X, v> in log, in forward order(earliest ® latest) do:

- if Ti Î S then Write(X, v)Output(X)

(3) For each Ti Î S, write <Ti, end>

CS 245 20

Page 20: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Combining <Ti, end> Records

Want to delay DB flushes for hot objects

Say X is branch balance:T1: ... update X...T2: ... update X...T3: ... update X...T4: ... update X...

Actions:write Xoutput Xwrite Xoutput Xwrite Xoutput Xwrite Xoutput X

CS 245 21

Page 21: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Combining <Ti, end> Records

Want to delay DB flushes for hot objects

Say X is branch balance:T1: ... update X...T2: ... update X...T3: ... update X...T4: ... update X...

Actions:write Xoutput Xwrite Xoutput Xwrite Xoutput Xwrite Xoutput X

combined <end> recordCS 245 22

Page 22: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Solution: Checkpoints

Simple, naïve checkpoint algorithm:1. Stop accepting new transactions2. Wait until all transactions finish3. Flush all log records to disk (log)4. Flush all buffers to disk (DB) (do not

discard buffers)5. Write “checkpoint” record on disk (log)6. Resume transaction processingCS 245 23

Page 23: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Example:What To Do at Recovery?

Redo log (disk):

<T1,

A,16

>

<T1,

com

mit>

Che

ckpo

int

<T2,

B,17

>

<T2,

com

mit>

<T3,

C,2

1>

Crash... ... ... ... ... ...

CS 245 24

Page 24: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Problems with Ideas So Far

Undo logging: need to wait for lots of I/O to commit; can’t easily have backup copies of DB

Redo logging: need to keep all modified blocks in memory until commit

CS 245 25

Page 25: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

+ =

CS 245 26

Page 26: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Solution: Undo/Redo Logging!

Update = <Ti, X, new X val, old X val>

(X is the object updated)

CS 245 27

Page 27: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Undo/Redo Logging Rules

Object X can be flushed before or after Ticommits

Log record (with undo/redo info) must be flushed before corresponding data (WAL)

Flush only commit record at Ti commit

CS 245 28

Page 28: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Example: Undo/Redo LoggingWhat to Do at Recovery?Undo/redo log (disk):

<che

ckpo

int>

<T1,

A, 1

0, 1

5>

<T1,

B, 2

0, 2

3>

<T1,

com

mit>

<T2,

C, 3

0, 3

8>

<T2,

D, 4

0, 4

1>

Crash... ... ... ... ... ...

CS 245 29

Page 29: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Example: Undo/Redo LoggingWhat to Do at Recovery?Undo/redo log (disk):

<che

ckpo

int>

<T1,

A, 1

0, 1

5>

<T1,

B, 2

0, 2

3>

<T1,

com

mit>

<T2,

C, 3

0, 3

8>

<T2,

D, 4

0, 4

1>

Crash... ... ... ... ... ...

T1 committed, soREDO all its updates

CS 245 30

Page 30: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Example: Undo/Redo LoggingWhat to Do at Recovery?Undo/redo log (disk):

<che

ckpo

int>

<T1,

A, 1

0, 1

5>

<T1,

B, 2

0, 2

3>

<T1,

com

mit>

<T2,

C, 3

0, 3

8>

<T2,

D, 4

0, 4

1>

Crash... ... ... ... ... ...

T1 committed, soREDO all its updates

T2 didn’t commit, soUNDO all its updates

CS 245 31

Page 31: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Non-Quiescent Checkpoints

LOG

for dirty memoryundo pages flushed

Start-ckptactive txs:T1,T2,...

endckpt

.........

...

CS 245 32

Page 32: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Non-Quiescent Checkpointsmemory

checkpoint process:for i := 1 to M do

output(buffer i)

[transactions run concurrently]

CS 245 33

Page 33: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Example 1: How to Recover?

no T1 commitLOG

T1,-a ... Ckpt

T1... Ckpt

end ... T1-b...

CS 245 34

Page 34: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Example 1: How to Recover?

no T1 commitLOG

T1,-a ... Ckpt

T1... Ckpt

end ... T1-b...

Undo T1 (undo a,b)

CS 245 35

Page 35: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Example 2: How to Recover?

LOG

... T1a ... ... T1

b ... ... T1c ... T1

cmt ...ckpt-end

ckpt-sT1

CS 245 36

Page 36: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

LOG

... T1a ... ... T1

b ... ... T1c ... T1

cmt ...ckpt-end

ckpt-sT1

Redo T1: (redo b,c)

Example 2: How to Recover?

CS 245 37

Page 37: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

What if a Checkpoint Does Not Complete?

... ckptstart ... ... T1

b ... ... T1c ...ckpt-

startckptend

LOG

start of lastcomplete

checkpoint

ckpt-start

Start recovery from last complete checkpoint

CS 245 38

Page 38: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Undo/Redo Recovery ProcessBackward pass (end of log → latest valid checkpoint start)» construct set S of committed transactions» undo actions of transactions not in S

Undo pending transactions» follow undo chains for transactions in

(checkpoint’s active list) - S

Forward pass (latest checkpoint start → end of log)» redo actions of all transactions in S

backward pass

forward passstart

check-point

CS 245 39

Page 39: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Outline

Recap from last time

Redo logging

Undo/redo logging

External actions

Media failures

CS 245 40

Page 40: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

External Actions

E.g., dispense cash at ATM

Ti = a1 a2 …... aj …... an

$

CS 245 41

Page 41: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Solution

(1) Execute external actions after commit

(2) Try to make idempotent

CS 245 42

Page 42: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Solution

(1) Execute real-world actions after commit

(2) Try to make idempotentATM

Give $$(amt, Tid, time)

$give(amt)

lastTid:time:

CS 245 43

Page 43: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

How Would You Handle These Other External Actions?Charge a customer’s credit card

Cancel someone’s hotel room

Send data into a streaming system

CS 245 44

Page 44: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Outline

Recap from last time

Redo logging

Undo/redo logging

External actions

Media failures

CS 245 45

Page 45: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Media Failure(Loss of Nonvolatile Storage)

A: 16

CS 245 46

Page 46: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

A: 16

Solution: Make copies of data!

Media Failure(Loss of Nonvolatile Storage)

CS 245 47

Page 47: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Example 1: Triple Modular RedundancyKeep 3 copies on separate disks

Output(X) --> three outputs

Input(X) --> three inputs + vote

X1 X2 X3

CS 245 48

Page 48: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Example 2: Redundant Writes, Single ReadsKeep N copies on separate disks

Output(X) → N outputs

Input(X) → Input one copy- if ok, done; else try another one

Assumes bad data can be detected!

CS 245 49

Page 49: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Example 3: DB Dump + Log

backupdatabase

activedatabase

log

If active database is lost,– restore active database from backup– bring up-to-date using redo entries in log

CS 245 50

Page 50: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Backup Database

Just like checkpoint, except that we write full database

database

create backup database:for i := 1 to DB_Size do

[read DB block i; write to backup]

[transactions run concurrently]

CS 245 51

Page 51: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Backup Database

Just like checkpoint, except that we write full database

database

create backup database:for i := 1 to DB_Size do

[read DB block i; write to backup]

[transactions run concurrently]

Restore from backup DB and log:Similar to recovery from checkpoint and logCS 245 52

Page 52: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

When Can Log Be Discarded?

check-point

DBdump

lastneededundo

not needed formedia recovery redo

not needed for undoafter system failure

not needed forredo after system failure

log

time

lastneededundo

not needed formedia recovery

CS 245 53

Page 53: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Summary

Consistency of data: maintain constraints

One source of problems: failures» Logging» Redundancy

Another source of problems: data sharing» We’ll cover this next!

CS 245 54

Page 54: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Concurrency Control

Instructor: Matei Zahariacs245.stanford.edu

Page 55: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

The Problem

T1 T2 … Tn

DB(consistencyconstraints)

Different transactions may need to access data items at the same time, violating constraints

CS 245 56

Page 56: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Example

Constraint: all interns have equal salaries

T1: add $1000 to each intern’s salary

T2: double each intern’s salary

Salaries: 2000 2000 2000 2000 20003000 3000 400040003000

600060006000 5000 5000 😱CS 245 57

Page 57: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

The Problem

Even if each transaction maintains constraints by itself, interleaving their actions does not

Could try to run just one transaction at a time (serial schedule), but this has problems» Too slow! Especially with external clients & IO

CS 245 58

Page 58: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

High-Level Approach

Define isolation levels: sets of guarantees about what transactions may experience

Strongest level: serializability (result is same as some serial schedule)

Many others possible: snapshot isolation, read committed, read uncommitted, …

CS 245 59

Page 59: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Fundamental Tradeoff

Weaker isolationlevel

Stronger isolation level

Easier to reason about (can’t see others’ changes)

See others’ changes,but more concurrency

CS 245 60

Page 60: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Interesting Fact

SQL standard defines serializability as “same as a serial schedule”, but then also lists 3 types of “anomalies” to define levels:

Isolation Level Dirty Reads Unrepeatable Reads

Phantom Reads

Read uncommitted Y Y Y

Read committed N Y Y

Repeatable read N N Y

Serializable N N N

CS 245 61

Page 61: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Interesting Fact

There are isolation levels other than serializability that meet the 2nd definition!» I.e. don’t exhibit those 3 anomalies

Virtually no commercial DBs do serializability by default, and some can’t do it at all

Time to call the lawyers?

CS 245 62

Page 62: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

In This Course

We’ll first discuss how to offer serializability» Many ideas apply to other isolation levels

We’ll see other isolation levels after

CS 245 63

Page 63: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

OutlineWhat makes a schedule serializable?

Conflict serializability

Precedence graphs

Enforcing serializability via 2-phase locking» Shared and exclusive locks» Lock tables and multi-level locking

Optimistic concurrency with validationCS 245 64

Page 64: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

OutlineWhat makes a schedule serializable?

Conflict serializability

Precedence graphs

Enforcing serializability via 2-phase locking» Shared and exclusive locks» Lock tables and multi-level locking

Optimistic concurrency with validationCS 245 65

Page 65: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Example

T1: Read(A) T2: Read(A)A ¬ A+100 A ¬ A´2Write(A) Write(A)Read(B) Read(B)B ¬ B+100 B ¬ B´2Write(B) Write(B)

Constraint: A=B

CS 245 66

Page 66: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Schedule AT1 T2Read(A); A ¬ A+100Write(A);Read(B); B ¬ B+100;Write(B);

Read(A); A ¬ A´2;Write(A);Read(B); B ¬ B´2;Write(B);

CS 245 67

Page 67: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Schedule AA B25 25

125

125

250

250250 250

T1 T2Read(A); A ¬ A+100Write(A);Read(B); B ¬ B+100;Write(B);

Read(A); A ¬ A´2;Write(A);Read(B); B ¬ B´2;Write(B);

CS 245 68

Page 68: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Schedule BT1 T2

Read(A); A ¬ A´2;Write(A);Read(B); B ¬ B´2;Write(B);

Read(A); A ¬ A+100Write(A);Read(B); B ¬ B+100;Write(B);

CS 245 69

Page 69: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Schedule BT1 T2

Read(A); A ¬ A´2;Write(A);Read(B); B ¬ B´2;Write(B);

Read(A); A ¬ A+100Write(A);Read(B); B ¬ B+100;Write(B);

A B25 25

50

50

150

150150 150

CS 245 70

Page 70: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Schedule CT1 T2Read(A); A ¬ A+100Write(A);

Read(A); A ¬ A´2;Write(A);

Read(B); B ¬ B+100;Write(B);

Read(B); B ¬ B´2;Write(B);

CS 245 71

Page 71: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Schedule CT1 T2Read(A); A ¬ A+100Write(A);

Read(A); A ¬ A´2;Write(A);

Read(B); B ¬ B+100;Write(B);

Read(B); B ¬ B´2;Write(B);

A B25 25

125

250

125

250250 250

CS 245 72

Page 72: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Schedule DT1 T2Read(A); A ¬ A+100Write(A);

Read(A); A ¬ A´2;Write(A);Read(B); B ¬ B´2;Write(B);

Read(B); B ¬ B+100;Write(B);

CS 245 73

Page 73: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Schedule DT1 T2Read(A); A ¬ A+100Write(A);

Read(A); A ¬ A´2;Write(A);Read(B); B ¬ B´2;Write(B);

Read(B); B ¬ B+100;Write(B);

A B25 25

125

250

50

150250 150

CS 245 74

Page 74: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Schedule ET1 T2Read(A); A ¬ A+100Write(A);

Read(A); A ¬ A+50;Write(A);Read(B); B ¬ B+50;Write(B);

Read(B); B ¬ B+100;Write(B);

Same as Schedule Dbut with new T2’

CS 245 75

Page 75: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Schedule ET1 T2Read(A); A ¬ A+100Write(A);

Read(A); A ¬ A+50;Write(A);Read(B); B ¬ B+50;Write(B);

Read(B); B ¬ B+100;Write(B);

A B25 25

125

175

75

175175 175

Same as Schedule Dbut with new T2’

CS 245 76

Page 76: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Want schedules that are “good”, regardless of» initial state and» transaction semantics

Only look at order of read & write operations

Example:

Sc = r1(A)w1(A)r2(A)w2(A)r1(B)w1(B)r2(B)w2(B)

Our Goal

We don’t know the logicin external client apps!

CS 245 77

Page 77: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

SC’ = r1(A)w1(A)r1(B)w1(B)r2(A)w2(A)r2(B)w2(B)

T1 T2

Example:

SC = r1(A)w1(A)r2(A)w2(A)r1(B)w1(B)r2(B)w2(B)

CS 245 78

Page 78: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

However, for SD:

SD = r1(A)w1(A)r2(A)w2(A) r2(B)w2(B)r1(B)w1(B)

Another way to view this:» r1(B) after w2(B) means T1 should be after T2 in an

equivalent serial schedule (T2 ® T1)» r2(A) after w1(A) means T2 should be after T1 in an

equivalent serial schedule (T1 ® T2)» Can’t have both of these!

CS 245 79

Page 79: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

Returning to SC

SC = r1(A)w1(A)r2(A)w2(A)r1(B)w1(B)r2(B)w2(B)

T1 ® T2 T1 ® T2

CS 245 80

No cycles Þ Sc is equivalent to a serialschedule (in this case T1, T2)

Page 80: Transactions and Failure Recoveryweb.stanford.edu/class/cs245/spr2019/slides/11-Recovery-p2.pdf · Restore from backup DB and log: Similar to recovery from checkpoint and log CS 245

OutlineWhat makes a schedule serializable?

Conflict serializability

Precedence graphs

Enforcing serializability via 2-phase locking» Shared and exclusive locks» Lock tables and multi-level locking

Optimistic concurrency with validationCS 245 81