Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1...

48
1 May 22, 2020 Database System Internals CSE 444 - Spring 2020 Two-Phase Commit (2PC)

Transcript of Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1...

Page 1: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

1May 22, 2020

Database System Internals

CSE 444 - Spring 2020

Two-Phase Commit (2PC)

Page 2: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

Announcements

§ Lab 3 due tonight

§HW5 due on Wednesday (new deadline)

§No lecture on Monday

§ Please fill out the course evaluation:https://uw.iasystem.org/survey/225399

May 22, 2020 CSE 444 - Spring 2020 2

Page 3: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

CSE 444 - Spring 2020 3

References

§Ullman book: Section 20.5

§ Ramakrishnan book: Chapter 22

May 22, 2020

Page 4: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

We are Learning about Scaling DBMSs

§ Scaling the execution of a query• Parallel DBMS• MapReduce• Spark

§ Scaling transactions• Distributed transactions• Replication• Scaling with NoSQL and NewSQL

CSE 444 - Spring 2020 4

May 22, 2020

Page 5: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

Scaling Transactions Per Second

§OLTP: Transactions per second“Online Transaction Processing”

§Amazon§Facebook§Twitter§… your favorite Internet application…

§Goal is to increase transaction throughput

CSE 444 - Spring 2020 5May 22, 2020

Page 6: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

How to Scale the DBMS?

§Can easily replicate the web servers and the application servers

§We cannot so easily replicate the database servers, because the database is unique

§We need to design ways to scale up the DBMS

CSE 444 - Spring 2020 6May 22, 2020

Page 7: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

How to Scale?

7

Application

DB Server

Connection(e.g., JDBC)

CSE 444 - Spring 2020May 22, 2020

Page 8: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

How to Scale?

8

Application

DB Server

Connection(e.g., JDBC)

CSE 444 - Spring 2020May 22, 2020

Page 9: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

How to Scale?

9Browser

DB Server

Connection(e.g., JDBC)

HTTP/SSL

httpmultiplex

CSE 444 - Spring 2020May 22, 2020

Web Server

Page 10: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

How to Scale?

10Browser

DB Server

Connection(e.g., JDBC)

HTTP/SSL…

httpmultiplex

CSE 444 - Spring 2020May 22, 2020

Web Server Farm

Page 11: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

How to Scale?

11Browser

Connection(e.g., JDBC)

HTTP/SSL…

httpmultiplex

CSE 444 - Spring 2020

Web Server Farm

May 22, 2020

Distributed DB

Page 12: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

How to Scale?

12Browser

Connection(e.g., JDBC)

HTTP/SSL…

httpmultiplex

CSE 444 - Spring 2020

Web Server Farm

May 22, 2020

Distributed DB

Hard to ensureACID

Page 13: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

Transaction Scaling Challenges§Distribution

• There is a limit on transactions/sec on one server• Need to partition the database across multiple servers• If a transaction touches one machine, life is good! • If a transaction touches multiple machines, ACID becomes

extremely expensive! Need two-phase commit

§ Replication• Replication can help to increase throughput and lower

latency• Create multiple copies of each database partition• Spread queries across these replicas• Easy for reads but writes, once again, become expensive!

CSE 444 - Spring 2020 13May 22, 2020

Page 14: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

CSE 444 - Spring 2020 14

Distributed Transactions

§Concurrency control

§ Failure recovery• Transaction must be committed at all sites or at none of

the sites!• No matter what failures occur and when they occur• Two-phase commit protocol (2PC)

May 22, 2020

Page 15: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

CSE 444 - Spring 2020 15

Distributed Concurrency Control

§ In theory, different techniques are possible• Pessimistic, optimistic, locking, timestamps

§ In practice, distributed two-phase locking• Simultaneously hold locks at all sites involved

§Deadlock detection techniques• Global wait-for graph (not very practical)• Timeouts

§ If deadlock: abort least costly local transaction

May 22, 2020

Page 16: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

CSE 444 - Spring 2020 16

Two-Phase Commit: Motivation

CoordinatorSubordinate 1

Subordinate 2

Subordinate 3

1) User decidesto commit

2) COMMIT

3) COMMIT4) Coordinatorcrashes

But I already aborted!(maybe due to crash)

What do we do now?

May 22, 2020

Page 17: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

2PC Outline

§ Phase 1: coordinator polls the subordinators whether they want to commit or abort

§ Phase 2: coordinator notifies all subordinators of the decision commit or abort

May 22, 2020 CSE 444 - Spring 2020 17

Page 18: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

CSE 444 - Spring 2020 18

2PC: Phase 1, Prepare

CoordinatorSubordinate 1

Subordinate 2

Subordinate 3

May 22, 2020

Page 19: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

CSE 444 - Spring 2020 19

2PC: Phase 1, Prepare

CoordinatorSubordinate 1

Subordinate 2

Subordinate 3

1) User decidesto commit

May 22, 2020

Page 20: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

CSE 444 - Spring 2020 20

2PC: Phase 1, Prepare

CoordinatorSubordinate 1

Subordinate 2

Subordinate 3

1) User decidesto commit

2) PREPARE

May 22, 2020

Page 21: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

CSE 444 - Spring 2020 21

2PC: Phase 1, Prepare

CoordinatorSubordinate 1

Subordinate 2

Subordinate 3

1) User decidesto commit

2) PREPARE

2) PREPARE

May 22, 2020

Page 22: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

CSE 444 - Spring 2020 22

2PC: Phase 1, Prepare

CoordinatorSubordinate 1

Subordinate 2

Subordinate 3

1) User decidesto commit

2) PREPARE

2) PREPARE

2) PREPARE

May 22, 2020

Page 23: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

CSE 444 - Spring 2020 23

2PC: Phase 1, Prepare

CoordinatorSubordinate 1

Subordinate 2

Subordinate 3

1) User decidesto commit

2) PREPARE

2) PREPARE

2) PREPARE

3) Force-write: prepare

May 22, 2020

Page 24: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

CSE 444 - Spring 2020 24

2PC: Phase 1, Prepare

CoordinatorSubordinate 1

Subordinate 2

Subordinate 3

1) User decidesto commit

2) PREPARE

2) PREPARE

2) PREPARE

3) Force-write: prepare

4) YES

May 22, 2020

Page 25: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

CSE 444 - Spring 2020 25

2PC: Phase 1, Prepare

CoordinatorSubordinate 1

Subordinate 2

Subordinate 3

1) User decidesto commit

2) PREPARE

2) PREPARE

2) PREPARE

3) Force-write: prepare

3) Force-write: prepare

4) YES

May 22, 2020

Page 26: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

CSE 444 - Spring 2020 26

2PC: Phase 1, Prepare

CoordinatorSubordinate 1

Subordinate 2

Subordinate 3

1) User decidesto commit

2) PREPARE

2) PREPARE

2) PREPARE

3) Force-write: prepare

3) Force-write: prepare

4) YES

4) YES

May 22, 2020

Page 27: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

CSE 444 - Spring 2020 27

2PC: Phase 1, Prepare

CoordinatorSubordinate 1

Subordinate 2

Subordinate 3

1) User decidesto commit

2) PREPARE

2) PREPARE

2) PREPARE

3) Force-write: prepare

3) Force-write: prepare

3) Force-write: prepare

4) YES

4) YES

May 22, 2020

Page 28: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

CSE 444 - Spring 2020 28

2PC: Phase 1, Prepare

CoordinatorSubordinate 1

Subordinate 2

Subordinate 3

1) User decidesto commit

2) PREPARE

2) PREPARE

2) PREPARE

3) Force-write: prepare

3) Force-write: prepare

3) Force-write: prepare

4) YES

4) YES4) YES

May 22, 2020

Page 29: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

29

2PC: Phase 2, Commit

CoordinatorSubordinate 1

Subordinate 2

Subordinate 3

1) Force-write:commit

2) COMMIT

2) COMMIT

2) COMMIT

3) Force-write: commit

3) Force-write: commit

3) Force-write: commit

4) ACK

4) ACK4) ACK

Transaction isnow committed! 5) Commit transaction

and “forget” it

5) Commit transactionand “forget” it

5) Commit transaction and “forget” it

5) Write: end, then forget transaction

CSE 444 - Spring 2020May 22, 2020

Page 30: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

30

2PC with Abort – Phase 1

CoordinatorSubordinate 1

Subordinate 2

Subordinate 3

1) User decidesto commit

2) PREPARE

2) PREPARE

2) PREPARE

3) Force-write: prepare

3) Force-write: abort

3) Force-write: abort

4) YES

4) No4) NO

5) Abort transactionand “forget” it

5) Abort transaction and “forget” itCSE 444 - Spring 2020May 22, 2020

Page 31: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

CSE 444 - Spring 2020 31

2PC with Abort – Phase 2

CoordinatorSubordinate 1

Subordinate 2

Subordinate 3

1) Force-write:abort

2) ABORT

3) Force-write: abort4) ACK

5) Write: end, then forget transaction

5) Abort transactionand “forget” it

May 22, 2020

Page 32: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

Recap

§ Phase 1, Prepare: collect votes• What if no response? Presume abort

§ Phase 2, send decision commit/abort• Wait for ack then write END and forget

May 22, 2020 CSE 444 - Spring 2020 32

Page 33: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

CSE 444 - Spring 2020 33

Coordinator State Machine

§ All states involve waiting for messages

COMMITTINGABORTING

INIT

Receive: CommitSend: Prepare

R: No votesFW: AbortS: Abort

R: Yes votesFW: CommitS: Commit

END

COLLECTING

R: ACKSW: EndForget

R: ACKSW: EndForget

May 22, 2020

Page 34: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

34

Subordinate State Machine

§ INIT and PREPARED involve waiting

PREPARED

COMMITTINGABORTING

INITR: PrepareFW: PrepareS: Yes voteR: Prepare

FW: AbortS: No vote

Abortand forget

R: AbortFW: AbortS: Ack

Commitand forget

R: CommitFW: CommitS: Ack

May 22, 2020 CSE 444 - Spring 2020

Page 35: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

CSE 444 - Spring 2020 35

Handling Site Failures

What to do if there is no response

§Approach 1: no site failure detection• Subordinate can only do retrying & blocking

§Approach 2: timeouts, since unilateral abort is ok• Subordinate: init state: can timeout;

prepared state is still blocking• Coordinator: collecting state can timeout

committing state is blocking

§2PC is a blocking protocol

May 22, 2020

Page 36: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

RecoveryA subordinate fails. During recovery:

§ If the last entry in the log is <COMMIT T> then the transaction is committed: REDO

§ If the last entry in the log is <ABORT T> then the transaction is aborted: UNDO

§ If no COMMIT/ABORT/PREPARE is found, then presume ABORT (why is this OK?)

§ If the last entry is <PREPARE T> then it’s hard:

May 22, 2020 CSE 444 - Spring 2020 36

Page 37: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

RecoveryA subordinate fails. During recovery:

§ If the last entry in the log is <COMMIT T> then the transaction is committed: REDO

§ If the last entry in the log is <ABORT T> then the transaction is aborted: UNDO

§ If no COMMIT/ABORT/PREPARE is found, then presume ABORT (why is this OK?)

§ If the last entry is <PREPARE T> then it’s hard:

May 22, 2020 CSE 444 - Spring 2020 37

Page 38: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

RecoveryA subordinate fails. During recovery:

§ If the last entry in the log is <COMMIT T> then the transaction is committed: REDO

§ If the last entry in the log is <ABORT T> then the transaction is aborted: UNDO

§ If no COMMIT/ABORT/PREPARE is found, then presume ABORT (why is this OK?)

§ If the last entry is <PREPARE T> then it’s hard:

May 22, 2020 CSE 444 - Spring 2020 38

Page 39: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

RecoveryA subordinate fails. During recovery:

§ If the last entry in the log is <COMMIT T> then the transaction is committed: REDO

§ If the last entry in the log is <ABORT T> then the transaction is aborted: UNDO

§ If no COMMIT/ABORT/PREPARE is found, then presume ABORT (why is this OK?)

§ If the last entry is <PREPARE T> then it’s hard:

May 22, 2020 CSE 444 - Spring 2020 39

Page 40: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

RecoveryA subordinate fails. During recovery:

§ If the last entry in the log is <COMMIT T> then the transaction is committed: REDO

§ If the last entry in the log is <ABORT T> then the transaction is aborted: UNDO

§ If no COMMIT/ABORT/PREPARE is found, then presume ABORT (why is this OK?)

§ If the last entry is <PREPARE T> then it’s hard:

May 22, 2020 CSE 444 - Spring 2020 40

Page 41: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

RecoveryA subordinate fails. During recovery:

§ If the last entry in the log is <COMMIT T> then the transaction is committed: REDO

§ If the last entry in the log is <ABORT T> then the transaction is aborted: UNDO

§ If no COMMIT/ABORT/PREPARE is found, then presume ABORT (why is this OK?)

§ If the last entry is <PREPARE T> then it’s hard:

May 22, 2020 CSE 444 - Spring 2020 41

Page 42: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

RecoveryA subordinate fails. During recovery:

§ If the last entry in the log is <COMMIT T> then the transaction is committed: REDO

§ If the last entry in the log is <ABORT T> then the transaction is aborted: UNDO

§ If no COMMIT/ABORT/PREPARE is found, then presume ABORT (why is this OK?)

§ If the last entry is <PREPARE T> then it’s hard:must re-contact coordinator to find out whether ABORT or COMMIT

May 22, 2020 CSE 444 - Spring 2020 42

Page 43: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

CSE 444 - Spring 2020 43

Observations

§ Coordinator keeps transaction in transactions table until it receives all acks

• To ensure subordinates know to commit or abort• So acks enable coordinator to “forget” about transaction

§ After crash, if recovery process finds no log records for a transaction, the transaction is presumed to have aborted

§ Read-only subtransactions: no changes ever need to be undone nor redone

May 22, 2020

Page 44: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

CSE 444 - Spring 2020 44

Presumed Abort Protocol

§ Optimization goals• Fewer messages and fewer force-writes

§ Principle• If nothing known about a transaction, assume ABORT

§ Aborting transactions need no force-writing

§ Avoid log records for read-only transactions• Reply with a READ vote instead of YES vote

May 22, 2020

Page 45: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

CSE 444 - Spring 2020May 22, 2020 45

2PC State Machines (repeat)

COMMITTINGABORTING

INIT

Receive: CommitSend: Prepare

R: No votesFW: AbortS: Abort

R: Yes votesFW: CommitS: Commit

END

COLLECTING

R: ACKSW: End

R: ACKSW: End

PREPARED

COMMITTINGABORTING

INIT

R: PrepareFW: PrepareS: Yes vote

R: PrepareFW: AbortS: No vote

Abortand forget

R: AbortFW: AbortS: Ack

R: CommitFW: CommitS: Ack

Commit and forget

Page 46: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

CSE 444 - Spring 2020May 22, 2020 46

Presumed Abort State Machines

COMMITTING

INIT

Receive: CommitSend: Prepare

R: No votesW: AbortS: Abort

R: Yes votesFW: CommitS: Commit

END

COLLECTING

R: ACKSW: End

PREPARED

COMMITTINGABORTING

INIT

R: PrepareFW: PrepareS: Yes vote

R: PrepareW: AbortS: No vote

Abortand forget

R: AbortW: Abort

R: CommitFW: CommitS: Ack

Commitand forget

Page 47: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

CSE 444 - Spring 2020 47

Summary: Two-Phase Commit Protocol§ One coordinator and many subordinates

• Phase 1: prepare• All subordinates must flush tail of write-ahead log to disk before ack• Must ensure that if coordinator decides to commit, they can commit!

• Phase 2: commit or abort• Log records for 2PC include transaction and coordinator ids• Coordinator also logs ids of all subordinates

§ Principle• Whenever a process makes a decision: vote yes/no or

commit/abort• Or whenever a subordinate wants to respond to a message: ack• First force-write a log record (to make sure it survives a failure)• Only then send message about decision

§ “Forget” completed transactions at the very end• Once synchronized, or transaction has committed or aborted, all

nodes can stop logging any more information about that transactionMay 22, 2020

Page 48: Two-Phase Commit (2PC) · 2020. 5. 22. · 2PC with Abort –Phase 1 Coordinator Subordinate 1 Subordinate 2 Subordinate 3 1) User decides to commit 2) PREPARE 2) PREPARE 2) PREPARE

Discussion

§Data replication: simple case of distributed TXN:ensure that all replicas performed the update

§ But 2PC is slow: waiting for the slowest link§Major shortcoming: need reliable coordinator§ Paxos: gives up the coordinator, even slower…

§NoSQL: give up strong consistency (i.e. ACID)§Mostly for data replication:“eventual consistency”§ Programming nightmare: how to write a TXN?

May 22, 2020 CSE 444 - Spring 2020 48