singhavnishsingh avatar image
singhavnishsingh asked Erick Ramirez answered

How is data stored if the number of partitions is greater than number of nodes?

Say I have 2 nodes and a table that has 5 partitions how would that be distributed across nodes... If the number of nodes is = number of partitions then that's understandable but what if the number of partitions> number of nodes.

10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

1 Answer

Erick Ramirez avatar image
Erick Ramirez answered

The number of partitions doesn't have any relationship with the number of nodes in a cluster.

Each node in the cluster is responsible for a range of tokens that is a subset of data in the "ring" of tokens. In the case of virtual nodes (num_tokens), each node owns multiple token ranges.

Cassandra's partitioner uses a hashing algorithm (the default Murmur3Partitioner uses the MurmurHash3 function) to convert the partition keys to a token value. Each partition in a table gets distributed around the ring of nodes in each DC based on its equivalent token value derived by the partitioner.

To illustrate how Cassandra determines where partitions are stored, let me use this example 3-node cluster:

$ nodetool ring community
 Datacenter: Cassandra
 Address       Rack   Status State  Load        Owns    Token                                       
                                                        3074457345618258602                  rack1  Up     Normal 174.98 KiB  33.33%  -9223372036854775808                 rack1  Up     Normal 164.12 KiB  33.33%  -3074457345618258603                  rack1  Up     Normal 200.86 KiB  33.33%  3074457345618258602

From this example, here are the range of tokens owned by each node: : -3074457345618258602 to 3074457345618258602 :  3074457345618258603 to -9223372036854775808 : -9223372036854775807 to -3074457345618258603

As you can see from the table above, each node owns a large number of tokens. When you add or remove nodes from the cluster, the range of tokens owned by each node will change.

Using an example partition key pk = 'Ramirez', the Murmur3 hash function returns the token value as:

    Murmur3Partitioner('Ramirez') = -4404190778401486753

This means that the partition with pk = 'Ramirez' belongs to node since the token for "Ramirez" (-4404190778401486753) is in the range owned by node in the table above.

When a coordinator gets a write request for a partition where pk = 'Ramirez', the coordinator will forward the write to node Similarly, when a coordinator gets a read request for the partition where pk = 'Ramirez', the data will be retrieved from node

For more information, see Data distribution and replication. Cheers!

10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.