Changing the Replication Factor in Cassandra

In this article, we are going to discuss how we can change the replication factor in both simple and network topology replication strategy. For better understanding please refer Replication Strategy in Cassandra article.

Altering a Keyspace:
To change the replication factor you can execute the Altering a keyspace statement where you can change the replication factor for Simple Strategy and NetworkTopology Strategy.

For example:
Changing the Replication Factor for SimpleStrategy:
If you want to change the replication factor of a keyspace, you can do it by executing the ALTER KEYSPACE command, which has the following syntax:

Syntax:
 
ALTER KEYSPACE "KeySpace Name"
WITH replication = {'class': 'Strategy name', 
                    'replication_factor' : 'No.Of replicas'}; 

first, you can create any keyspace and then you can change the replication factor or if you have existing keyspace then you can change in the same way.

Example: Creating a WFH keyspace.



CREATE KEYSPACE WFH WITH replication =
           {
            'class': 'SimpleStrategy',
           'replication_factor': '2'
               }
            AND durable_writes = true;

Now, here you can change the replication factor for the same.

cassandra@cqlsh> ALTER KEYSPACE WFH
   ...            WITH replication =
   ...            {
   ...             'class': 'SimpleStrategy',
   ...            'replication_factor': '3'
   ...                }
   ...             AND durable_writes = true;

Now, to verify the result you can execute the following CQL query.

cassandra@cqlsh> describe WFH;

Output:

CREATE KEYSPACE wfh 
WITH 
replication = 
{
'class': 'SimpleStrategy', 
'replication_factor': '3'
}  
AND durable_writes = true;

In Cassandra, You set the replication strategy at the keyspace level when creating the keyspace or later by modifying the keyspace.

Changing the Replication Factor for NetworkTopologyStrategy:
In this case, you can consider an existing keyspace that you want to change the Replication Factor for NetworkTopologyStrategy.

Example: Existing keyspace : app_data
you can see the description of app_data keyspace by executing the following CQL query.

cassandra@cqlsh> describe app_data;

Output:

CREATE KEYSPACE app_data 
WITH replication = 
{'class': 'NetworkTopologyStrategy', 
'datacenter1': '3', 
'datacenter2': '2'}  
AND durable_writes = true;

Now, if we want to change the replication factor for datacenter2 from 2 to 3 then you can execute the following CQL query given below.

cassandra@cqlsh> ALTER KEYSPACE app_data
   ...        WITH replication =
   ...        {
   ...         'class': 'NetworkTopologyStrategy',
   ...         'datacenter1': '3',
   ...         'datacenter2': '3'
   ...        }
   ...          AND durable_writes = true;

Now, to verify the result then you can execute the following CQL query.

cassandra@cqlsh> describe app_data;

Output:

CREATE KEYSPACE app_data 
WITH replication = 
{
'class': 'NetworkTopologyStrategy', 
'datacenter1': '3', 
'datacenter2': '3'
}  
AND durable_writes = true;
    Note:

  • In Cassandra, You can’t alter the name of a keyspace.
  • It is always a good practice after changing the replication factor or any modification you can execute the repair command.
  • You can execute the following CQL query for full repair.
    nodetool repair -full
    

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.