Try
↑ ↓ move↵ openesc close

Interview prep / Raft and consensus

Senior · Raft and consensus · System design interview question

A Raft leader answers a read from its local state. Can the answer be stale, and what does it take to serve a linearizable read?

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

A strong answer

Yes. A leader that has been cut off from the majority does not know it until it hears a higher term. In that window it will answer reads from a log that the real cluster has moved past. Serving reads locally trades correctness for latency.

To make a read linearizable the leader has to confirm it is still the leader at the time of the read. The direct way is to exchange a round of heartbeats with a majority before answering. The cheaper way is a lease: if the leader heard from a majority within the last election timeout, less a margin, then no one else can have won in the meantime, it can answer without a new round, with a margin for clock drift between nodes.

The leader must also have committed at least one entry in its current term before serving reads, so that it knows where the committed prefix ends. A new leader does this with a no-op. Followers can serve linearizable reads too. They ask the leader for its current commit index and wait until they have applied up to it.

Sources

Learn it by running it

The raft consensus 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