Why this lab exists
Every distributed system you have ever deployed needs an answer to the same question: which nodes are currently alive? Kubernetes tracks this with etcd and the control plane. Cassandra tracks it with gossip. Consul does the same. ZooKeeper uses ZAB consensus. The answer you choose determines what happens when your network hiccups at 2am.
Centralized heartbeats are the obvious first answer: nodes send heartbeats to a coordinator every second; if the coordinator misses three, the node is dead. This works fine up to a few thousand nodes. Beyond that, the coordinator processes O(N) heartbeats per second. At 10,000 nodes and one heartbeat per second, that is 10,000 messages per second hitting a single process. The coordinator becomes the bottleneck and the single point of failure simultaneously.
Gossip protocols invert this. There is no coordinator. Every node gossips to a few random peers. Information spreads like an infection — logarithmically fast, without any central bottleneck. Add SWIM’s failure detection and you have a system that scales to tens of thousands of nodes with the same constant resource usage per node.
This lab builds all three layers from scratch in Go: dissemination (v0), failure detection (v1), and piggybacking (v2). After finishing it you will understand exactly why Cassandra, Consul, and etcd all chose gossip over centralized heartbeats, and what they had to sacrifice to get there.
What we’re building
A SWIM gossip cluster in Go, implemented across three stages. Each stage adds one protocol layer and measures the result.
We build:
- A gossip node that disseminates full membership state to random peers over UDP
- SWIM probe cycles: direct Ping, indirect PingReq, Suspect state, Dead declaration
- Piggybacking: recent membership events ride on Ping/Ack messages instead of separate gossip rounds
- An HTTP server exposing the cluster to external clients
We explicitly skip (and why):
- Gossip over TCP (UDP is intentionally lossy — that is the point of indirect probing)
- Encryption and authentication (that is the security lab)
- Network partitions and split-brain reconciliation (the Raft lab handles consensus)
- Production-grade compression (LZ4 for large clusters)
Mental model
Think of gossip as epidemiology. A virus infects one person, who infects three others, who each infect three more. After k rounds, the number of infected people is at most F^k where F is the fanout. With fanout F=3:
Round 1: 3 nodes infected
Round 2: 9 nodes infected (3^2)
Round 3: 27 nodes infected (3^3)
Round 5: 243 nodes infected
Round 7: 2187 nodes infected
Round 10: 59,049 nodes infected
To infect all N nodes, you need ceil(log_3(N)) rounds. For N=1000 that is about 7 rounds. At a gossip interval of 500ms, that is 3.5 seconds to fully disseminate a membership change to a 1000-node cluster.
The SWIM paper proves this more precisely: with fanout F, after k rounds the expected number of uninformed nodes is N * ((N-F)/N)^k. The key insight is that each gossip round multiplies the infected population by F, and the exponent drops exponentially. Even with packet loss, the convergence holds because different paths through the fanout graph cover different failure modes.
Full state vs. event gossip: v0 sends the entire membership map on every gossip round. For 10 nodes this is fine. For 1000 nodes, each gossip message is 1000 entries. v2 fixes this by sending only the last K=8 recent changes (events), piggybacked onto the Ping/Ack messages that SWIM already sends for failure detection. The health check doubles as the gossip vehicle.
Prerequisites
None specific. Basic Go knowledge (goroutines, channels, net/udp) helps. Understanding the concepts of process crash vs. network partition is useful — the SWIM probe cycle treats them differently.
Stage-by-stage build
Stage 0 Simple gossip — O(log N) dissemination
- Create a GossipNode that listens on UDP and maintains a membership map
- Every 500ms, pick 3 random peers and send the full membership as JSON
- On receive, merge: add unknown nodes, update timestamps
- Write a test: 10 nodes fully converge within 3 seconds
The smallest gossip implementation that proves the convergence property. A node is an address plus a membership map. The protocol is: broadcast your entire view to three random peers on a timer; when you receive a view from someone else, merge it.
type GossipNode struct {
addr string
conn *net.UDPConn
members map[string]*Member
mu sync.RWMutex
stopCh chan struct{}
}
// doGossipRound picks GossipFanout random peers and sends full membership.
func (n *GossipNode) doGossipRound() {
n.mu.Lock()
peers := n.pickRandomPeers(GossipFanout)
snapshot := n.memberSnapshot()
n.roundCount++
n.mu.Unlock()
msg := Message{Type: "gossip", From: n.addr, Members: snapshot}
for _, peer := range peers {
n.sendTo(peer, msg)
}
}The merge rule is the core invariant: when you receive a member entry from a peer, it wins if it has a higher incarnation number, or the same incarnation with a worse status (Alive=0, Suspect=1, Dead=2). This monotone merge function means merging commutes and is idempotent — you can merge in any order and get the same result.
func (n *GossipNode) mergeMembers(remote map[string]Member) {
for addr, rm := range remote {
local, exists := n.members[addr]
if !exists {
m := rm
n.members[addr] = &m
continue
}
if rm.Incarnation > local.Incarnation {
m := rm
n.members[addr] = &m
continue
}
if rm.Incarnation == local.Incarnation && rm.Status > local.Status {
local.Status = rm.Status
}
}
}The core lesson: with fanout=3 and 10 nodes, a 10-node cluster fully converges in under 2 seconds on loopback. The math works. The problem is bandwidth: at 100 nodes, each gossip message is 100 entries. At 10,000 nodes it is 10,000 entries — v0 does not scale. That is v2’s problem to solve.
Stage 1 SWIM failure detection — indirect probing
- Add a probe loop: every 1s, Ping a random alive peer and wait 200ms for Ack
- On timeout: send PingReq to 2 other nodes, asking them to Ping the target
- After SuspectThreshold=3 failed probe cycles: mark Dead
- Write a test: stop a node, verify the rest declare it Dead within 7 seconds
v0 detects nothing. If a node crashes, it stays in every other node’s membership map with an old LastSeen timestamp. v1 adds SWIM’s probe cycle.
The probe cycle:
Every 1s (ProbeInterval):
1. Pick a random non-Dead peer as target.
2. Send Ping{seq=N} to target. Wait 200ms for Ack{seq=N}.
3. If Ack arrives: target is alive. Reset its failedProbes counter.
4. If no Ack: send PingReq{target=T} to 2 other random nodes.
Ask them to Ping T and forward any Ack.
5. If any forwarded Ack arrives: target is alive. A was the problem, not B.
6. If still no Ack: increment target.failedProbes.
failedProbes == 1 or 2: mark Suspect.
failedProbes >= 3: mark Dead.Why indirect probing matters: suppose there is 10% packet loss between node A and node B. A’s direct Ping to B fails with probability 0.10 — that is a 10% false positive rate. But if A asks C and D to probe B, the false positive rate becomes 0.10 × 0.10 × 0.10 = 0.1% — A, C, and D must all lose their packet to B in the same probe cycle. In practice this drops the false positive rate from ~10% to ~2% (some correlation between paths), which is the difference between constant pager alerts and something you can ignore.
func (n *SWIMNode) doProbeCycle() {
target := n.pickProbeTarget()
seq := n.nextSeq()
// Step 1: Direct Ping.
ackCh := n.registerAck(seq)
n.sendTo(target, Message{Type: "ping", From: n.addr, SeqNo: seq})
if n.waitAck(ackCh, ProbeTimeout) {
n.markAlive(target)
return
}
n.deregisterAck(seq)
// Step 2: Indirect PingReq via 2 helpers.
helpers := n.pickRandomPeers(2)
seq2 := n.nextSeq()
ackCh2 := n.registerAck(seq2)
for _, h := range helpers {
n.sendTo(h, Message{Type: "pingreq", From: n.addr, Target: target, SeqNo: seq2})
}
if n.waitAck(ackCh2, ProbeTimeout) {
n.markAlive(target)
return
}
n.deregisterAck(seq2)
// Both failed: increment suspect counter.
m.failedProbes++
if m.failedProbes >= SuspectThreshold {
m.Status = StatusDead
} else {
m.Status = StatusSuspect
}
}Suspect state is the key design choice. When a direct probe fails, you do not immediately declare the node dead. You mark it Suspect and gossip that. The node hears its own Suspect rumor and can refute it by bumping its incarnation number and gossiping Alive(incarnation: N+1). Only after SuspectThreshold consecutive probe failures — all of which also fail indirect probing — does the node get marked Dead. This is why Consul’s failure detection has a configurable acl_replication_rate_limit and suspect timeout: they are tuning the tradeoff between detection speed and false positives.
The Ack matching mechanism uses a sequence number map. Each probe registers a channel keyed by the probe’s sequence number. When an Ack arrives with SeqNo=N, the channel for N is closed, which unblocks the waiting goroutine. This is the same channel-based rendezvous pattern used in Go’s RPC implementations.
Stage 2 Piggybacking — dissemination for free
- Replace full-state gossip messages with event lists (last K=8 changes)
- Piggyback events onto every Ping and Ack — health check doubles as gossip
- Implement incarnation number self-refutation when a node hears it is Suspect
- Implement event decay: stop propagating an event after log2(N)+2 transmissions
- Write a test: a node declared Dead refutes and shows Alive within 3 seconds
v1 still sends full membership state in gossip rounds, separate from probe messages. v2 eliminates the separate gossip message entirely. Every Ping and Ack now carries a list of recent membership events. The health check and the gossip message become a single UDP packet.
Event piggybacking:
// Instead of: msg := Message{Type: "gossip", Members: fullSnapshot}
// v2 sends: msg := Message{Type: "ping", SeqNo: seq, Events: recentEvents}
func (n *PiggybackNode) doProbeCycleV2() {
events := n.pickEvents() // last K=8 events, incrementing transmit counts
ping := Message{
Type: "ping",
From: n.addr,
SeqNo: seq,
Events: events, // gossip rides the probe for free
}
n.sendTo(target, ping)
// Ack also carries events back the other direction:
// ack := Message{Type: "ack", SeqNo: msg.SeqNo, Events: n.pickEvents()}
}Event decay prevents old events from circulating forever. Each event tracks how many times it has been piggybacked. The decay limit is log2(N)+2, where N is the current cluster size. For N=10, that is about 5 transmissions per event. For N=1000, it is about 12. After the limit, the event is removed from the queue. This bounds the per-message overhead at O(K) regardless of cluster size.
Incarnation numbers and self-refutation solve a subtle problem: a node can be incorrectly declared Suspect by a peer with a slow link. Without refutation, the Suspect rumor spreads and eventually becomes Dead — a false positive that survives. With incarnation numbers, the falsely-suspected node hears the rumor about itself (via gossip), bumps its own incarnation, and broadcasts Alive(incarnation: N+1). Since higher incarnation always wins, the false Suspect is overridden everywhere within a few gossip rounds.
// When node B receives a Suspect event about itself:
if ev.Addr == n.addr && ev.Status == StatusSuspect {
local.Incarnation++
local.Status = StatusAlive
go n.pushEvent(n.addr, StatusAlive, local.Incarnation) // refutation
continue
}Why piggybacking matters: in v1, each node sends GossipFanout=3 gossip messages per GossipInterval=500ms, plus probe messages every ProbeInterval=1s. In v2, the gossip loop sends zero separate messages — events ride the existing probe traffic. For a 10-node cluster this saves ~85% of gossip bandwidth. For a 1000-node cluster where full-state gossip messages are enormous, the savings approach 99%.
Run it yourself
labs/gossip ↗
Go 1.23+ - 1 Clone and build
git clone https://github.com/pushkarkumar-dev/system-design-labs cd system-design-labs/labs/gossip go build ./... - 2 Run all tests (includes convergence and failure detection tests)
go test ./... -v -timeout 60s - 3 Start node 1 (HTTP on 8080, gossip UDP on 7946)
go run ./cmd/server --http-port 8080 --gossip-addr 127.0.0.1:7946 - 4 Start node 2 and join the cluster (separate terminal)
go run ./cmd/server --http-port 8081 --gossip-addr 127.0.0.1:7947 --join 127.0.0.1:7946 - 5 Check cluster membership
curl http://localhost:8080/members | jq . - 6 Check gossip stats
curl http://localhost:8080/stats | jq . - 7 Run benchmarks
go test -bench=. -benchmem -benchtime=3s ./...
[
{
"addr": "127.0.0.1:7946",
"status": "alive",
"lastSeen": "2026-05-08T10:00:01Z",
"incarnation": 0
},
{
"addr": "127.0.0.1:7947",
"status": "alive",
"lastSeen": "2026-05-08T10:00:01Z",
"incarnation": 0
}
] Benchmarks
M2 MacBook Pro, 16GB RAM — loopback UDP | Metric | Result | Notes |
|---|---|---|
| Dissemination time (10 nodes, fanout=3) | ~1.5 seconds to full coverage | estimated — 3 gossip rounds × 500ms |
| Failure detection time | 2-4 seconds | estimated — 1 probe interval + suspect timeout |
| False positive rate (10% packet loss) | ~2 % | estimated — indirect probe reduces from ~10% |
| Merge cost (10 nodes) | ~200 ns/op | merge hot path in v0/v1 receive |
| Merge cost (100 nodes) | ~830 ns/op | linear in member count |
| PickEvents cost (K=8 events) | ~65 ns/op | v2 hot path — runs on every Ping send |
| Network overhead (piggybacking vs full) | ~85 % reduction | estimated at N=10 |
Stress tests
What breaks at 100 nodes? The full-state gossip in v0/v1 sends 100-entry JSON objects on every gossip round. At 100 nodes × 3 fanout × 2 gossip rounds/second, that is 600 large UDP packets per second per node. UDP maximum transmission unit on loopback is 65535 bytes, so the packets themselves fit. The real problem is the merge cost: O(N) per received gossip message means the receive loop spends proportionally more time on merges than on actual protocol logic. v2 eliminates this by limiting each message to K=8 events.
At 1000 nodes? Full-state gossip messages overflow a single UDP packet. Each message would need to be fragmented. v2 sidesteps fragmentation entirely: K=8 events fit comfortably in one packet regardless of cluster size.
Kill a node mid-probe-cycle: stop a node with Ctrl+C while another node is waiting for its Ack. The waiting node times out at 200ms, sends PingReq to 2 helpers, those helpers also get no response, and after SuspectThreshold=3 failed probe cycles the node is marked Dead. The total detection time is about 3-4 seconds on a loopback test. On a real WAN with variable latency, set ProbeTimeout and SuspectThreshold based on your p99 round-trip time, not the mean.
Revive a Dead node: restart a stopped node at the same address. It starts with incarnation=0. Its old entry in the cluster has incarnation=0 and status=Dead. The Dead announcement spreads everywhere: Dead(incarnation=0) wins over anything with the same incarnation. The revived node needs to use a higher incarnation — in production, nodes persist their incarnation number to disk so they restart with a monotonically increasing value.
What the toy misses
- Phi Accrual failure detector SWIM uses a fixed probe timeout. Phi Accrual (used by Cassandra, Akka) uses inter-arrival statistics to compute a confidence score. It adapts to network conditions instead of requiring manual timeout tuning.
- Encryption and authentication Our gossip messages are plain UDP. A malicious node can inject false Dead announcements and partition the cluster. Consul encrypts gossip with symmetric keys.
- Network partition handling If the cluster splits into two halves, each half will declare the other half Dead. Recovering after a partition heals requires external reconciliation.
- Gossip compression For large clusters (1000+ nodes), even piggybacked events can be large. Production systems (HashiCorp memberlist) compress gossip messages with LZ4.
- Zombie nodes A node declared Dead can come back with the same address. Without correct incarnation number handling, it may be silently ignored. Our incarnation handling is simplified.
- Push-pull anti-entropy Gossip is eventually consistent but can diverge if all fanout peers happen to share the same knowledge gap. Push-pull (Cassandra anti-entropy) does a full state exchange periodically to correct persistent divergence.
Wiring it into a Java/Spring service
Wiring it into a Java/Spring service
org.springframework.boot:spring-boot-starter-web:3.3.0 org.springframework.boot:spring-boot-starter-actuator:3.3.0 labs/gossip/java-integration Clone and run: cd labs/gossip/java-integration && mvn spring-boot:run Why this integration matters
Spring Cloud Consul uses a gossip layer (HashiCorp memberlist, which is itself a SWIM implementation) underneath its DiscoveryClient. When your Spring Boot service calls discoveryClient.getInstances("my-service"), it is ultimately reading from a membership view maintained by gossip. This integration makes that connection explicit: the same HTTP API the Go gossip server exposes is the same shape as what Consul presents to Spring Cloud.
Part A — GossipClient: the HTTP bridge
The Go gossip server exposes four routes. GossipClient.java wraps them using Spring Framework 6.1’s RestClient.
public class GossipClient {
private final RestClient http;
public GossipClient(String baseUrl) {
this.http = RestClient.builder()
.baseUrl(baseUrl)
.defaultHeader("Accept", MediaType.APPLICATION_JSON_VALUE)
.build();
}
public List<Member> getMembers() {
var result = http.get()
.uri("/members")
.retrieve()
.body(new ParameterizedTypeReference<List<Member>>() {});
return result != null ? result : List.of();
}
public void joinCluster(String addr) {
http.post()
.uri("/join")
.contentType(MediaType.APPLICATION_JSON)
.body(Map.of("addr", addr))
.retrieve()
.toBodilessEntity();
}
public ClusterStats getStats() {
return http.get().uri("/stats").retrieve().body(ClusterStats.class);
}
// Java 17 records — immutable DTOs with no boilerplate
public record Member(String addr, String status, long lastSeen) {
public boolean isAlive() { return "alive".equals(status); }
public boolean isSuspect() { return "suspect".equals(status); }
public boolean isDead() { return "dead".equals(status); }
}
public record ClusterStats(int roundCount, int messagesSent, int memberCount) {}
}Part B — ClusterHealthService: Actuator integration
The service-client pattern is not just about making HTTP calls. The real value is surfacing cluster health through Spring’s standard HealthIndicator interface so it appears in /actuator/health alongside JVM health, disk health, and database health.
@Service
public class ClusterHealthService implements HealthIndicator {
private final GossipClient client;
private final GossipProperties props;
private volatile List<GossipClient.Member> lastSnapshot = List.of();
@Scheduled(fixedDelayString = "#{@gossipProperties.pollIntervalMillis()}")
public void refreshMembers() {
try {
lastSnapshot = client.getMembers();
} catch (Exception ignored) {
// Keep stale snapshot; health() will report DOWN.
}
}
public List<GossipClient.Member> getLiveMembers() {
return lastSnapshot.stream().filter(GossipClient.Member::isAlive).toList();
}
public List<GossipClient.Member> getSuspectMembers() {
return lastSnapshot.stream().filter(GossipClient.Member::isSuspect).toList();
}
@Override
public Health health() {
var members = lastSnapshot;
int memberCount = members.size();
if (memberCount == 0) {
return Health.down().withDetail("reason", "no cluster members known").build();
}
long liveCount = members.stream().filter(GossipClient.Member::isAlive).count();
double liveRatio = (double) liveCount / memberCount;
return (liveRatio > props.minLiveRatio() ? Health.up() : Health.down())
.withDetail("memberCount", memberCount)
.withDetail("liveCount", liveCount)
.withDetail("suspectCount", members.stream().filter(GossipClient.Member::isSuspect).count())
.withDetail("deadCount", members.stream().filter(GossipClient.Member::isDead).count())
.withDetail("liveRatio", String.format("%.0f%%", liveRatio * 100))
.build();
}
}The @Scheduled + volatile pattern is the right choice here. The poll result is written by one scheduler thread and read by many health-check threads. A volatile field gives visibility without locking — the snapshot is replaced atomically (reference assignment is atomic in Java). The cost of reading a stale snapshot is a slightly outdated health report, which is acceptable for a 30-second poll interval.
Comparing to Spring Cloud Consul: ConsulHealthIndicator does exactly this — polls the Consul HTTP API on a schedule and surfaces the result to Actuator. The minLiveRatio threshold maps to Consul’s quorum check. Our implementation makes the mechanism transparent.
Part C — GossipProperties: typed configuration
@ConfigurationProperties(prefix = "gossip")
public record GossipProperties(
String baseUrl,
Duration pollInterval,
double minLiveRatio
) {
public GossipProperties {
if (baseUrl == null) baseUrl = "http://localhost:8080";
if (pollInterval == null) pollInterval = Duration.ofSeconds(30);
if (minLiveRatio == 0.0) minLiveRatio = 0.5;
}
public long pollIntervalMillis() { return pollInterval.toMillis(); }
}application.yml
gossip:
base-url: http://localhost:8080
poll-interval: PT30S
min-live-ratio: 0.5
management:
endpoints:
web:
exposure:
include: health, info, metrics
endpoint:
health:
show-details: alwaysRun it
# Terminal 1: start the Go gossip server
cd labs/gossip
go run ./cmd/server --http-port 8080 --gossip-addr 127.0.0.1:7946
# Terminal 2: optionally start a second node
go run ./cmd/server --http-port 8081 --gossip-addr 127.0.0.1:7947 --join 127.0.0.1:7946
# Terminal 3: run the Spring Boot demo
cd labs/gossip/java-integration
mvn spring-boot:runExpected output:
=== SWIM Gossip Protocol — Spring Integration Demo ===
Cluster has 2 known members:
127.0.0.1:7946 status=alive
127.0.0.1:7947 status=alive
Gossip stats: rounds=12 messages=24 members=2
Live members (2):
127.0.0.1:7946
127.0.0.1:7947
Actuator health status: UP
memberCount = 2
liveCount = 2
suspectCount = 0
deadCount = 0
liveRatio = 100%
Demo complete. Actuator available at /actuator/healthRun the tests
cd labs/gossip/java-integration
mvn testThe five unit tests cover: all-alive returns UP, majority-dead returns DOWN, suspect members not counted as alive, zero members returns DOWN, health detail contains all count fields. Tests use Mockito to stub the GossipClient — no live Go server required.
What surprised me
Where to go deeper
- The original SWIM paper: Das, Gupta, Motivala (2002) — dense but short, and the convergence proof is readable even without a distributed systems background
- HashiCorp memberlist: the production SWIM implementation used by Consul, Nomad, and Serf — github.com/hashicorp/memberlist
- Cassandra gossip internals: how Cassandra uses gossip for ring topology and token ranges — cassandra.apache.org/gossip
- Phi Accrual Failure Detector: the Akka documentation explains the math cleanly — doc.akka.io/failure-detector
- Serf by HashiCorp: a gossip-based cluster membership and orchestration tool — if you want to see SWIM in production Go code, Serf is the clearest example
Related labs and questions
- Next in distributed: Raft From Scratch — once you know which nodes are alive (gossip), you need a way to elect a leader among them (Raft)
- Prerequisite for: Consistent Hashing — Cassandra uses gossip to propagate the token ring; without failure detection the ring cannot be rebalanced
- System design companion: designing a service discovery system — gossip vs. Zookeeper vs. etcd, when each is appropriate
- Follow-up question: why does Kubernetes use etcd (consensus) for service discovery instead of gossip? What property does etcd provide that gossip cannot?