Watching and Agreeing on State

Lesson 3 of 4

Confirm a Write Against a Majority

Estimated time: 10 minutes

A write is not done until a majority agrees

A write acknowledged from a single copy looks finished the moment that copy answers, and it can be lost when that one copy fails before the change has spread. A quorum write waits for a majority of the copies to record the change before it is called done, and in return the change survives the loss of any single copy.

Choose the size from the number of copies

The quorum size is a majority of the copies that hold the value. Set it from the count, not from habit, and keep it fixed so a caller knows what was promised.

Count distinct copies, not replies

Summing replies is the easy mistake: one healthy copy that answers twice can look like a quorum when only one copy holds the change. Count distinct copies that recorded it, and treat anything below the size as a failure rather than a success.

def confirmed(acks, quorum_size):
    distinct = set(acks)
    return len(distinct) >= quorum_size

Read the quorum back

After a confirmed write, read the quorum back so a caller that was told the change succeeded can find it. A write that cannot be read back was not a write, whatever the acknowledgements said.