Changing the Replication Factor in Cassandra - GeeksforGeeks

28 Apr, 2020
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_datayou 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;

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

nodetool repair -full

NoSQL
DBMS
Write From Home
DBMS
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
SQL Interview Questions
Introduction of B-Tree
SQL Trigger | Student Database
CTE in SQL
Difference between Clustered and Non-clustered index
Convert integer to string in Python
Convert string to integer in Python
Python infinity
How to set input type date in dd-mm-yyyy format using HTML ?
Matplotlib.pyplot.title() in Python