Data Replication

7 min to complete

Data Replication

To ensure no single point of failure, data is replicated. Replication means storing copies of data on multiple nodes. This means that even if one node goes down, the data will still be available. It ensures reliability and fault tolerance. The Replication Factor defines the number of copies of the data.

For example, a replication factor of 3 (RF=3) means that three copies of the data are stored at all times. A user sets the RF for a particular keyspace. Depending on that RF setting, the coordinator will then share the data with other nodes, called replicas, to create copies of the data for fault tolerance.

More information about high availability,  replication, and consistency can be found in this lesson.

Hands-on: Create keyspace, read and write data

We’ll use the previously created cluster and the CQL shell to create a keyspace.

docker exec -it scylla-node3 cqlsh 

Create a keyspace called “mykeyspace” with a Replication Factor of three:

CREATE KEYSPACE mykeyspace WITH REPLICATION = { 'class' : 'NetworkTopologyStrategy', 'replication_factor' : 3};

Let’s see that the keyspace was created with a Replication Factor of three:

use mykeyspace; 
DESCRIBE KEYSPACE mykeyspace;

Next, create a table with three columns: user id, first name, and last name, and insert some data:

CREATE TABLE users ( user_id int, fname text, lname text, PRIMARY KEY((user_id))); 

Insert into the newly created table two rows:

insert into users(user_id, fname, lname) values (1, 'rick', 'sanchez'); 
insert into users(user_id, fname, lname) values (4, 'rust', 'cohle'); 

Read the table contents:

select * from users;

To summarize, we saw how to open a CQL Shell, create a keyspace, create a table, insert data into it, and read the data.

fa-angle-up