Building a Self-Healing Cluster with Gossip and CRDTs
When actors run across several nodes, every node must agree on two things: who is in the cluster, and where each actor lives. MLambda answers the first with a gossip protocol and the second with CRDTs. No external coordination service is involved.
Why gossip instead of ZooKeeper or etcd?
Coordination services work, but they add a single point of failure and an operational dependency. Gossip has different properties:
- No single point of failure. Every node is equal.
- Convergence after partitions. When a split heals, nodes reconcile their views by exchanging state.
- Linear scalability. Each node talks to a few random peers per round, whatever the cluster’s size.
- Simplicity. The exchange has three messages.
The cost is eventual consistency: nodes may briefly disagree. For routing actors that is acceptable; a message sent on a stale view is retried.
The Syn, Ack, Ack2 exchange
Each round (every second by default), a node picks up to three peers (the fan-out) and sends each a Syn with a digest per member it knows: the member’s endpoint and heartbeat sequence.
The peer compares the digests with its own view and answers an Ack with the members it knows better (a higher sequence) and requests for those the sender knows better. The sender answers the requests with an Ack2.
Node A Node B
|-- Syn [digests] ------------->|
| |-- compare with its view
|<-- Ack [newer members, asks] -|
|-- apply, then Ack2 [asked] -->|
| |-- apply
After the exchange both nodes know the union of what they knew. Merging keeps, for each member, the higher sequence.
Information reaches every node in O(log N) rounds. (Gossip.Membership, Gossip.Cluster)
Phi accrual failure detection
A node silent for five seconds might be dead, paused, or behind a congested link. A fixed timeout gives false alarms
under load and slow detection when idle. MLambda uses a phi accrual detector (Gossip.Detector), which adapts to
each node’s heartbeat pattern.
Per node it keeps the intervals between the last 100 heartbeats. Asked about a node, it computes how far the current silence is from their mean, in standard deviations (at least 100 ms), and turns that into phi, a suspicion level:
y = (elapsed - mean) / max(stdDev, 100 ms)
cdf = 1 / (1 + e^(-y * pi / sqrt 6))
phi = -log10(1 - cdf)
Phi 1 means a 10% chance the silence is normal; phi 3, 0.1%. A member goes from Up to Suspect when phi passes the threshold (8 by default), and from Suspect to Down after the suspect timeout (30 s). The detector is a value over a given clock, so it is tested without waiting.
CRDTs for the route table
Membership says which nodes exist; the route table says which route lives where. It is replicated as a CRDT, a data
type whose replicas merge without coordination (Gossip.Crdt):
- an LWW map: a key’s latest write wins, by logical time, then by node. A removal is a tombstone with its own time, so a stale replica that still has the key cannot bring it back;
- a bag: entries by a unique id, in order of their time; a removal is a tombstone, and a tombstone always wins;
- a vector clock: a counter per node.
-- on node A -- on node B, at the same time
lwwSet "account/alice" "satellite-1" table lwwSet "account/alice" "satellite-2" table
-- after lwwMerge, both hold the write with the later time (the node breaking a tie)
Merging is commutative, associative and idempotent. The specs check it on random replicas (CrdtProofsSpec).
Cluster nodes send their route table to their peers every half second.
The lifecycle of a node
Join
A node starts and contacts its seeds. It joins as Joining; the leader (the first Up member by endpoint) lets it Up, and the change spreads by gossip.
Satellite heartbeats
Every second a satellite sends its load to its cluster nodes. A heartbeat also registers the satellite again, which recovers from a registration that was lost.
Route registration
A satellite registers the routes it can run. A concrete route it hosts enters the shared table, owned by that cluster node, so every peer knows who makes it before anyone asks.
Failure and healing
A satellite that has not heartbeated for 15 seconds is dropped, and its routes with it. The next ask for one of those routes has another satellite make the actor (waiting up to 10 seconds for it). Work already sent to the lost satellite is retried on another one, at most three times.
Detection, removal and re-creation need no operator.
Consistency
- All nodes eventually agree on membership and the route table.
- Convergence takes O(log N) rounds.
- While views differ, a message may go to a node that no longer has the actor; it is retried, and the route made again where needed.
Actors themselves stay correct by handling their messages: the cluster provides the routing, the actors the guarantees.