In transaction processing, databases, and computer networking, the two-phase commit protocol (2PC) is a type of atomic commitment protocol (ACP). ... The protocol achieves its goal even in many cases of temporary system failure (involving either process, network node, communication, etc. failures), and is thus widely used.
4. Describe the Two phase commit
I believe most of people know what 2PC (two-phase commit protocol)
is and how to use it in Java or most of modern languages. Basically, it is
used to make sure the transactions are in sync when you have 2 or
more DBs.
Assume I've two DBs (A and B) using 2PC in two different locations.
Before A and B are ready to commit a transaction, both DBs will report
back to the transaction manager saying they are ready to commit. So,
when the transaction manager is acknowledged, it will send a signal
back toA and B telling them to go ahead.
5. How to work two phase commit
The steps performed in the two phases are as follows ....
Phase 1: Prepare Phase
After each slave has locally completed its transaction, it sends a “DONE”
message to the controlling site. When the controlling site has received
“DONE” message from all slaves, it sends a “Prepare” message to the
slaves.
The slaves vote on whether they still want to commit or not. If a slave wants
to commit, it sends a “Ready” message.
A slave that does not want to commit sends a “Not Ready” message. This
may happen when the slave has conflicting concurrent transactions or there
is a timeout.
6. Phase 2: Commit/Abort Phase
After the controlling site has received “Ready” message from all the
slaves −
The controlling site sends a “Global Commit” message to the
slaves.
The slaves apply the transaction and send a “Commit ACK”
message to the controlling site.
When the controlling site receives “Commit ACK” message from
all the slaves, it considers the transaction as committed.
After the controlling site has received the first “Not Ready” message
from any slave −
The controlling site sends a “Global Abort” message to the
slaves.
The slaves abort the transaction and send a “Abort ACK”
message to the controlling site.
When the controlling site receives “Abort ACK” message from all
the slaves, it considers the transaction as aborted.
7. The greatest disadvantage of the two-phase commit protocol is that it is a
blocking protocol. If the coordinator fails permanently, some cohorts will
never resolve their transactions:After a cohort has sent an agreement
message to the coordinator, it will block until a commit or rollback is
received.
Two phase commit Disadvantages
8. Example
Clients want all-or-nothing transactions
Transfer either happens or not at all
Bank A Bank B
Transfer $1000
From A:$3000
To B:$2000
client
9. Strawman solution
Bank A Bank B
Transfer $1000
From A:$3000
To B:$2000
client
Transaction
coordinator
10. Failures in a distributed system
Consistency requires agreement among multiple servers
Is transaction X committed?
Have all servers applied update X to a replica?
Achieving agreement w/ failures is hard
Impossible to distinguish host vs. network failures
This class:
all-or-nothing atomicity in distributed systems
12. Handling timeout on A/B
TC times out waiting for A (or B)’s “yes/no” response
Can TC unilaterally decide to commit?
Can TC unilaterally decide to abort?
14. What problem is Sinfonia addressing?
Targeted uses
systems or infrastructural apps within a data center
Sinfonia: a shared data service
Span multiple nodes
Replicated with consistency guarantees
Goal: reduce development efforts for system programmers
15. Sinfonia uses 2P commit
prepare
commit
action1
action2
actions…
Traditional transactions:
general but expensive
BEGIN tx
If (a > 0 && b== 0)
b = a * a
for (i = 0; i < a; i++)
b += i
END tx
Mini-transaction:
less general but efficient
BEGIN tx
If (a == 3000 && b==2000)
{
a=2000
b=3000
}
END tx
Prepare & exec
commit
Traditional
transactions
Mini-
transactions
coordinator coordinator