Sunday, 12 January 2020

Cassandra: Keyspace data replication strategies

Cassandra replicate data in multiple nodes to ensure tolerance and reliability. A replication strategy determines, which nodes can store what data. Cassandra support two replication strategies.
a.   SimpleStrategy
b.   NetworkTopologyStrategy

SimpleStrategy
In simple strategy, user has to define 'replication_factor' attribute. If 'replication_factor' is set to 3, then three different nodes should store the copy of each row.

It is recommended to use SimpleStrategy in below scenarios.
a.   In Developement Environment
b.   If you have single data center and one rack

For example, below snippet creates a keyspace ‘cassandratutorial’ with ‘SimpleStrategy’ and replication_factor as 3.

CREATE keyspace cassandratutorial WITH REPLICATION =
{
         'class' : 'SimpleStrategy',
         'replication_factor' : 3
};
cqlsh> CREATE keyspace cassandratutorial WITH REPLICATION = 
   ... { 
   ... 'class' : 'SimpleStrategy', 
   ... 'replication_factor' : 3 
   ... };
cqlsh> 
cqlsh> DESCRIBE cassandratutorial;

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

NetworkTopologyStrategy
You can specify replication factor for each data center in the cluster. You can even choose replicas within a datacenter from different racks.
a.   If number of racks are greater than or equal to the replication_factor, then each replica is placed in different rack.
b.   It is recommended to use this strategy in production.

For example, below snippet creates a keyspace ‘cassandratutorial’ that replicates data in 'bangalore', 'tokyo' and 'berlin' data centers.

CREATE KEYSPACE cassandratutorial WITH REPLICATION = {
         'class' : 'NetworkTopologyStrategy',
         'bangalore' : 3, //Datacenter 1
         'tokyo' : 2, //Datacenter 2
         'berlin' : 3 //Datacenter 3

 };

cqlsh> CREATE KEYSPACE cassandratutorial WITH REPLICATION = { 
   ... 'class' : 'NetworkTopologyStrategy', 
   ... 'bangalore' : 3, //Datacenter 1
   ... 'tokyo' : 2, //Datacenter 2
   ... 'berlin' : 3 //Datacenter 3 
   ...  };
cqlsh> 
cqlsh> DESCRIBE KEYSPACE cassandratutorial;

CREATE KEYSPACE cassandratutorial WITH replication = {'class': 'NetworkTopologyStrategy', 'bangalore': '3', 'berlin': '3', 'tokyo': '2'}  AND durable_writes = true;

cqlsh>

Previous                                                    Next                                                    Home

No comments:

Post a Comment