Introduction
For much of this semester, we assumed that transactions ran on databases where all of the data existed on one node (machine). This is often the case for databases with lighter workloads, but as demand increases, databases scale out to improve performance by using a Shared Nothing architecture. Each node receives a partition of the data set that is distributed based on a range or hash key and is connected to other nodes through a network. Distributed Transactions are needed for executing queries in distributed databases as a transaction may need to perform reads and writes on data that exist on different nodes.
Distributed Locking
Since every node contains data that is independent of any other node’s data, every node can maintain its own local lock table. This works if the data fits entirely within that node, such as pages or tuples of data. For coarser grained locks on data that spans multiple nodes, such as a table or database, the coarser grained locks can either be given to all nodes containing a partition of that data or be centralized at a predetermined master node. This design makes locking simple as 2 phase locking is performed at every node using local locks in order to guarantee serializability between different transactions.
When dealing with locking, deadlock is always a possibility. To determine whether deadlock has occurred in a distributed database, the waits-for graphs for each node must be unioned to find cycles as transactions can be blocked by other transactions executing on different nodes. Do this by drawing all waits-for graphs on top of each other. If a cycle is detected in this unioned graph, then there is a deadlock within the distributed system.

Two Phase Commit (2 PC)
With a distributed database where transactions can be running on multiple nodes, how do the various nodes know whether to commit or abort a transaction? In either case, commit or abort, we want to ensure that all nodes reach consensus, which is to say that all nodes agree on one course of action. Consensus is implemented through Two Phase Commit and enforces the property that all nodes maintain the same view of the data. It provides this guarantee by ensuring that a distributed transaction either commits or aborts on all nodes involved. If consensus is not enforced, some nodes may commit the transaction while others abort, causing nodes to have views of data at different points in time.
Every distributed transaction is assigned a coordinator node that is responsible for maintaining consensus among all participant nodes involved in the transaction. When the transaction is ready to commit, the coordinator initiates Two Phase Commit.
2 PC’s first phase is the preparation phase:
-
Coordinator sends prepare message to participants to tell participants to either prepare for commit or abort
-
Participants generate a prepare or abort record and flush record to disk
-
Participants send yes vote to coordinator if prepare record is flushed or no vote if abort record is flushed
-
Coordinator generates a commit record if it receives unanimous yes votes or an abort record otherwise, and flushes the record to disk
2 PC’s second phase is the commit/abort phase:
-
Coordinator broadcasts (sends message to every participant) the result of the commit/abort vote based on flushed record
-
Participants generate a commit or abort record based on the received vote message and flush record to disk
-
Participants send an ACK (acknowledgement) message to the coordinator
-
Coordinator generates an end record once all ACKs are received and flushes the record sometime in the future

The asterisk * in the diagram above means that the node must wait for that log record to flush to disk before sending the next message.
Distributed Recovery (2PC)
It is also important that the Two-Phase Commit protocol maintains consensus among all nodes even in the presence of node failures. In other words – suppose a node were to fail at an arbitrary point in the protocol. When this node comes back online, it should still end up making the same decision as all the other nodes in the database.
How do we accomplish this? Luckily, the 2PC protocol tells us to log the prepare, commit, and abort records. This means that between looking at our own log, and talking to the coordinator node, we will have all the information needed to accomplish recovery correctly (for most failure scenarios).
An important assumption we will make for now is that failures are temporary and that all nodes eventually recover. Many of 2PC’s invariants break down without this assumption, but that is beyond the scope of this class.
Let’s look at the specifics. We will analyze what happens for a failure at each possible point in the protocol, for either the participant or the coordinator.
Note that in some cases, we can determine what recovery decisions to make just by looking at our own log. In other cases, however, we might also have to talk to the coordinator; we wrap this logic in a separate process called the recovery process.
The possible failures, in chronological order:
-
Participant is recovering, and sees no prepare record.
-
This probably means that the participant has not even started 2PC yet – and if it has, it hasn’t yet sent out any vote messages (since votes are sent after flushing the log record to disk).
-
Since it has not sent out any vote messages, it aborts the transaction locally. No messages need to be sent out (the participant has no knowledge of the coordinator ID).
-
-
Participant is recovering, and sees a prepare record.
-
This situation is trickier. Looking at the diagram above, a lot of things could have happened between logging the prepare record and crashing – for instance, we don’t even know if we managed to send out our YES vote!
-
Specifically, we don’t know whether or not the coordinator made a commit decision. So the participant node’s recovery process must ask the coordinator whether a commit happened ("Did the coordinator log a commit?"). The coordinator can be determined from the coordinator ID stored in the prepare log record.
-
The coordinator will respond with the commit/abort decision, and the participant resumes 2PC from phase 2.
-
-
Coordinator is recovering, and sees no commit record.
-
The coordinator crashed at some point before receiving the votes of all participants and logging a commit decision.
-
The coordinator will abort the transaction locally. No messages need to be sent out (the coordinator has no knowledge of the participant IDs involved in the transaction since it does not log its own prepare record).
-
If the coordinator receives an inquiry from a participant about the status of the transaction, respond that the transaction aborted.
-
-
Coordinator is recovering, and sees a commit record.
-
We’d like to commit, but we don’t know if we managed to tell the participants.
-
So, rerun phase 2 (send out commit messages to participants). The participants can be determined from the participant IDs stored in the commit log record.
-
-
Participant is recovering, and sees a commit record.
- We did all our work for this commit, but the coordinator might still be waiting for our ACK, so send ACK to coordinator. (The coordinator can be determined from the coordinator ID stored in the commit log record.)
-
Coordinator is recovering, and sees an end record.
- This means that everybody already finished the transaction and there is no recovery to do.
The list above only discusses commit records. What about abort records?
We could handle them the same way as commit records (e.g. tell participants, send acks). This works fine, but is it really necessary? What if nodes just didn’t bother recovering aborted transactions?
Distributed Recovery (2PC with Presumed Abort)
It turns out if all participants and the coordinator understand that no log records means abort, we wouldn’t have to bother with recovering aborted transactions. Instead, we can assume we should abort if we see no log record. This optimization is called presumed abort, and it means that abort records never have to be flushed – not in phase 1 or phase 2, not by the participant or the coordinator.
The protocol with the presumed abort optimization looks like this:

The asterisk * in the diagram above means that the node must wait for that log record to flush to disk before sending the next message. Notice that in the presumed abort optimization, abort records no longer need to be flushed to disk.
And this is how we would recover from failures in aborted transactions, with and without presumed abort:
-
Participant is recovering, and sees no phase 1 abort record.
-
Without presumed abort: This probably means that the participant has not even started 2PC yet – and if it has, it hasn’t yet sent out any vote messages (since votes are sent after flushing the log record to disk).
-
With presumed abort: It is possible that the participant decided to abort and sent a "no" vote to the coordinator before the crash.
-
With or without presumed abort, the participant aborts the transaction locally. No messages need to be sent out (the participant has no knowledge of the coordinator ID).
-
-
Participant is recovering, and sees a phase 1 abort record.
-
Without presumed abort: Abort the transaction locally and send “no” vote to the coordinator. (The coordinator can be determined from the coordinator ID stored in the abort log record.)
-
With presumed abort: Abort the transaction locally. No messages need to be sent out! (The coordinator will timeout after not hearing from the participant and presume abort.)
-
-
Coordinator is recovering, and sees no abort record.
-
Without presumed abort: The coordinator crashed at some point before reaching a commit/abort decision.
-
With presumed abort: It is possible that the coordinator decided to abort and sent out abort messages to the participants before the crash.
-
With or without presumed abort, the coordinator will abort the transaction locally. No messages need to be sent out (the coordinator has no knowledge of the participant IDs involved in the transaction).
-
If the coordinator receives an inquiry from a participant about the status of the transaction, respond that the transaction aborted.
-
-
Coordinator is recovering, and sees an abort record.
-
Without presumed abort: Rerun phase 2 (sending out abort messages to participants). The participants can be determined from the participant IDs in the abort log record.
-
With presumed abort: Abort the transaction locally. No messages need to be sent out! (Participants who don’t know the decision will ask the coordinator later.)
-
-
Participant is recovering, and sees a phase 2 abort record.1
-
Without presumed abort: Abort the transaction locally, and send back ACK to coordinator. (The coordinator can be determined from the coordinator ID stored in the abort log record.)
-
With presumed abort: Abort the transaction locally. No messages need to be sent out! (ACKs only need to be sent back on commit.)
-
Note that the recovery processes for commit records is the same in Two-Phase Commit and Two-Phase Commit with Presumed Abort. For instance, the scenarios where the participant recovers from Two-Phase Commit with Presumed Abort and sees a prepare record or commit record is handled in the same way as in regular Two-Phase Commit. The same goes for when a coordinator recovers and sees a commit record.
To wrap everything up, here are some subtleties that you should be explicitly be aware of:
-
The 2PC recovery decision is commit if and only if the coordinator has logged a commit record.
-
Since 2PC requires unanimous agreement, it will only make progress if all nodes are alive. This is true for the recovery protocol as well – for recovery to finish, all failed nodes must eventually come back alive. If the coordinator believes a participant is dead, it can respawn the participant on a new node based on the log of the original participant, and ignore the original participant if it does come back online. However, 2PC struggles to handle scenarios where the coordinator is dead. For example, consider a scenario where all participants vote yes in Phase 1, but the coordinator crashes before sending out a commit decision. The participants will keep pinging the dead coordinator for the status of the transaction, and the system is blocked from making progress.
Practice Questions
-
Suppose that there are some transactions happening concurrently. If no two concurrent transactions ever operate on rows that are being stored in the same node, can a deadlock still happen?
-
Suppose a participant receives a prepare message for a transaction. Assuming the participant node remains online and does not fail, why might it decide to log an abort record and vote no?
-
Suppose a participant receives a prepare message for a transaction and replies
VOTE-YES. Suppose that they were running a wound-wait deadlock avoidance policy, and a transaction comes in with higher priority. Will the new transaction abort the prepared transaction? -
True or false - if we, as a participant node, are recovering and see a
PREPARErecord, it means we must have sent out aYESvote. -
True or false - if the coordinator is recovering and sees a
COMMITrecord, we can locally commit and end the process there. -
How many messages and log flushes does the presumed abort optimization skip if the transaction commits? What about if it aborts due to the participants aborting?
Solutions
-
You may be inclined to say no, since a transaction does not seem like it will ever wait on the locks of another transaction in this scenario. But remember that in addition to each node maintaining its own local lock table, coarser grained locks, such as locks for tables or the entire database, can still be shared between nodes, and the transactions may still conflict via these coarser locks.
-
The participant may decide to abort the transaction and vote no to avoid deadlock. For example, if we are using deadlock detection and we discover that the union of the waits-for graphs for each node contains a cycle the transaction is involved in, we may decide to abort the transaction. If we are using a deadlock avoidance policy, we will abort the transaction if acquiring the locks necessary to perform the transaction’s operations would require us to wait on a lower priority transaction (for wait-die) or higher priority transaction (for wound-wait).
-
No - transactions that are prepared must be ready to commit if the coordinator tells them to, so they cannot be aborted by anyone other than the coordinator.
-
False! We could have crashed between logging
PREPAREand sendingVOTE-YES. -
False! There is no guarantee that all participants received a
COMMITrecord, so we must rerun phase 2 first. -
If the transaction commits, no messages or log flushes are skipped - the messages sent and records logged are the same as the protocol without presumed abort.
If the participants abort, they do not flush any log records (skipping up to two flushes) and only have to send one message (they don’t have to send the ACK). The coordinator also does not have to flush any log records (skipping one flush), but does not skip any messages (it still sends the messages it would without presumed abort).
Past Exam Problems
Sp22 Final Q5: Distributed Money Heist (29 points)
Rithvik joins the Money Heist crew on their latest heist. The Professor, who masterminds the operation, is stationed at a remote location and can only communicate with the crew through an unreliable network. As a result, the Professor decides to use Two Phase Commit to communicate heist operations to the crew.
Assume the following for the entire question:
- The Professor is the Coordinator node.
- All latencies are in milliseconds and are symmetric (if it takes 100 ms to send a message from Rithvik to the Professor, it takes 100 ms to send a message from the Professor to Rithvik as well).
- Flushing log records takes 50 ms.
- When a participant wants to ask the coordinator about the status of a transaction, it sends a STATUS INQUIRY message.
- All other operations (except sending messages) occur instantaneously.
- If the coordinator doesn’t receive a response 1000 ms after sending out a message, it will resend the message to the participants it has not heard back from.
(a) Standard 2PC
Assume for this part that there are 3 members on the crew: Micah, Sabrina, and Rithvik (all of whom are participants and communicate with the Professor through 2PC). All participants vote YES. Assume no optimizations are made with regards to the 2PC algorithm.
- It takes 250 ms to send a message from Micah to the Professor
- It takes 350 ms to send a message from Sabrina to the Professor
- It takes 450 ms to send a message from Rithvik to the Professor
And the following events take place:
- 0 ms: the Professor begins the 2PC protocol
- 350 ms: Micah is wounded in combat (Micah crashes)
- 2000 ms: Micah recovers (in both senses)
i. (1 pt) What is the first message the Professor receives once the 2PC algorithm starts?
- A. Prepare
- B. Commit
- C. ACK
- D. Status Inquiry
- E. Vote Yes
- F. Vote No
Solution
E. Vote Yes
ii. (2 pt) When is the earliest that the Professor can determine that the decision will be a commit? Answer with a single timestamp. For example, if this happens 1200 ms after the start of the 2PC protocol, answer with “1200”.
Solution
950. This is when the Professor receives a Yes vote from everyone. (Note: 1000 was also accepted since the coordinator must log the commit decision in the case that it crashes, which would change the decision from commit to abort.)
iii. (1 pt) Is there a need to perform recovery during Phase 1 of 2PC?
- Yes
- No
Solution
No. Since Micah sends out his Yes vote before crashing we do not need to perform recovery during Phase 1.
iv. (2 pt) The first action the Professor takes after receiving Rithvik’s ack:
- A. Log Commit Record
- B. Log End Record
- C. Send Prepare Message
- D. Send Commit Message
- E. Send ACK Message
- F. Vote Yes
- G. Vote No
Solution
D. Send Commit Message
v. (2 pt) When is this action taken? Answer with a single timestamp.
Solution
2000
vi. (2 pt) Select all that apply: After time 350, Micah sends which of the following messages to the Professor?
- A. Prepare
- B. Commit
- C. ACK
- D. Status Inquiry
- E. Vote Yes
- F. Vote No
Solution
C. ACK and D. Status Inquiry
vii. (2 pt) When is the earliest time an END log record can be emitted? Answer with a single timestamp.
Solution
2550
viii. (2 pt) Select all that apply: At ____, Micah receives a commit message.
- A. 1250
- B. 2250
- C. 2350
- D. 2500
- E. 2550
Solution
B. 2250 and D. 2500
ix. (2 pt) Select all that apply: At ____, the Professor receives an ACK message.
- A. 1550
- B. 1750
- C. 1950
- D. 2550
- E. 2750
Solution
B. 1750, C. 1950, D. 2550, and E. 2750
Here is a comprehensive list of all actions taken:
Phase 1:
- 0: Professor sends prepare messages out
- 250: Micah receives prepare
- 300: Micah flushes the prepare and sends yes vote back
- 350: Sabrina receives prepare
- 350: Micah crashes
- 400: Sabrina flushes the prepare and sends yes vote back
- 450: Rithvik receives the prepare
- 500: Rithvik flushes the prepare and sends yes vote back
- 550: P receives yes vote from Micah
- 750: P receives yes vote from Sabrina
- 950: P receives yes vote from Rithvik
- 1000: P flushes the commit record
Phase 2:
- 1000: P sends commit messages out
- 1350: Sabrina receives commit message
- 1400: Sabrina flushes the commit and sends ack back
- 1450: Rithvik receives commit message
- 1500: Rithvik flushes the commit and sends ack back
- 1750: P receives ack from Sabrina
- 1950: P receives ack from Rithvik
Recovery Phase 2:
- 2000: Micah recovers
- 2000: Micah sends a STATUS INQUIRY to the professor
- 2000: The Professor receives no response from Micah in 1000 ms and resends the commit message to Micah
- 2250: Micah receives the commit message
- 2250: The Professor receives the STATUS INQUIRY from Micah
- 2250: The Professor responds to Micah’s STATUS INQUIRY with commit
- 2300: Micah flushes the commit and sends ack back
- 2500: Micah receives the second commit message from P and sends an ack back
- 2550: The Professor receives the ack from Micah
- 2550: P writes the end log record
- 2750: P receives Micah’s ack
(b) Hierarchical 2PC
Rithvik happens to be very lazy and instead outsources all his heist work to his two friends, Vik and Wiggins. However, the Professor does not know about this and Rithvik intends to keep it a secret from him. In order to perform 2PC with the Professor whilst hiding the identities of his two helpers, Rithvik performs a nested/hierarchical 2PC protocol with Vik and Wiggins.
In other words, Rithvik acts as a participant in the 2PC protocol with the Professor (same as the previous part); however, he formulates his vote by establishing consensus with Vik and Wiggins through a separate nested 2PC protocol, of which Rithvik is the coordinator and Vik and Wiggins are participants.
Assume for this part that there are 2 members on the crew that the Professor knows about:
- Aditya, who votes yes
- Rithvik, who formulates his vote through the nested 2PC protocol with Vik and Wiggins
In other words, Aditya and Rithvik are the participants directly involved in 2PC with the Professor. Rithvik formulates his decision based on the votes of Vik and Wiggins (in a nested 2PC protocol).
- Vik votes yes
- Wiggins votes no
Assume no optimizations are made with regards to the 2PC algorithm.
- It takes 100 ms to send a message from Rithvik to the Professor
- It takes 100 ms to send a message from Aditya to the Professor
- It takes 50 ms to send a message from Vik to Rithvik
- It takes 50 ms to send a message from Wiggins to Rithvik
And the following events take place:
- 0 ms: the Professor begins the 2PC protocol
i. (2 pt) What messages does Rithvik send the Professor?
- A. Prepare
- B. Commit
- C. ACK
- D. Status Inquiry
- E. Vote Yes
- F. Vote No
Solution
C. ACK and F. Vote No
ii. (1 pt) What is the final decision the Professor makes?
- A. Commit
- B. Abort
Solution
B. Abort
iii. When is the earliest that Rithvik can determine what the final decision will be? Answer with a single timestamp.
Solution
250. This is when Rithvik receives votes from Vik and Wiggins.
iv. (2 pt) When is the earliest that the Professor can determine what the final decision will be? Answer with a single timestamp.
Solution
400. This is when the Professor receives the no vote from Rithvik.
v. (2 pt) When is the earliest that Aditya can determine what the final decision will be? Answer with a single timestamp.
Solution
550. This is when Aditya receives the abort message from the Professor.
vi. (2 pt) When is the earliest that Vik can determine what the final decision will be? Answer with a single timestamp.
Solution
650. This is when Vik receives the abort message from Rithvik. (Note: 350 was also accepted, if it was interpreted that Rithvik could send the abort message to his participants at the same time that he sends the Professor his Vote No message.)
vii. (2 pt) When is the earliest that the Professor can emit an End log record? Answer with a single timestamp.
Solution
850. This is when the Professor receives Rithvik’s ack. (Note: 700 was also accepted for the same sequence of events for which 350 was accepted above. 450 was also accepted since this semester taught an option to not send ACKs upon abort.)
Here is a comprehensive list of all actions taken:
Phase 1:
- 0: P sends out prepare messages
- 100: Rithvik receives prepare message
- 100: A receives prepare message
- 100: Rithvik sends prepare message to Vik and Wiggins
- 150: Vik receives Rithvik’s prepare message
- 150: Wiggins receives Rithvik’s prepare message
- 150: A flushes the prepare and sends yes vote to the P
- 200: Vik flushes the prepare and sends yes vote to Rithvik
- 200: Wiggins flushes the abort and sends no vote to Rithvik
- 250: P receives yes vote from A
- 250: Rithvik receives yes vote from Vik
- 250: Rithvik receives no vote from Wiggins
- 300: Rithvik flushes the abort and sends no vote to the Professor
- 400: P receives no vote from Rithvik
- 450: P flushes the abort
Phase 2:
- 450: P sends out abort messages
- 550: A receives the abort message
- 550: Rithvik receives the abort message
- 600: Rithvik flushes the abort and passes the abort message on to Vik and Wiggins
- 600: A flushes the abort and sends ACK to P
- 650: Vik receives Rithvik’s abort message
- 650: Wiggins receives Rithvik’s abort message
- 700: Vik flushes the abort and sends ACK to Rithvik
- 700: Wiggins flushes the abort and sends ACK to Rithvik
- 700: P receives ACK from Aditya
- 750: Rithvik receives ACK from Vik
- 750: Rithvik receives ACK from Wiggins
- 750: Rithvik sends out ACK to P
- 850: P receives Rithvik’s ACK
Plot twist: The Professor is actually Lakshya.
Fa21 Final Q9: Distributed Transactions (17 points)
Professor Natacha Crooks and her favorite PhD students, Audrey and Neil, are writing a paper together. Before submitting, they must all agree on the finished draft. They decide to use 2PC (do not assume presumed abort), with Professor Crooks as the coordinator, in order to achieve consensus.
- It takes 2 minutes for Audrey and Neil to receive Professor Crooks’ message.
- It takes 1 minute for Professor Crooks to receive Audrey’s response.
- It takes 10 minutes for Professor Crooks to receive Neil’s response.
- Assume that everyone takes 1 minute to log and flush (15 secs to log and 45 secs to flush).
(a) (1 pt) In the best case scenario, how many minutes will Professor Crooks have to wait, starting from the beginning of the protocol, before she gets the VOTE back from both Audrey and Neil?
Solution
13
2 min from Professor Crooks to Audrey & Neil + 1 min for each to log and flush “VOTE YES” + 10 min to receive Neil’s response = 13 min. Note that Audrey’s response arrives first as Professor Crooks waits for Neil.
(b) (1 pt) In the best case scenario, how many minutes does 2PC take, starting from the beginning of the protocol to after the “END_TRANSACTION” is flushed?
Solution
28
13 from above + 1 min for Professor Crooks to log and flush “COMMIT” + 2 min from Professor Crooks to Audrey & Neil + 1 min for both to log and flush “COMMIT” + 10 min to receive Neil’s response + 1 min for Professor Crooks to log and flush “END_TRANSACTION” = 13 + 1 + 2 + 1 + 10 + 1 = 28.
(c) Commit
Both Audrey and Neil approve of the draft and will vote to COMMIT.
i. (2 pt) Who is the first person to decide to COMMIT? Select the best answer.
- A. Audrey
- B. Neil
- C. Professor Crooks
- D. Audrey and Neil
- E. Professor Crooks, Audrey, and Neil
Solution
C. Professor Crooks. She sees that everyone voted “YES”, including herself, after receiving Audrey and Neil’s votes.
ii. (1 pt) How many minutes will it take, starting from the beginning of the protocol, for the first person to decide to COMMIT? Remember that once someone decides to COMMIT, they cannot change their mind, even after recovery.
Solution
14
13 min to receive votes + 1 min for Professor Crooks to log and flush. If she did not log and flush, she might decide to COMMIT, crash, then during recovery decide ABORT.
iii. (2 pt) Who is the last person to decide to log a COMMIT record? Select the best answer.
- A. Audrey
- B. Neil
- C. Professor Crooks
- D. Audrey and Neil
- E. Professor Crooks, Audrey, and Neil
Solution
D. Audrey and Neil. Both Audrey and Neil receive Professor Crooks’ “COMMIT” simultaneously and will decide to COMMIT.
iv. (1 pt) How many minutes will it take, starting from the beginning of the protocol, for the last person to decide to COMMIT?
Solution
16
As above, after 16 min, both Audrey and Neil receive “COMMIT”. Both can decide COMMIT without logging and flushing, because on failure and recovery, they will see “VOTE YES” in their log and ask the coordinator (Professor Crooks), who will tell them COMMIT.
(d) Recovery
Recall that Professor Crooks is the coordinator.
Shadaj is very evil. He offers Professor Crooks a $1 million research grant from Facebuck, on the condition that she sabotages the paper. She might pretend to fail and take a long time to recover. Assume that both Audrey and Neil still vote to COMMIT.
i. (2 pt) Professor Crooks wants to fail such that either Audrey or Neil does not know, based on their local logs, whether the decision was COMMIT or ABORT. Select all scenarios in which this is true.
- A. Professor Crooks fails before sending the COMMIT message.
- B. Professor Crooks fails after sending a COMMIT message to Audrey (but not to Neil).
- C. Professor Crooks fails after sending all COMMIT messages.
- D. Professor Crooks fails before flushing “END TRANSACTION”.
Solution
A and B.
- A: Neither Audrey nor Neil see what each other voted.
- B: Neil does not see what Audrey voted.
ii. (2 pt) Audrey and Neil realize they can communicate with each other to find out whether the transaction committed. Select all scenarios in which one of them would still not know, without asking Professor Crooks, whether the decision was COMMIT or ABORT.
- A. Professor Crooks fails before sending the COMMIT message.
- B. Professor Crooks fails after sending a COMMIT message to Audrey (but not to Neil).
- C. Professor Crooks fails after sending all COMMIT messages.
- D. Professor Crooks fails before flushing “END TRANSACTION”.
Solution
A only. Professor Crooks can fail before logging & flushing COMMIT. Then, when she recovers, she will ABORT. Therefore Neil and Audrey must know that she has logged & flushed before deciding.
(e) Optimizations
Shreya likes to go fast. She wants to optimize 2PC to reduce the amount of logging necessary. She decides to use presumed abort.
i. (3 pt) We know that Neil does not have to log and flush if he receives “ABORT” in phase 2, then fails and recovers. Assume that all others do not fail and all network messages have been delivered. Why does Neil not have to log and flush? Select all explanations that are correct.
Note: We do not know if Neil’s “ACK” was ever sent.
- A. During recovery, he can ask Audrey (ignoring normal protocol). She will tell him to ABORT.
- B. During recovery, he can ask Professor Crooks. She will tell him to ABORT.
- C. During recovery, he might not see anything in his log, so he’ll decide to ABORT.
Solution
A, B, and C.
ii. (1 pt) Conor is impatient. He wants to know whether the paper was COMMITted as soon as possible. When is the earliest Shreya is allowed to tell him that the paper was COMMITted?
- A. After Neil or Audrey logs and flushes “YES”.
- B. After Neil and Audrey log and flush “YES”.
- C. After Professor Crooks logs and flushes “COMMIT”.
- D. After Neil or Audrey logs and flushes “COMMIT”.
- E. After Neil and Audrey log and flush “COMMIT”.
Solution
C. After Professor Crooks logs and flushes “COMMIT”. Similar reasoning to the earlier question on Natacha failing before sending COMMIT.
iii. (1 pt) When is the earliest Shreya is allowed to tell him that the paper was ABORTed?
- A. After Neil or Audrey vote “NO”, before sending the vote.
- B. After Neil and Audrey vote “NO” and Professor Crooks receives Audrey’s vote.
- C. After Neil and Audrey vote “NO” and Professor Crooks receives both votes.
- D. After Neil or Audrey receives “ABORT” from Professor Crooks.
- E. After Neil and Audrey receives “ABORT” from Professor Crooks.
Solution
A. After Neil or Audrey vote “NO”, before sending the vote. Using presumed abort, if Neil or Audrey fail, they will abort anyway, so they don’t need to log & flush. Sending the vote is an indication to Professor Crooks that they have logged & flushed, so sending the vote is not necessary.
Sp21 Final Q10: 2 Faze (Commit) 2 Furious (18 points)
(a) True or False (2 points)
For the following 4 questions, select whether the statement is True or False.
i. (0.5 pt) Presumed abort always results in fewer messages sent across the network than regular 2PC.
Solution
False. This is not true for commits.
ii. (0.5 pt) Presumed abort always results in fewer flushed log writes than regular 2PC.
Solution
False. This is not true for commits.
iii. (0.5 pt) Machine M1 can be the coordinator for transaction T1 at the same time that machine M2 is the coordinator for transaction T2.
Solution
True. There can be more than 1 coordinator, as long as each transaction is assigned to just 1 coordinator.
iv. (0.5 pt) If all participants crash at the same time, the transaction will always abort.
Solution
False. If all crashed after sending an ACK, the transaction can still commit.
(b) (16 points)
Our database runs 2 Phase Commit without any optimizations with coordinator C and 3 participants: P1, P2, and P3. The communication latencies to send a message between each machine are shown in the diagram below. All latencies are in milliseconds. The latencies are symmetric (i.e. it takes 200 ms to send a message from C to P1 and also to send a message from P1 to C).

Assume the following information:
- Flushing a log record to disk takes 50 ms. All other operations (except sending messages) occur instantaneously.
- Timeouts occur after 2500 ms.
- All participants vote YES.
- When a participant wants to ask the coordinator about the status of a transaction, it sends a STATUS INQUIRY message.
For the rest of this question, consider the following chronological sequence of events. All event times are shown in milliseconds since the 2PC round began.
- 0 ms: C begins the 2PC round.
- 500 ms: P3 crashes.
- 1000 ms: P3 recovers.
- 1500 ms: C crashes.
- 2000 ms: C recovers.
NOTE: A coordinator sending a message to all participants at the same time counts as sending ONE message.
For questions that ask at what time an event occurs, provide the time in milliseconds since the 2PC round began and input ONLY the number.
i. (1 pt) What is the 1st message C receives?
- PREPARE
- COMMIT
- ACK
- STATUS INQUIRY
- VOTE YES
- VOTE NO
Solution
VOTE YES
ii. (1 pt) What is the latest time at which C receives VOTE YES? Put NONE if C never receives VOTE YES.
Solution
850. P3 sends VOTE YES at 450 and C receives it at 850.
Full table of events for this problem:
| Time | C | P1 (200ms) | P2 (300ms) | P3 (400ms) |
|---|---|---|---|---|
| Phase 1 starts | ||||
| 0 | Sends PREPARE | |||
| 200 | Receives PREPARE, flushes PREPARE | |||
| 250 | Sends VOTE YES | |||
| 300 | Receives PREPARE, flushes PREPARE | |||
| 350 | Sends VOTE YES | |||
| 400 | Receives PREPARE, flushes PREPARE | |||
| 450 | Sends VOTE YES | |||
| 450 | Receives VOTE YES from P1 (1/3) | |||
| 500 | CRASHES | |||
| 650 | Receives VOTE YES from P2 (2/3) | (crashed) | ||
| 850 | Receives VOTE YES from P3 (3/3), flushes COMMIT | (crashed) | ||
| Phase 2 starts | ||||
| 900 | Sends COMMIT | (crashed) | ||
| 1000 | Recovers, sees PREPARE, sends STATUS INQUIRY | |||
| 1100 | Receives COMMIT, flushes COMMIT | |||
| 1150 | Sends ACK | |||
| 1200 | Receives COMMIT, flushes COMMIT | |||
| 1250 | Sends ACK | |||
| 1300 | Receives COMMIT, flushes COMMIT | |||
| 1350 | Sends ACK | |||
| 1350 | Receives ACK from P1 (1/3) | |||
| 1400 | Receives STATUS INQUIRY from P3, sends COMMIT to P3 | |||
| 1500 | CRASHES (Note: C had received 1 out of 3 ACKs before the crash, but they are now all lost.) | |||
| 1800 | (crashed) | Receives COMMIT, already in log so no need to flush, sends ACK | ||
| 2000 | Recovers, sees COMMIT, sends COMMIT | |||
| 2200 | Receives ACK from P3 (1/3) | |||
| 2200 | Receives COMMIT, already in log so no need to flush, sends ACK | |||
| 2300 | Receives COMMIT, already in log so no need to flush, sends ACK | |||
| 2400 | Receives COMMIT, already in log so no need to flush, sends ACK | |||
| 2400 | Receives ACK from P1 (2/3) | |||
| 2600 | Receives ACK from P2 (3/3), emits END | |||
| 2PC complete | ||||
| 2800 | Receives ACK from P3, ignores it |
iii. (1 pt) For this question only, assume the answer to the previous question is 750. What is the earliest time at which C sends COMMIT? Put NONE if C never sends COMMIT.
Solution
800. C would flush a COMMIT log record, which takes 50 ms and then send COMMIT.
iv. (1 pt) Select all of the following that belong in the blank. At time ____, C receives ACK.
- 1350
- 1550
- 2000
- 2400
- None of the above
Solution
1350 and 2400
v. (1 pt) Select all of the following that belong in the blank. At time ____, C sends COMMIT.
- 850
- 2000
- 2200
- 2400
- None of the above
Solution
2000
vi. (1 pt) For this question only, assume C sends 4 COMMIT messages in total. How many messages does C send in total?
Solution
5: 1 PREPARE and the 4 COMMITs.
vii. (1 pt) Select all of the following that belong in the blank. At time ____, P1 begins to flush a log record to disk.
- 100
- 200
- 1100
- 2200
- None of the above
Solution
200 and 1100
viii. (1 pt) How many messages does P1 receive in total?
Solution
3: 1 PREPARE, 2 COMMITs.
ix. (1 pt) For this question only, assume the answer to the previous question is 6. How many messages does P1 send in total?
Solution
6. P1 does not have any failure, so each message received will have a response sent.
x. (1 pt) Select all of the following that belong in the blank. At time ____, P2 begins to flush COMMIT to disk.
- 300
- 500
- 1200
- 2300
- None of the above
Solution
1200
xi. (1 pt) Select whether the following statement is True or False. P2 sends more messages than P1.
Solution
False
xii. (1 pt) Select all of the following that belong in the blank. After time 500, P3 sends ____.
- PREPARE
- COMMIT
- ACK
- STATUS INQUIRY
- VOTE YES
- VOTE NO
- P3 sends no messages after time 500.
Solution
ACK and STATUS INQUIRY
xiii. (1 pt) What machine sends the final message?
- C
- P1
- P2
- P3
Solution
P3. P3 sends an ACK at 2400.
xiv. (1 pt) What is the earliest time at which an END log record can be emitted?
Solution
2600. This is when C receives all ACKs.
xv. (2 pt) Consider an optimization to 2PC, where the coordinator flushes an ACK log record every time it receives an ACK from a participant. In case the coordinator crashes, it will know upon recovery which participants have already sent an ACK.
Using this optimization, how many milliseconds earlier can an END log record be emitted in comparison to the regular case without the optimization? For example, if this optimization allows an END log record to be emitted at 4000 and the answer to the previous question is 4500, your answer would be 500.
Solution
0. There is no change, because P2 does not send the ACK until 2600. However, C would not have to send COMMIT to P1 when C recovers, since it had already received an ACK from P1 at the time of failure.
Fa20 Final Q7: Distributed (Vaccine) Transactions (17 points)
(a) (1 pt) Which strategy do we use to perform distributed deadlock detection?
- Intersection of individual waits-for graphs
- Union of individual waits-for graphs
- Union of individual conflict dependency graphs
- Intersection of the individual conflict dependency graphs
- None of the above
Solution
Union of individual waits-for graphs. Each machine has its own waits-for graph for the locks on that machine. We use the union of the individual waits-for graphs in order to determine global deadlock across the machines.
(b) (1 pt) Assuming no presumed abort, a participant does not have to ACK a phase 2 abort message from the coordinator.
Solution
False. Unlike presumed abort, participants need to send back an ACK for both commit messages from the coordinator and abort messages from the coordinator.
(c) Message Verification
Say that we have three machines, named M1, M2, and M3, all working on the same transaction. M1 is designated the coordinator, and the other two machines are designated as participants. We pose three scenarios in the following questions. Assume the following:
- The scenarios are all independent of each other.
- The questions in this scenario are ordered in chronological order.
- We are under presumed abort.
- None of the machines crash, unless a machine is explicitly stated to have crashed in the question statement.
- Machines always receive messages, unless they are crashed at the time of the message being sent; in that case, assume that the machine did not receive the message.
- There is a network connection between M1 and M2, and a network connection between M1 and M3.
i. Scenario 1 (4 points)
For each set of potential actions, select the action(s) that would happen for the case where the transaction successfully commits. If none of the actions are correct, select “None of the above.”
We start with M1 sending a PREPARE message to all participants.
A. (1 pt)
- M2 sends a VOTE YES message to M1
- M2 sends a VOTE NO message to M1
- None of the above
Solution
M2 sends a VOTE YES message to M1. In order to commit, all of the participants need to vote yes.
B. (1 pt)
- M3 sends a VOTE YES message to M1
- M3 sends a VOTE NO message to M1
- None of the above
Solution
M3 sends a VOTE YES message to M1. In order to commit, all of the participants need to vote yes.
C. (1 pt)
- M1 sends a COMMIT message to all participants
- M1 sends an ABORT message to all participants
- None of the above
Solution
M1 sends a COMMIT message to all participants. All of the participants voted yes, so the coordinator, M1, decides to commit, and sends a COMMIT message to all the participants.
D. (1 pt)
- M2 sends an ACK message to M1
- M3 sends an ACK message to M1
- None of the above
Solution
M2 sends an ACK message to M1 and M3 sends an ACK message to M1. When the participants receive the COMMIT message, they must send an ACK back to the coordinator (M1).
ii. Scenario 2 (4 points)
Now, consider the scenario where M2 votes yes while M3 votes no. For each set of potential actions, select the action(s) that would happen. If none of the actions are correct, select “None of the above.”
We start with M1 sending a PREPARE message to all participants.
A. (1 pt)
- M2 sends a VOTE YES message to M1
- M2 sends a VOTE NO message to M1
- None of the above
Solution
M2 sends a VOTE YES message to M1. You are given that M2 votes yes.
B. (1 pt)
- M3 sends a VOTE YES message to M1
- M3 sends a VOTE NO message to M1
- None of the above
Solution
M3 sends a VOTE NO message to M1. You are given that M3 votes no.
C. (1 pt)
- M1 sends a COMMIT message to all participants
- M1 sends an ABORT message to all participants
- None of the above
Solution
M1 sends an ABORT message to all participants. Because at least one of the participants voted no, the coordinator, M1, decides to abort, and sends an ABORT message to all participants.
D. (1 pt)
- M2 sends an ACK message to M1
- M3 sends an ACK message to M1
- None of the above
Solution
None of the above. Under presumed abort, when participants receive an ABORT message from the coordinator, they do not have to send an ACK back to the coordinator.
iii. Scenario 3 (7 points)
Now, select all of the valid actions for the scenario where M1, M2, and M3 participate in a transaction that successfully commits. However, M3 crashes during the process. It is noted within the scenario when M3 crashes and when it later recovers. For each set of potential actions, select the action(s) that would happen. If none of the actions are correct, select “None of the above.”
We start with M1 sending a PREPARE message to all participants.
A. (1 pt)
- M2 logs a PREPARE record
- M2 logs an ABORT record
- None of the above
Solution
M2 logs a PREPARE record. In order for the transaction to successfully commit, all participants must decide to commit, and they will log PREPARE records accordingly.
B. (1 pt)
- M2 sends a VOTE YES message to M1
- M2 sends a VOTE NO message to M1
- None of the above
Solution
M2 sends a VOTE YES message to M1. In order for the transaction to successfully commit, all participants must decide to commit, and they will send VOTE YES messages back to the coordinator.
C. (1 pt)
- M3 logs a PREPARE record
- M3 logs an ABORT record
- None of the above
Solution
M3 logs a PREPARE record. In order for the transaction to successfully commit, all participants must decide to commit, and they will log PREPARE records accordingly.
D. (1 pt)
- M3 sends a VOTE YES message to M1
- M3 sends a VOTE NO message to M1
- None of the above
Solution
M3 sends a VOTE YES message to M1. In order for the transaction to successfully commit, all participants must decide to commit, and they will send VOTE YES messages back to the coordinator.
E. (1 pt) M3 crashes at this point, immediately after the action(s) in the previous question. What happens next?
- M1 sends a COMMIT message to all participants
- M1 sends an ABORT message to all participants
- None of the above
Solution
M1 sends a COMMIT message to all participants. M1 has received VOTE YES messages from all of the participants, so it decides to COMMIT and sends the message to all the participants.
F. (1 pt) M3 recovers at this point, and M3 asks M1 what the final decision was. What happens next?
- M1 sends a COMMIT message to M3
- M1 sends an ABORT message to M3
- None of the above
Solution
M1 sends a COMMIT message to M3. The coordinator decided to COMMIT earlier, so it sends the COMMIT message to M3 again.
G. (1 pt)
- M2 sends an ACK message to M1
- M3 sends an ACK message to M1
- None of the above
Solution
M2 sends an ACK message to M1 and M3 sends an ACK message to M1. When the participants receive the COMMIT message, they must send an ACK back to the coordinator (M1).
-
In reality, there is no difference between a phase 1 and phase 2 abort record. Therefore, for the without presumed abort case, a participant can simply respond with NO to the coordinator upon seeing an abort record. The coordinator will then treat that message as either a phase 1 vote or a phase 2 ACK accordingly. ↩