Try
↑ ↓ move↵ openesc close

Interview prep / Two-phase commit

Senior · Two-phase commit · System design interview question

What does two-phase commit cost in latency, availability and throughput, and why do many systems avoid it across services?

Sketch your own answer first, then compare it with the one below.

A strong answer

Latency: two round trips plus forced log writes at each participant and at the coordinator, and the transaction is as slow as its slowest participant.

Availability: the transaction commits only if every participant is up. With n participants each up with probability a, it commits with probability about a to the n. Add the blocking when the coordinator fails, and the locks of in-doubt transactions stall other work.

Throughput: locks are held through both rounds, so hot rows serialize.

Teams avoid it between independent services because those services do not share a transaction manager and should not lock each other. They use sagas or design to keep a transaction inside one partition instead. It remains common inside one database, where the participants are replicas or shards that the same system manages.

Sources

Learn it by running it

The two-phase commit lesson explains the mechanism behind this question, and its lab lets you change the parameters and watch the effect.

Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms