How does Cassandra's ring architecture and consistent hashing work?
Learn how Cassandra's masterless ring architecture and consistent hashing distribute data with tokens and virtual nodes for even, scalable storage.
Expected Interview Answer
Cassandra arranges all nodes in a logical ring where each node owns a range of a token space, and consistent hashing maps every row's partition key to a token that decides which node stores it.
When a write arrives, Cassandra hashes the partition key (using the Murmur3 partitioner by default) into a 64-bit token and walks the ring clockwise to the node owning that token range, which becomes the coordinator's target. Because there is no master, any node can act as coordinator. Virtual nodes (vnodes) split each physical node into many small token ranges, so adding or removing a node only reshuffles a fraction of the data and keeps the cluster evenly balanced.
- No single point of failure (masterless peer-to-peer ring)
- Even data distribution across nodes
- Minimal data movement when scaling up or down
- Any node can serve as coordinator
- Horizontal, near-linear scalability
AI Mentor Explanation
Picture the boundary rope of a cricket ground divided into fielding zones, each zone assigned to one fielder. When a ball is struck, its landing angle decides which zone it enters and therefore which fielder chases it, with no captain relaying every ball. Consistent hashing is that angle-to-zone rule: the partition key decides the token, the token decides the owning node, and adding a substitute fielder only reshapes the zones nearest to them.
Step-by-Step Explanation
Step 1
Hash the partition key
Cassandra applies the partitioner (Murmur3 by default) to the partition key, producing a 64-bit token.
Step 2
Map token to ring position
The token is a point on a circular token space that spans from -2^63 to 2^63-1.
Step 3
Find the owning node
Walking the ring clockwise, the first node whose token range covers that value owns the primary replica.
Step 4
Use virtual nodes
Each physical node holds many small token ranges (vnodes), so ownership is spread in tiny slices for balance.
Step 5
Rebalance on topology change
Adding or removing a node only reassigns the token ranges near it, moving a fraction of the data.
What Interviewer Expects
- Understanding that Cassandra is masterless and peer-to-peer
- Knowledge of tokens, token ranges, and the partitioner
- How consistent hashing minimizes data movement on scaling
- The role of virtual nodes (vnodes) in balancing
- How the coordinator locates the owning node
Common Mistakes
- Claiming Cassandra has a master node that routes writes
- Confusing the partition key with the clustering key when hashing
- Saying adding a node reshuffles all data instead of a fraction
- Ignoring virtual nodes and assuming one range per node
- Believing consistent hashing is just a modulo of node count
Best Answer (HR Friendly)
“Cassandra lines up all its servers in a circle and uses a math formula on each record's key to decide which server stores it. Because the servers share the work as equals with no boss, the system stays online if one fails, and adding a new server only moves a small slice of the data.”
Code Example
CREATE TABLE users_by_country (
country text,
user_id uuid,
name text,
PRIMARY KEY (country, user_id)
);
-- 'country' is the partition key: its hash (token) decides
-- which node owns the row. Inspect token distribution:
SELECT token(country), country FROM users_by_country;Follow-up Questions
- What is a virtual node (vnode) and why does Cassandra use many per host?
- How does the Murmur3Partitioner differ from the older RandomPartitioner?
- What happens to token ranges when a node is decommissioned?
- How does the coordinator node forward a request to replicas?
- Why does consistent hashing scale better than modulo-based sharding?
MCQ Practice
1. What determines which node stores a given row in Cassandra?
The partitioner hashes the partition key into a token, and the node owning that token range stores the primary replica.
2. What is the main advantage of virtual nodes (vnodes)?
Vnodes split each physical host into many small token ranges, spreading data and easing rebalancing when topology changes.
3. Why does adding a node in Cassandra move only a fraction of data?
Consistent hashing means a new node takes over token ranges near its position, so only that neighboring slice of data relocates.
Flash Cards
What is a token in Cassandra? — A 64-bit value produced by hashing the partition key; it positions the row on the ring.
What is the default partitioner? — Murmur3Partitioner, which distributes tokens uniformly across the ring.
Is Cassandra master-based? — No — it is masterless and peer-to-peer; any node can coordinate a request.
What are vnodes? — Virtual nodes: many small token ranges per physical host for even data distribution and easier rebalancing.