Distributed transactions

Everyone commits or nobody does · Part of Databases

Key idea

Practice

7 questions · about 4 min

Explained step by step

Distributed transaction

A distributed transaction is a set of operations on data performed across 2 or more databases. It's usually coordinated across separate nodes connected by a network, though it can also span several databases on one server. It's like 3 friends paying for dinner with 3 cards: either all 3 are charged, or none is. If the payments database and the stock database are separate, a purchase touches both.

In 1 single database, ACID already guarantees all or nothing. With several databases it's harder: each commits on its own, so they must be coordinated so all commit or all roll back as one unit. If 1 of 3 databases commits and the other 2 don't, data is broken. It's like a trip with flight, hotel and car: if the hotel fails, the flight and car must be canceled too.

Two-phase commit (2PC)

Two-phase commit (2PC) uses a coordinator node that directs the transaction across all participants. Phase 1, prepare: the coordinator asks each node if it's ready; if any doesn't answer 'prepared', it aborts. Phase 2, commit: if everyone said 'prepared', the coordinator asks them to commit; if something fails, rollback. It's like a wedding: first both say 'yes', and only then is it signed.

2PC works even with many temporary failures, which is why it's widely used. The key is the coordinator: it collects the votes and decides. If a single participant says 'not ready', the coordinator aborts everywhere. It doesn't survive every possible failure: in rare cases a person must step in by hand. It's like a match referee: without one, nobody knows if the goal counts.

2PC has problems. What if a participant crashes halfway? What if the coordinator itself crashes? It's also a blocking protocol: participants that said 'prepared' wait for the decision, with data locked, and if the coordinator disappears they can wait indefinitely. It's like keeping your hand raised waiting for the teacher to call on you, after the teacher has left.

Three-phase commit (3PC)

Three-phase commit (3PC) extends 2PC by splitting the commit phase in 2. It becomes: prepare (same as 2PC), pre-commit (the coordinator announces everyone is ready and each node must acknowledge; if it doesn't arrive in time, it aborts) and commit (same as 2PC). It's like a launch: ready, set (pre-commit), go. The middle step helps with the blocking problem.

The pre-commit phase does 2 things. First, if a node is in pre-commit, it knows every participant finished the prepare phase. Second, each phase can now have a time limit (timeout), so nobody waits forever. It's like waiting for a friend with a rule: if they're not here in 10 minutes, you leave. With 2PC you waited indefinitely; with 3PC, say, 5 seconds and you abort.

Terms in this lesson

Arki, the beaver mascot

Your next interview starts with 5 minutes today.

Free on iOS and Android. Start from zero, no account needed.

Key idea

Everyone commits or nobody does