Clustering
Members hold one replicated log. The first member starts with --cluster; the rest join it.
Overview
A Flo cluster is a group of nodes holding the same log. One member leads at a time. Every write goes through the leader, reaches a majority of the voters before it is acknowledged, and is then applied on every member. Clients connect to any node: a write sent to a node that does not lead is carried to the leader over the peer link and answered on the node the client wrote to, once that node has applied it, so a client always reads its own writes. Reads are served locally by every node.
Membership is recorded in the log itself. A configuration entry names the members and their roles, every member applies the same entry, and a restarted member learns its group from its own data directory.
A member is a voter or a replica. Voters elect the leader and count toward the majority a write needs. A replica takes the log and serves reads, but counts toward nothing: a slow or lost replica never holds up a write.
:::note
Two limits of the current release. A cluster replicates one shard: leave
shards at its default (it resolves to one on a member) or set it to 1; more
is refused at start, and so is a data directory that was written with more
than one shard — a member starts from an empty one.
:::
Starting a Cluster
Every member proves the same shared secret at the peer port. Make it once
with flo server secret and give every member its output, in flo.toml, in a
file only its owner can read (secret_file, mode 600, owned by the user the server
runs as: flo for the Linux packages), or in the
FLO_CLUSTER_SECRET environment variable — one of the three, since a node
setting more than one refuses to start rather than ignore the rest. A node
refuses to bind the peer port without one, or with a secret flo server secret
did not make — a value typed by hand is refused rather than trusted to be
strong.
flo server secret
# flo-secret-3f9a…(64 hex characters)
[cluster]
secret = "flo-secret-…" # the same value on every node
# or: secret_file = "/etc/flo/cluster.secret"
The first member
flo server start --cluster
It leads a group of one until others join. [cluster] enabled = true in the
config file means the same as --cluster.
Joining
flo server start --join 10.0.1.10:9500
A seed is any member's peer port (listen_port + 500), as host:port;
the host may be an IPv4 address or a DNS name (a Compose service name works).
Several seeds are comma-separated. In the config file the same thing is
seeds = ["10.0.1.10:9500"] with enabled left off.
The joiner asks the members it can reach, once a second, to be added. The
leader writes a configuration entry naming it as a replica, one change at a
time, and from then on it is a member. flo cluster status shows joining
until that happens; a node still asking after thirty seconds logs what to
check (the seed addresses, the secret, and that the group has room — a group
holds at most 8 members).
Once a replica has caught up, the leader makes it a voter on its own, up to three voters and only into an odd number: the second and third members of a new cluster become voters together, once both have caught up. A two-node cluster is a leader and a live copy, not failover; failover needs three. A fourth member and later stay replicas until promoted. A joiner that never catches up within ten minutes is dropped again (it may ask to join anew).
A new cluster has one voter until both joiners have caught up and been
promoted, which takes a few seconds after they join. Before taking any node
down, wait until flo cluster members shows three voters: stopping the first
member while it is still the only voter leaves the others with no one to
elect a leader.
Restarting a member
Start it the same way it was started before, with --cluster or --join.
Its membership comes from its log, not from the flags: a member restarted with
--join needs no seed to be up to know its group, and a first member
restarted with --cluster follows whatever configuration its log holds.
A data directory that belonged to a group refuses to run without either flag: started plainly it would lead alone and take writes the group never sees.
Promoting and removing members
flo cluster members # id, role, address, last contact and lag (the leader's view)
flo cluster promote 4 # make a caught-up replica a voter; `promote 4 5` for two
flo cluster remove 3 # remove a member; it cannot rejoin under that id
Both run against any voter (a replica answers with the leader's address) and are answered once the change has committed; promoting a node that is already a voter succeeds and changes nothing. Voters change one at a time, so a second change waits for the first; a change too soon after an election is refused until the new leader has committed in its own term — retry in a moment.
promote refuses a replica that is not caught up at that moment. remove refuses to
leave no voter, and asks for --yes when it would leave an even number of
voters, or two, saying what that tolerates. After a removal that leaves an even
number, the leader promotes a caught-up replica, if one may vote, to make it
odd again.
The leader can remove itself: it keeps leading, without counting itself, until
the change commits, then steps down and the remaining voters elect a leader.
A removed node stops taking part: it answers clients
unavailable: this node was removed from the cluster, shows removed in
flo cluster status, logs every 30 seconds how to bring the machine back (a new
node id and an empty data directory, then --join), and stays removed across
restarts.
A write sent to a replica is not carried to the leader: the answer names the leader and its address, and the client retries there.
What is refused at start
| Situation | Answer |
|---|---|
--cluster and --join together | one starts the first member, the other joins; pass one |
raft_port set with neither | pick --cluster or --join, or remove the port |
--join with no seed whose address resolves | check the seed addresses, or start the first member with --cluster |
| more than one shard in cluster mode | leave shards at its default or set 1 |
a key [cluster] does not read | the line names the keys that exist |
Ports
| Port | Purpose |
|---|---|
listen_port | Client wire protocol (9000 by default) |
listen_port + 1 | Prometheus metrics |
listen_port + 2 | Dashboard and REST API |
listen_port + 500 | Peer port: Raft RPCs and forwarded writes between members (9500 by default) |
The peer port is bound only by a node started with --cluster, --join, or
seeds; a single node leaves it unbound, so there is no undeclared port to
account for. It carries membership and log contents and must not be
reachable from outside the cluster's private network. Members prove the secret
before a link carries anything, and every message on it after that is
encrypted and authenticated under keys that link alone holds: a message
altered, replayed, reordered or dropped on the way closes the link, and the
nodes reconnect.
Upgrading. Members on different versions of the peer protocol cannot link; a node that meets one logs both versions. Upgrade a cluster by stopping every node, then starting them all on the new version. Members restarted with their data keep their roles; a cluster formed anew after an upgrade that wipes the data directories has one voter again until its joiners are promoted, as above.
Node ids are derived from hostname:port unless --node-id is given, and
are stored in the data directory on first boot; to change one, start from an
empty data directory.
Writes and Reads
client → any member
├─ the member leads: append to its log, replicate, a majority acks,
│ apply, answer
└─ it does not: carry the request to the leader over the peer link,
hold the client until this member has applied the result, answer
A client never needs to know which node leads. What it can see:
| Answer | Meaning | What to do |
|---|---|---|
unavailable: electing a leader — retry | no leader was known within 5 s | retry |
unavailable: the leader is not reachable from this node — retry | this node has no link to the leader | retry, or use another node |
unavailable: lost leadership before commit — write may still apply | the leader changed while the write waited | see below |
unavailable: lost the link to the leader — write may still apply | the link the write went over dropped before an answer came | see below |
unavailable: leader changed, write not applied — retry | the entry was overwritten by a new leader | retry |
unavailable: the leader is still applying what earlier leaders committed; retry | a new leader hasn't applied earlier terms yet, and the request didn't run (below) | retry |
too many writes waiting for the leader | 1024 forwarded writes are already held on this node | back off |
overloaded: too many writes waiting for commit — back off and retry | 1024 writes are already waiting for a commit on the leader | back off |
internal error: could not hold the request until commit — write may still apply | the leader proposed the write but could not keep the request to answer it | see below |
internal error: write committed but its answer was lost — do not resend | the write committed and applied, but its answer could not be built | do not resend |
internal error: write committed but not applied on this node — do not resend | the group committed the write but this node could not apply it | do not resend |
The "may still apply" answers are honest: the entry is in a log and a later leader holding it will commit it. What to do before sending it again depends on the write:
- A plain KV put or delete can be sent again.
- A KV increment, a JSON update, a stream append or a queue push cannot: read back first.
- A conditional KV put (
NX, CAS) that now answers "condition not met" may have met its condition the first time. - A workflow start with an idempotency key can be sent again: it answers with the run the first one started.
- A namespace create sent again answers
conflict: namespace already existsif the first one landed.
Reads are local. On the node a client wrote to, the write is visible when the response arrives; on other members within about a heartbeat.
What a read reflects depends on the node that answers it, and a read doesn't check with the rest of the group:
-
A leader refuses a read with
unavailable: the leader is still applying what earlier leaders committed; retryuntil it has applied everything in its log from earlier terms, so that what was committed before it was elected is all there. A leader that has already applied its whole log is ready as soon as it's elected. Entries it doesn't yet know are committed need a majority, because they commit with the no-op it writes when elected. It usually lasts well under a second, and longer with a long backlog to apply. A leader whose only surviving peer is far behind refuses until that peer catches up, and one that can't commit its no-op keeps refusing until it can or it steps down.It refuses most writes the same way, having run nothing, so a retry is safe. Stream appends, queue pushes, time-series writes and workflow ops are among them, so clients can see this during a failover. KV's writes go ahead, since they're decided as they apply. So do worker registrations and heartbeats, and a worker's await gets no task. A membership change waits for the leader's first commit, as above says, and needn't wait for the apply after that.
/readykeeps answering 200 meanwhile, on purpose: a 503 for a few milliseconds would make load balancers flap. The exception is a member's first catch-up after anasync_flushrestart, leading or not, which answers 503catching_up. The SDKs retry this refusal on their own a few times, over a few hundred milliseconds; the CLI reports it.A read already waiting on a key (
kv get --wait) isn't refused: it answers when that key's entry applies, with the key's state at that entry, even mid-backlog. One whose wait runs out in that time can answer not found. -
A follower can lag: it may not yet have what the leader has committed.
-
A deposed leader that hasn't heard yet can answer stale. It stays leader until it hears a newer term or goes an election timeout without contact from a majority, and a write acknowledged by its successor in that window isn't in what it answers.
So a read isn't linearizable. With async_flush, what a leader has committed
can still be lost if a majority crashes
(below).
Failure Handling
Failover
One setting governs timing:
[cluster]
failover_timeout_ms = 1500 # default; minimum 100
A leader unheard for between half and all of it is replaced; heartbeats go out at a sixth of it. A leader that cannot reach a majority for that long steps down and answers every write it was holding. Elections are a poll first, so a member that was cut off does not depose a live leader when it returns. Typical failover is one to three seconds at the default.
The default is deliberately generous: a busy disk's fsync must not look like a dead leader. Lower it only on fast, quiet disks.
Durability and the diverged state
With durability = "sync" an acknowledgement means the write is on disk on a
majority. A leader whose log contradicts history a member has committed is a
bug, and the member refuses it.
With async_flush (the default) an acknowledgement comes from memory and is
flushed within a second. A majority that crashes inside that window can lose
acknowledged writes, and the survivors' logs follow the new leader.
For the same reason, a member restarted under async_flush applies nothing
from its log until a leader commits it: what it flushed may be history that a
crashed majority lost and a new leader rewrote. Until it has caught up it
serves no reads (unavailable: this node restarted and is catching up with its group's leader; retry, or read from another node) and /ready answers
503 catching_up; writes go to the leader as usual. A member normally catches
up as soon as a leader reaches it. A lone survivor, with the rest of its group
down, serves nothing until a majority is back, or until
flo server force-members makes it a group of one (below).
Either way, a member whose applied state no longer matches the group marks
itself diverged: it stops taking part (writes are refused with
unavailable: this node's data diverged from the group and it takes no writes; use another node, reads still serve what it has), and flo cluster status and its log both say what to do: stop it, move its data directory
aside, and start it again with --join naming a live member — not
--cluster. It rejoins with no log, as below.
A halted shard
A shard that reaches a log entry every replica fails to apply the same way
halts at it, on every replica together. It applies nothing past that
entry and takes no writes until the entry is repaired, but it still serves
reads. Restarting doesn't help: a halted shard comes up halted. The node
stays ready, since a 503 would pull the whole group; /status shows the
shard as halted with the reason and halt_index, the entry it halted at,
and the log says the same.
On a single node, stop it and run flo server repair skip
with the shard and halt_index: it appends a repair that skips that entry,
and the shard comes up past it. The skip is permanent: the write never runs,
on this build or a later one. A cluster member's shard can't be repaired yet:
it has to be repaired by the cluster, and nothing proposes that skip yet.
| Answer | Meaning | What to do |
|---|---|---|
a read, marked stale | served from the state just before the halting entry; a not-found is marked too | use it knowing it's stale |
unavailable: shard N is halted at …; this request was not run (reason shard_halted) | a write sent to the halted shard | don't retry: it fails the same way until the repair |
outcome_pending (reason shard_halted) | the write committed at or past the halting entry but hasn't applied | don't resend it unchanged; see below |
outcome_pending's message says which case it is. A write past the halt
applies when the shard resumes. The write the shard halted at never runs: a
repair skips that entry. Resending either unchanged would write it twice, or
halt the shard again. The CLI exits 6 on outcome_pending and 3 on the
refused write; the SDKs never retry either. An SDK worker's registration,
group join and polls keep backing off on shard_halted instead, up to about
30 seconds apart, and resume by themselves once the shard is repaired.
Membership changes still go through, so a halted group can replace a dead node.
A member that lost its data
Start it with --join, never with --cluster. A member that starts with
--join and no log — a new node, or one that lost its disk — may have voted,
or held writes a majority counted, before it lost them. So it grants no vote
and never stands for election until:
- a leader has caught it up: it holds everything the leader had committed, and
- enough of the other members have confirmed the current term that every majority includes one of them — for the latest committed member list and any newer one — asked no sooner than one election timeout after it started, so an election it voted in before has finished.
Its acknowledgements reach the leader but count toward no commit until then.
A member that kept its log but lost its hard state (the file holding its term and vote) is guarded the same way, without the catch-up: its log is real, but the vote it cast in the current term is gone.
After that it votes only in later terms. While it is guarded the group
tolerates one fewer failure. flo cluster status shows guarded: catching up,
then guarded: confirming the term. A restart part way through keeps it
guarded.
A node whose data directory has hard state but no log refuses to start with
--cluster: it was a member, and founding would start a second cluster. A
node with an empty data directory started with --cluster founds a new
cluster and logs a warning saying so.
When a majority lost its data
No leader can be elected: the surviving member needs votes, and members that lost their logs give none. The members waiting say so in their logs every 30 seconds. To recover:
- Stop every node.
- On each node that still has its data, run
flo server inspect --data-dir <dir>. Pick the most up-to-date log: the highest last term, then the highest last index. Prefer one whose lost-log guard is off. Writes that node never received are lost. - On that node,
flo server force-members --data-dir <dir>shows what it would change; add--yesto make it the only voter. It also retires the node's cluster secret, and the server refuses to start with that secret again. - Make a new secret with
flo server secret, give it to the survivor, and start the survivor. - On every other node, move its data directory aside, give it the new secret,
and start it with
--joinnaming the survivor.
Reset the other nodes' data before they rejoin: with their old logs and the old secret, they could still form a group of their own.
The guard also waits forever, by design, in a few smaller cases. Each is
recovered the same way, with inspect and force-members:
- a group of two whose other member is gone;
- a group growing from one member to two, when the first restarts before the joiner has caught up;
- a group of three with one member down and another guarded.
Monitoring
Cluster status
flo cluster status
Cluster Status
──────────────
Node ID: flo-24e3b2
Address: 127.0.0.1:9000
Role: leader
Leader: flo-24e3b2
Term: 3
Members: 3
| Role | Meaning |
|---|---|
leader | this node takes writes for the group |
follower | a voter; writes are carried to the leader |
replica | a member that takes the log and serves reads, without a vote; writes are answered with the leader's address |
removed | removed from the cluster; takes no part |
electing | a member choosing a leader; Leader: none |
joining | asking to be added; not a member yet |
guarded: catching up | no log or no hard state of its own (new, or lost); following a leader, no vote |
guarded: confirming the term | caught up; asking the members for their term, no vote |
diverged | stopped taking part; see above |
--output json gives the same as one object, with "leader_id": null while
none is known. A single node reports itself as the leader of a one-member
group.
Prometheus metrics
The node serves the Prometheus exposition format on the metrics port
(listen_port + 1 by default, so 9001 for a node on 9000):
curl localhost:9001/metrics
On the same port, GET /health answers ok while the process is up,
GET /ready answers 200 ready only when this member can serve; any state that
can't serve is a 503 naming it (the states, in order), and
GET /status gives each shard's state, role, applied and commit index as JSON.
A load balancer should route on /ready. See REST API.
A new leader applying earlier terms is the one brief exception: it stays 200
while it refuses reads and most writes, so clients retry.
:::note
The metrics port binds to 127.0.0.1 by default. /metrics and /health
are unauthenticated, as the dashboard is, so the
endpoint is loopback-only unless you opt in. To scrape from another host set:
[metrics]
bind = "0.0.0.0"
Prefer a node-local collector or a sidecar over exposing the port directly. :::
Metric families include server counters (flo_commands_total,
flo_bytes_received_total, flo_connections_current, flo_uptime_seconds),
per-shard series (flo_shard_*), per-stream / per-queue / per-KV-namespace
counters, workflow and processing aggregates, and the peer link counters.
Per-KV-namespace memory, each a lower bound on what the node holds:
flo_kv_bytes_stored (keys and current values, tombstones and expired values
included) and flo_kv_history_bytes (older versions, value bytes plus 64
each), with flo_kv_history_evicted_total counting versions evicted since the
process started, replay included. Every node reports its own full copy, so
across a cluster take the max of these, not the sum.
The peer link counters:
| Metric | Description |
|---|---|
flo_replication_peers_linked | Peer links up right now |
flo_replication_peer_disconnects_total | Links that went down |
flo_replication_handshake_failures_total | Peers refused at the handshake (a wrong secret, mostly) |
flo_replication_frames_rejected_total | Frames that failed a check; each closed its link |
flo_replication_frames_dropped_total | Frames the shard could not queue |
flo_replication_slow_peer_drops_total | Links dropped for not reading |
The same counters are available as JSON from the dashboard at
GET /api/v1/metrics, under replication. A peers_linked that stays below
the member count minus one names a link that is down.
A node's shards hand each other client requests for keys they own. A cluster member runs one shard, so this happens only on a single node of several shards; what clients can see from it, and its metrics, are under Cross-shard requests.
:::note
There are no flo_raft_* families yet (role, term, commit index, per-member
lag); flo cluster status on each node is the signal until they land
(issue #96).
:::
Configuration
[cluster]
enabled = false # true starts the first member (same as --cluster)
# secret = "flo-secret-…" # required whenever the peer port is bound; from `flo server secret`
# secret_file = "/path" # or the secret in a file only its owner can read
# node_id = 1 # default: derived from hostname:port, then stored
# raft_port = 0 # 0 = listen_port + 500
# seeds = ["10.0.1.10:9500"] # peer ports of members to join
# failover_timeout_ms = 1500 # minimum 100
These are the only keys [cluster] reads; any other key is refused at
start, not ignored, and the line names the ones that exist. There are no
per-timer settings (everything derives from failover_timeout_ms), no
replication factor (every member holds a full copy, so it is the cluster
size), and no gossip (members talk over the peer port).
Recommended Topologies
Development. One node; nothing to configure.
Production. Three voters (tolerates one failure) or five (tolerates two;
promote the fourth and fifth with flo cluster promote),
the peer port reachable only inside the private network, and
durability = "sync" where an acknowledged write must survive a majority
crash.
[storage]
durability = "sync"
[cluster]
secret = "flo-secret-…" # the same value on every node
seeds = ["10.0.1.10:9500"]