SDSystem DesignConcepts to case studies
System DesignDSA
x
0/311
Part 1: Foundations
Part 2: APIs and Communication
Part 3: Caching
Part 4: Data and Storage
Part 5: Architecture
Part 6: Distributed Systems
  • Consensus Algorithms
  • Paxos
  • Raft
  • Leader Election
  • ZooKeeper and etcd
  • Distributed Locks
  • Fencing Tokens
  • Gossip Protocol
  • CRDTs
  • Operational Transformation
  • Distributed Unique ID Generation (Snowflake, UUID)
  • Two Generals Problem
  • Byzantine Fault Tolerance (BFT)
Part 7: Advanced Data Topics
Part 8: Operations and Security
Part 9: Estimation and Interview Method
Part 10: Case Studies
System Design/Distributed Systems/Coordination and Consensus

Gossip Protocol

Spread news like a rumour: each machine tells a few random others.

⏱ 12 min readIntermediateInterview FavouriteHas visualizer
SummaryConceptReal-Life ExampleWhy?Where?How It WorksStepsKey TermsPros & ConsMistakesReal WorldRead NextInterview Q&ACheat-Sheet

01Quick Summary

A gossip protocol spreads information through a group of machines the way a rumour spreads through an office: each machine regularly tells a few randomly chosen others what it knows, and they do the same.

  • There is no leader and no central list. Every machine runs the same simple rule, over and over.
  • News reaches every machine in about log N rounds, even with thousands of machines, and no machine is flooded with messages.
  • It keeps working when machines crash or messages are lost, because there are many paths to everyone.

02Concept: What is it?

Imagine a cluster of 1,000 machines. One of them learns something: a new node joined, a node died, or a setting changed. How do all the others find out? The simple way is for that one machine to message all 999 others. But that machine becomes a bottleneck, and if it crashes halfway, only some machines know.

Gossip takes a different path. Every round (say once a second), each machine picks a few random peers and shares what it knows. A machine that just heard the news does the same in the next round. The group that knows roughly doubles each round: 1, 2, 4, 8, 16... After about logβ‚‚(N) rounds, almost everyone knows. For 1,000 machines that is only about 10 rounds.

The same idea is called an epidemic protocol, because news spreads like a disease: the more machines are infected, the faster it spreads, until nearly everyone has it.

One rumour, three rounds
🟑Round 01 machine knows
→↓tell 2
πŸ”΅Round 1about 3 know
→↓each tells 2
πŸ”΅Round 2about 9 know
→↓and so on
🟒Round ~log Nalmost all know
Each informed machine tells a few random others. The informed group grows very fast.
🧠Fast, but not instant
Gossip gives eventual results. For a short time different machines know different things. It is a good fit when "everyone will know very soon" is enough.

03Real-Life Example

🍡News in a Big Office
A manager does not walk to 500 desks to say "the party is moved to Friday." She tells two colleagues. Each of them tells two more at the coffee machine. Within a few hours everybody knows, even if a few people are on holiday or miss a chat. Nobody had to keep a list of who knows what, and no single person did all the talking. If someone hears the same news a second time they just nod and say nothing new.
The office
Gossip protocol
A person with news
A machine with new state
Telling two random colleagues
Sending to a few random peers (the fan-out)
A coffee break
One gossip round
Hearing news you already know
A duplicate message, which is ignored
Someone on holiday
A crashed machine or a lost message
Everyone knows by the afternoon
Convergence in about log N rounds
No one is in charge of the rumour
No leader, no single point of failure
Who talks to whom (random every round)
πŸ–₯️Aknows
→↓tells
πŸ–₯️Dlearns
→↓tells
πŸ–₯️Flearns
→↓tells
πŸ–₯️Blearns

04Why Do We Need It?

πŸ“ˆScales very well
Each machine sends only a few messages per round, no matter how big the cluster is. 10,000 machines is not much harder than 100.
πŸ›‘οΈNo single point of failure
No leader to crash. Any machine can start a rumour and any machine can carry it.
🧱Tough against failures
Lost messages and crashed machines slow the spread a little, but many other paths still reach everyone.
🧩Very simple
One short rule on each machine. No complex agreement or locking is needed.
βš–οΈBalanced load
The work is shared evenly, so no machine becomes the hot spot that tells everyone.
🌍Works over bad networks
It copes with delays, partial partitions and machines joining or leaving all the time.
When you cannot afford a central boss and only need everyone to know soon, let the machines tell each other like rumours.

05Where Do We Use It?

πŸ‘₯Cluster membership
Who is in the cluster, and who is alive? Machines gossip lists of members and their latest heartbeat counters.
🩺Failure detection
If a machine's heartbeat counter stops growing in everyone's gossip, it is marked as suspect and then dead.
πŸ—‚οΈSpreading metadata
Data placement, schema versions, node load and settings are spread with gossip (Cassandra does this).
πŸ”„Anti-entropy repair
Two replicas compare their data and fix any differences they find. This keeps copies close over time.
πŸ“ŠAggregation
Machines can gossip to compute a total or an average without a central collector.
⛓️Peer networks
Blockchains and P2P networks spread blocks, transactions and peer lists in a gossip style.

06Types / Parts / How It Works

Three ways to share news

Push, pull and push-pull
Push
πŸ–₯️Knows
→↓here is the news
πŸ–₯️Random peer
Pull
πŸ–₯️Asks
←anything new?
πŸ–₯️Random peer
Push-pull
πŸ–₯️Peer A
↔swap what is new
πŸ–₯️Peer B
Who sends the data in a gossip round?
StyleHow it worksGood and bad
PushMachines that know the news send it to random peersFast at the start, when few know. Wasteful at the end, as most pushes hit machines that already know.
PullMachines ask random peers if they have anything newSlow at the start, fast at the end. Good for finishing off the last few machines.
Push-pullTwo peers swap and merge what each knowsFastest overall. Used by most real systems, at the cost of bigger messages.

Knobs you can turn

SettingWhat it meansWhy it matters
Fan-outHow many peers each machine tells per roundMore means faster spread, but more traffic and more duplicates.
Gossip intervalTime between rounds (often about 1 second)Shorter means faster news but more network use.
Message contentOnly the latest changes, or the whole stateSmall digests save bandwidth. Full state is simpler and more robust.
Stop ruleWhen to stop spreading an old rumourWithout it, old news circulates forever and wastes bandwidth.
VisualizerSpread a rumour through a clusterPress Play and watch the S-shaped curve. Change the number of machines (it grows slowly), the fan-out, and the message loss. Try another random draw to see that the result is always close.
4%
know the news
Machines that know, after each round (the curve is an S shape)
Round
0
so far
Informed
1/24
Messages sent
0
all arrived
Busiest machine
2/round
vs 23 if one tells all
Only machine 0 knows the news (the amber ring). Press Play, or Step, and watch each informed machine tell a few random others.

A simplified push-gossip model: in each round every informed machine picks 2 random other machines and sends the news. The picks come from a fixed seeded sequence, so the same settings always replay the same story. Real systems also pull and exchange state, and they check for failures.

07How to Use It / Steps / Implementation

Building a simple gossip layer

  1. 1
    Keep versioned state
    Each piece of news carries a version (a counter or timestamp), so machines can tell new from old and keep the newer one.
  2. 2
    Start a timer
    Every interval (for example 1 second), each machine wakes up to gossip.
  3. 3
    Pick random peers
    Choose a small number of peers at random from the member list. Random choice is what gives the fast, even spread.
  4. 4
    Exchange digests
    Send a short summary (names and versions). The peer answers with the items it is missing, and asks for the ones you have that are newer.
  5. 5
    Merge
    Keep the newer version of each item. The merge must be safe to repeat, so duplicates do no harm.
  6. 6
    Detect failures
    Each machine bumps its own heartbeat counter and gossips it. If a machine's counter has not grown for a while, mark it suspect, and later dead.
  7. 7
    Stop old news
    Let items age out, or stop spreading a rumour after enough peers say they already know it.
πŸ’‘Seed nodes
A new machine needs to find someone to talk to first. Give it a few well-known seed addresses. After the first gossip it learns about the rest.
⚠️Not for everything
Do not use gossip when you need a single exact answer at one moment, such as a bank transfer or choosing a leader. Use a consensus algorithm for that.

08Key Terms to Know

TermMeaning in easy words
Gossip / epidemic protocolSpreading information by random peer-to-peer exchanges, like a rumour or an infection
RoundOne cycle in which every machine gossips once
Fan-outHow many peers a machine contacts in each round
Push, pull, push-pullWhether the news is sent, asked for, or swapped both ways
ConvergenceThe point when all machines hold the same information
Anti-entropyComparing and repairing differences between replicas, so they drift back together
Rumour mongeringSpreading a fresh update until most peers already know it, then stopping
DigestA short summary of what a machine knows, used to find what is missing
Membership listThe list of machines a node thinks are in the cluster
Seed nodeA well-known machine that new nodes contact first to join the gossip
SWIMA popular gossip-style protocol for membership and failure detection

09Pros, Cons and Trade-offs

βœ… Pros
  • Scales to thousands of machines
  • No leader and no single point of failure
  • Survives crashes and lost messages
  • Simple to build and reason about
  • Spreads load evenly across machines
⚠️ Cons
  • Only eventual: machines can disagree for a moment
  • Hard to say exactly when everyone has the news
  • Duplicate messages waste some bandwidth
  • Hard to debug, since paths are random
  • Not suitable for strict ordering or exact agreement

Trade-offs

ChoiceWe gainWe lose
Bigger fan-outFaster spread, more robustMore traffic and more duplicate messages
Shorter intervalNews arrives sooner, failures found soonerMore network and CPU use, more false alarms
Send full stateSimple and self-healingLarge messages as the data grows
Send only digestsSmall messagesExtra round trips to fetch the missing parts
Gossip instead of a central listNo single point of failure, scales outViews are slightly stale and only eventually agree
Aggressive failure detectionDead machines are removed quicklyHealthy but slow machines may be wrongly declared dead

10Common Problems and Mistakes

Common mistakes

  1. Using it when you need exact agreement. Gossip tells you everyone will know soon, not that everyone knows now.
  2. Picking peers in a fixed pattern. Without true randomness, news can get stuck in one corner of the cluster.
  3. No stop rule. Old rumours keep circulating and fill the network.
  4. Sending too much each round. Full state of a huge dataset every second can overload the links.
  5. Ignoring false failure alarms. A slow machine or a busy network can look like a dead one.
  6. No versioning. Without versions, an old rumour can overwrite fresh state.

Things to watch

🧱Network partition
Each side keeps gossiping inside itself. The two sides catch up after the network heals.
πŸ””False positives
Healthy nodes marked as dead. Use suspicion states and wait for several confirmations.
🌊Duplicate storms
A big fan-out in a big cluster can create a lot of repeated messages.
⏱️Slow convergence
Tiny fan-out or heavy loss means a long wait for the last machines.

11Real-World Examples

πŸ‘οΈCassandra
Nodes gossip every second about who is alive and what the cluster looks like, with no master node.
πŸ›οΈAmazon Dynamo / DynamoDB lineage
The Dynamo paper used gossip for membership and failure detection across storage nodes.
πŸ”Consul and Serf
HashiCorp's Serf, used by Consul, runs a SWIM-based gossip to track members and detect failures.
πŸ”΄Redis Cluster
Cluster nodes use a gossip bus to share slot ownership and failure reports.
β‚ΏBitcoin and Ethereum
Transactions and blocks spread across peers by relaying them to neighbours.
☁️Cloud storage systems
Large storage services spread health and load information with gossip-style exchanges.
πŸ“Note
Details such as the interval, fan-out and exact messages differ by product and version. Check the project's own docs.
Mini case: a node dies in a 6-node cluster
πŸ–₯️B noticesno reply from E
→↓gossip
πŸ–₯️A, C, Dmark E as suspect
→↓gossip
πŸ–₯️EveryoneE is dead
First to notice Suspicion spreads Agreed: dead
Nobody is in charge, yet everyone soon agrees that node E is gone.

12Related Topics to Read Next

Heartbeats→

Gossip often carries heartbeat counters to find dead machines.

Network Partitions→

Gossip keeps working on each side of a split, then merges.

Consistency Models→

Gossip gives eventual consistency, one of the weaker models.

CRDTs→

Data whose merges are safe to repeat fits gossip very well.

Vector Clocks→

Version numbers tell machines which gossip is newer.

Consensus Algorithms→

Use these when you need exact agreement, not just soon.

13Interview Questions with Short Answers

14Quick Revision Cheat-Sheet

  • βœ”What: each machine tells a few random peers what it knows, every round.
  • βœ”Speed: the informed group doubles, so all know in about logβ‚‚ N rounds.
  • βœ”Load: every machine sends only fan-out messages per round, so it scales to thousands.
  • βœ”Styles: push (fast early), pull (fast late), push-pull (best).
  • βœ”Failures: no leader to lose, and lost messages only slow it down.
  • βœ”Uses: membership, failure detection, metadata, anti-entropy repair (Cassandra, Consul, Redis Cluster).
  • βœ”Limit: eventual only. For exact agreement use consensus (Raft, Paxos).
  • βœ”One line: β€œTell a few random friends, and let them tell theirs.”
← Previous
Fencing Tokens
Next β†’
CRDTs