Open In App

Changing the Replication Factor in Cassandra

Last Updated : 28 Apr, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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
    

Similar Reads

Replication strategy in Cassandra
In this article, we will discuss Different strategy class options supported by Cassandra such that SimpleStrategy, LocalStrategy, NetworkTopologyStrategy are three Replication strategy in which we generally used Simple and NetworkTopology Strategy in which LocalStrategy is used for system only. let's discuss one by one. Different types of Replicati
3 min read
Difference between Mirroring and Replication
Mirroring: Mirroring is also known as Shadowing, is the process of creating multiple copies of data and database. Generally in mirroring, the database is copied on totally different machine or location from its main database. There is if any primary server smashes for the maintenance then at that time, the system can failure to the mirrored databas
2 min read
Single-Master and Multi-Master Replication in DBMS
Prerequisite - Data Replication Data replication is simply process of copying data from one database server to another database server so that all users can share same data without any inconsistency. Before understanding various models of replicating data it is very important to know why we need to replicate data. Example - Assume that you have Rs.
5 min read
Data Replication in DBMS
Data Replication is the process of storing data in more than one site or node. It is useful in improving the availability of data. It is simply copying data from a database from one server to another server so that all the users can share the same data without any inconsistency. The result is a distributed database in which users can access data re
7 min read
Static type using batch in Cassandra
Prerequisite - Cassandra In this article, we will discuss the static column using the BATCH statement and how it works with the BATCH statement. Static column can be used for conditional update using a batch statement. Let’s have a look. Let's consider bank_emp_details is a table name and Id, Name, Bank_name, E_start_date and E_end_date are the fie
2 min read
High Availability Mechanism in Cassandra
In this article we will learn about mechanism of high availability in Cassandra by using the following key terminology. 1. Seed Node 2. Gossip Communication Protocol 3. Failure Detection 4. Hinted Handoff lets discuss one by one. 1. Seed Node: In Apache Cassandra it is first nodes to start in cluster. If we want to Configured seed node, then we can
5 min read
Apache Cassandra tools
Prerequisites - Introduction to Apache Cassandra Apache Cassandra (NOSQL database) Architecture of Apache Cassandra In this article, we are going to discuss the tools of Apache Cassandra which help to perform in various aspects of tasks such that the status of the node, the status of the ring, back up and restore data, etc. The CQL shell (cqlsh) -
3 min read
Blob conversion function in Cassandra
In Cassandra, Blob data type represents constant hexadecimal data type and the number is defined as 0[xX](hex)+ where hex means hexadecimal. It is useful to store a short string or small images. In this article we will discuss Blob conversion function which converts the native types into binary data (blob): typeAsBlob(value) blobAsType(value) Let's
1 min read
Designing models in Cassandra
Prerequisite - Introduction to Cassandra, Overview of Data Modelling In this article, we are going to discuss how we can Designing the model in Cassandra. Modelling design is one of the key part for any application. So, let's discuss how we can make a better model for any application with an example we will see how we can do that. As you will see C
4 min read
Apache Cassandra (NOSQL database)
In this article, we will learn the basics of Apache Cassandra and the basics of CQL (Cassandra Query Language) operations like Create, insert, delete, select, etc. Apache Cassandra: Apache Cassandra is an open-source no SQL database that is used for handling big data. Apache Cassandra has the capability to handle structure, semi-structured, and uns
3 min read
Article Tags :