Bringing together the Apache Cassandra experts from the community and DataStax.

Want to learn? Have a question? Want to share your expertise? You are in the right place!

Not sure where to begin? Getting Started

 

question

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.

partitioner
10 |1000 characters needed characters left characters exceeded

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                         
 10.101.33.146 rack1  Up     Normal 174.98 KiB  33.33%  -9223372036854775808                        
 10.101.33.196 rack1  Up     Normal 164.12 KiB  33.33%  -3074457345618258603                        
 10.101.36.82  rack1  Up     Normal 200.86 KiB  33.33%  3074457345618258602

From this example, here are the range of tokens owned by each node:

  10.101.36.82 : -3074457345618258602 to 3074457345618258602
 10.101.33.146 :  3074457345618258603 to -9223372036854775808
 10.101.33.196 : -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 10.101.33.196 since the token for "Ramirez" (-4404190778401486753) is in the range owned by node 10.101.33.196 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 10.101.33.196. Similarly, when a coordinator gets a read request for the partition where pk = 'Ramirez', the data will be retrieved from node 10.101.33.196.

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

Share
10 |1000 characters needed characters left characters exceeded

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