Scrivio Interview library
InternalsFoundations7 min read · updated 13 November 2026

In Dynamo-style stores such as Cassandra, each node owns several tokens, called virtual nodes, on a hash ring, and every key is stored on several replicas chosen by walking the ring from the key's position.

How are the replicas for a key chosen when nodes own many virtual nodes, why must the walk skip tokens of the same physical node and rack, and why do more tokens per node give a more even spread?

The key is hashed to a token on the ring, the first replica is the node that owns the next token clockwise, and the walk continues clockwise, skipping tokens of nodes already chosen (and, with topology-aware placement, racks already used) until it has N distinct replicas. Skipping matters because with virtual nodes consecutive tokens often belong to the same machine. More tokens per node make ownership more even because each node's share is the sum of many small ranges, so the variation shrinks roughly with the square root of the token count, though recent versions use fewer tokens chosen by an allocation algorithm.

Why this question is asked

Consistent hashing is the textbook answer to how distributed stores spread data, but the textbook picture of one point per node on a circle hides the details that matter in practice. Cassandra and other Dynamo-style stores give each node many tokens and place replicas by walking the ring, and both the walk and the token count have consequences for safety and balance. Interviewers ask about these internals to see whether we understand why the defaults are what they are. In this answer, we will recap the ring and tokens, walk the ring to choose replicas, see why the walk must skip nodes and racks, and explain why more tokens balance better and what that costs.

1A Quick Recap: The Token Ring

Picture the full range of a hash function, for Cassandra's Murmur3 partitioner the 64-bit values from about -9.2 x 10^18 to 9.2 x 10^18, bent into a circle. Each node owns one or more points on that circle, called tokens. A node with several tokens is said to have virtual nodes, or vnodes: each token is a slice of the ring the node is responsible for. A row's partition key is hashed to a position on the ring, and the node owning the first token at or after that position, moving clockwise, is the primary owner of that row.

The replication factor, N or RF, says how many copies of each row exist. With RF 3, every row lives on three different nodes.

2Walking the Ring to Choose Replicas

To find the replicas for a key, we start at the key's position and walk clockwise. The owner of the first token we meet is replica 1. We keep walking and take the owner of each next token as the next replica, until we have N of them. With one token per node this is simple: the next three tokens belong to three different nodes.

With virtual nodes, it is not. Each node owns many tokens scattered randomly around the ring, so it is common for two consecutive tokens to belong to the same machine. If we simply took the next three tokens, two of the three replicas could land on one machine, and losing that machine would lose two copies of the data. So the walk skips any token whose node has already been chosen and continues until it has N distinct physical nodes.

Walking the ring for a key that hashes just before token t1 (RF 3)
Token (clockwise)OwnerRackDecision
t1node Arack 1replica 1
t2node Arack 1skip: node A already chosen
t3node Crack 1skip: rack 1 already used (topology-aware)
t4node Brack 2replica 2
t5node Drack 3replica 3
SimpleStrategy would stop at node C; NetworkTopologyStrategy spreads across racks.

3Why the Walk Also Skips Racks

Distinct machines are not enough when machines fail together. Nodes in the same rack share a power supply and a top-of-rack switch; nodes in the same cloud availability zone share a data centre. If all three replicas were in one rack, a single switch failure would make the row unavailable.

Cassandra's NetworkTopologyStrategy is topology aware. Each node is labelled with a data center and a rack (in the cloud, racks usually map to availability zones). For each data center, the replication factor is set separately, and the walk prefers nodes in racks that have not yet received a replica, only reusing a rack when every rack already has one. With RF 3 across three racks, each rack holds exactly one copy of every row, so losing a whole rack leaves two copies, still a majority. That is what makes QUORUM reads and writes (2 of 3) keep working during a rack or zone outage. SimpleStrategy, which ignores topology, is meant only for single-rack test clusters.

Java
CREATE KEYSPACE shop
WITH replication = {'class': 'NetworkTopologyStrategy', 'dc1': 3};
-- with nodes spread over 3 racks (zones) in dc1: one replica of each row per rack

4Why More Tokens Give a More Even Spread

With one random token per node, the gaps between neighbouring tokens vary a lot. Some nodes end up owning a long arc of the ring and others a short one; with a handful of nodes, one can easily own two or three times as much as another. Each node's share is a single random gap, and single random gaps are very uneven.

With many tokens per node, each node's share is the sum of many small gaps. Sums of many random pieces are much more predictable than any single piece, so every node's total ends up close to the average. Statistically, the relative spread of ownership shrinks roughly with the square root of the number of tokens: going from 1 to 256 random tokens per node reduces the typical imbalance by a factor of about 16. That is why older Cassandra versions defaulted to num_tokens 256.

Typical ownership spread with random tokens (illustrative)
Tokens per nodeTypical deviation from average shareComment
1very large, owners can differ several times overclassic consistent hashing
4still largetoo few for random placement
16 randommoderateneeds the allocation algorithm
16 allocatedsmalldefault in Cassandra 4.x configs
256 randomsmallold default, heavy on repair

5The Costs of Many Tokens and the Modern Default

Many tokens have a second benefit: when a node fails and is rebuilt, its data is spread over hundreds of small ranges, whose other replicas are on many different nodes. So the rebuild streams from many peers in parallel instead of hammering two neighbours. But they also cost. Each token is a range that repair must process separately, so 256 tokens per node means many small repair sessions, more metadata in gossip, and slower operations such as range scans across the cluster. And with many vnodes per node, a cluster has more distinct ways to lose two nodes that share a range, which affects availability with low replication factors.

Recent Cassandra versions resolve this by choosing tokens instead of drawing them at random. The token allocation algorithm picks each new node's tokens to balance ownership for a given replication factor, so 16 tokens give a spread as even as hundreds of random ones. The default cassandra.yaml in 4.x sets num_tokens to 16 with allocate_tokens_for_local_replication_factor set to 3; check your version's defaults and remember the allocator only affects nodes as they join.

6Follow-up questions

What does replication factor 3 across three racks guarantee when one rack is lost?

With NetworkTopologyStrategy and nodes spread over three racks, each rack holds exactly one replica of every row. Losing a rack removes one replica of every row, leaving two, so QUORUM reads and writes, which need 2 of 3, keep working and no data is lost. Losing a second rack before recovery would leave one replica: data is still there, but QUORUM fails and only ONE-level operations succeed. The guarantee also assumes racks have similar numbers of nodes; uneven racks make some nodes own much more.

What happens if racks have different numbers of nodes?

Because each rack must hold one replica of every row, a rack with fewer nodes spreads a full copy of the data over fewer machines, so its nodes hold more data and take more load. That is why Cassandra deployments keep the same number of nodes in each rack.

Why not just use hash(key) mod N instead of a ring?

Because changing N moves almost every key. On a ring, adding a node only takes over the ranges next to its new tokens, so only a fraction of about 1/(nodes) of the data moves.

7Conclusion

In this answer, we saw how Dynamo-style stores choose replicas: hash the key to a token, take the owner of the next token clockwise, and keep walking until there are N distinct nodes, skipping tokens of nodes already chosen because with vnodes consecutive tokens often belong to one machine. NetworkTopologyStrategy also spreads replicas across racks or zones, so losing one keeps a quorum. More tokens per node even out ownership because each share is a sum of many small ranges, with variation shrinking roughly with the square root of the token count, and they spread rebuilds, but they cost repair work and metadata, which is why modern Cassandra uses 16 tokens chosen by an allocation algorithm.

What to say in the interviewAbout 90 seconds aloud

Open with

The key's partition key is hashed to a position on the ring. The first replica is the node that owns the next token clockwise, and we keep walking clockwise collecting nodes until we have the replication factor, say three.

Then, if they want more

With virtual nodes each machine owns many tokens spread randomly around the ring, so the next token very often belongs to a machine we already picked. If we did not skip those, two replicas could sit on one machine, and one failure would lose two copies.

Cassandra's NetworkTopologyStrategy goes further and also skips racks that already have a replica, so with RF 3 over three racks or zones, each rack has one copy of every row and losing a whole rack still leaves a quorum of two.

As for balance: with one random token per node, ownership is a single random gap and can differ several times between nodes. With many tokens, each node's share is the sum of many small gaps, so it averages out; the variation shrinks roughly with the square root of the token count. Many tokens also spread a rebuild across many peers.

But they make repair and metadata heavier, so Cassandra moved from 256 random tokens to 16 tokens with an allocation algorithm that chooses them to balance ownership for the replication factor.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How are the replicas for a key chosen when nodes own many virtual nodes, why must the...": https://scrivio-interview-library.netlify.app/architecture/partitioning-and-replication/626bd93b9c/