Skip to content

Scale and replication

How a cluster keeps every write: committed at once on its group's leader, gathered into Raft entries, held by a quorum before anything it caused is sent, and applied on every node; with current reads at any node, snapshots, restarts and commits of any size.

Architecture sets out the cluster: three levels of Raft groups, every node holding every group. This chapter follows one write through it, and then what keeps the cluster whole as nodes stop, fall behind, join and start again.

A write's way through a clusterAny node takes a request and checks who sent it, forwarding a write to the leader of the group it changes. The leader commits it in its own database at once, with no sync to disk per write. Group commit gathers the writes waiting into one Raft entry. Once a majority of the group's voters hold that entry, the reply, events and runner jobs it caused pass the send gate and leave the forge. Every node applies each entry, row by row and in log order; learners serve reads, current ones after checking with the leader.ON THE GROUP'S LEADERAny nodechecks who asks,forwards a writeLeader commitsin its database,no fsync per writeGroup commitwaiting writesbecome one entryQuorum holds ita majority ofvoters have itThen it is sentreplies, events andjobs pass the gateON EVERY NODEEach entry applied, row by row, in log orderLearners serve reads; a current read first checks with the leader (ReadIndex).A commit larger than one entry goes in chunks, applied whole at the last one.A write's way through a clusterAny node takes a request and checks who sent it, forwarding a write to the leader of the group it changes. The leader commits it in its own database at once, with no sync to disk per write. Group commit gathers the writes waiting into one Raft entry. Once a majority of the group's voters hold that entry, the reply, events and runner jobs it caused pass the send gate and leave the forge. Every node applies each entry, row by row and in log order; learners serve reads, current ones after checking with the leader.ON THE GROUP'S LEADERAny nodechecks who asks, forwards a writeLeader commitsin its database, no fsync per writeGroup commitwaiting writes become one entryQuorum holds ita majority of voters have itThen it is sentreplies, events and jobs pass the gateON EVERY NODEEach entry applied, in log orderLearners serve reads, currentones after checking with the leader
A write is committed on its group's leader at once, gathered with others into one Raft entry, and held by a quorum before its reply, its events or a runner's job leave the forge. Every node applies the entry.

A write, start to end

Any node takes it. People, agents and runners connect to whichever node they reach. That node checks who is asking, as it would for a read. A write changes one group, so the node forwards it to that group’s leader over the Raft port, where each end proves its node key in a TLS 1.3 handshake with raw public keys. The leader takes the actor the forwarding node names, and the receipt for the write names both the actor and the node that vouched for it, as Google’s front ends pass the user they authenticated to their backends.

The leader commits it at once. A group’s databases are Turso databases in MVCC mode. Every write is a concurrent transaction that appends to the database’s own log with no sync to disk per commit, and the writer goes straight on. Writes that must be strictly ordered with one another all write one row, so the database’s conflict check orders them: every landing moves main’s head, and every approval writes its request.

Group commit makes it an entry. The leader’s replicator receives each committed transaction’s log frame in log order and gathers what is waiting into one Raft entry, with several entries in flight at once. The busier the forge, the larger each entry, and a writer waits only when the in-flight limit is reached.

A quorum holds it. The entry is committed once a majority of the group’s voters hold it. Each database keeps a durable watermark: its last commit a quorum holds.

Then, and only then, it is sent. Replies, events, runners’ jobs and checkpoints for witnesses all pass one gate: nothing leaves the forge until the watermarks of the groups it read cover what it saw. A client never sees a write the cluster could still lose, and a group that lacks its quorum holds back only the answers that read it.

Every node applies it. Followers apply each entry’s transactions row by row, each table found by its name, the way Turso’s sync engine replays this log, and keep their own indexes.

Reads

A read of mutable state that must be current, such as main or a change’s state, uses Raft’s ReadIndex. The node asks the group’s leader for its commit index. The leader confirms it still leads with one round to a majority. The node answers once it has applied the log that far. No clock is trusted. Reads that may be a moment behind, such as a list of changes, are answered by the node from what it holds.

Who votes

Each group has at most three voters, which commit its writes; every other node follows it as a learner, holding the same databases and serving reads without voting. Adding nodes therefore adds readers without slowing commits.

Nodes in the cluster Each group’s members
One It votes alone.
Two One voter and one learner.
Three or more Three voters; the rest learners.

A node that stops, or loses its machine for a while, keeps its vote: the others are a majority meanwhile, and it catches up when it starts again. A node that is retired (af stop, which ends its enrollment) is taken out of each group’s voters, and the group’s leader then promotes the learner that was enrolled longest and has stayed caught up for ten seconds, as Consul’s autopilot waits for a server to stabilize. A live voter is never made a learner again. A node joins only once the platform enrolls it, on the word of one of its administrators, and takes the cluster’s key from a live member, sealed to its own node key. af cluster peers shows each group’s voters and learners as a node sees them.

Snapshots and catching up

Raft keeps a group’s entries until a checkpoint past them is durable. Each node cuts its log as etcd does: once 10,000 entries have been applied since its last snapshot, it takes one, and keeps the 5,000 entries before it for a slow follower to catch up from.

A snapshot is the group’s two database files as of a cut: the end of a committed entry, at a commit time that every earlier frame is at or below and every later one above. A follower further behind than the leader’s log reaches copies the leader’s files at a cut, over the Raft port, in ranges, and starts again from them. TiKV and etcd send their snapshots apart from their Raft messages in the same way.

Restarts and new leaders

A node that stops and starts again, or a leader that is deposed, reopens each database from its file alone and replays Raft from the entry recorded with that file’s cut. Its own transaction log is needed only while it runs, so it is never synced for recovery. A new leader takes writes only after it has applied everything committed before its term. A write that reaches it before then waits, at most as long as the group’s longest election, as etcd’s new leader holds proposals until its first entry commits. One that reaches a node that does not lead the group is refused at once as unavailable, to be made again, and one still waiting when that time runs out is refused the same way. The node a write first reaches decides once where it is made: there, if that node leads the group, and otherwise at the leader it knows of. While it knows of none, as during an election, it waits for one at most as long as the group’s longest election, and then refuses the write as not made. It never sends a write anywhere else on its own: a refusal from the leader ends it, and the message says what is known of what it did.

Commits of any size

No write is refused for its size. A Raft entry is at most one record of the Raft log, 4 MiB by default. A commit whose frame is larger goes in chunks, each alone in an entry. Each chunk names its commit, the frame’s whole length and SHA-256, and its place among the chunks. Every node checks the frame against them and applies it at the last chunk, whole or not at all, as HashiCorp’s go-raftchunking does for Vault. A follower keeps only where each chunk is in the log while it waits, not the bytes, and no cut passes a frame whose last chunk has not arrived. If the leader that sent the chunks is lost, its unfinished frame is dropped, as its commit never completed.

The repository’s object store adds a layer of its own. A file larger than 1 MiB is kept as 1 MiB chunks, each written in its own transaction, and the object’s row last, so no reader sees part of a file.

What a cluster promises

  • A write a client is told about is held by a quorum of its group’s voters.
  • A current read, at any node, reflects every write committed before it.
  • A commit is applied whole on every node, or not at all, whatever its size.
  • One repository’s writes never wait for another’s: each group commits in its own order.

Growing beyond one cluster, by adding clusters each holding a set of organizations, is designed but not built.

Next

  • Operator lesson 3: A cluster: three nodes on one machine, a leader stopped and a voter retired, watched with af cluster peers.
  • Architecture: the three levels and the cluster map.
  • Keys and the vault: the cluster’s key, held only in memory, and how a joining node gets it.
  • af-server in the reference: starting a node and joining it to a cluster.

Type to search…

↑↓ navigate↵ selectEsc close