Can NoSQL Databases be strongly consistent?

Aakash Goyal
Technical Insights
Published in
3 min readFeb 17, 2024

Can we achieve strong consistency in NoSQL databases?

What is replication factor and what are the different consistency levels for read and write operations?

Let's understand these concepts using an example of Apache Cassandra.

In Apache Cassandra, replication factor and consistency levels are crucial concepts related to ensuring fault tolerance, high availability, and data consistency in a distributed database system.

Note 1: In Cassandra, there is no strict concept of a “master” node and “replica” nodes as you might find in traditional master-slave architectures. Cassandra is designed as a distributed and decentralized system where all nodes are treated as equals. This architecture contributes to its ability to provide high availability, fault tolerance, and scalability.

Replication Factor in Cassandra:

1. Definition:

  • The replication factor (RF) in Cassandra refers to the number of copies (replicas) of each piece of data that are stored across the nodes in the Cassandra cluster.

Note: Its worth mentioning here that the concept of RF is although generic, but it carries different meaning in different storage technologies. Kafka, for eg, has a concept of leader-follower unlike Cassandra where all nodes are treated equal. So, a RF = 2 in Kafka means that there are total three copies of a data (with one leader and two followers). But a RF = 2 in Cassandra means that there are only two copies in total.

2. Importance:

  • Replication is critical for fault tolerance. If a node fails, having multiple replicas ensures that the data remains available and can be retrieved from other nodes.

3. Setting Replication Factor:

  • The replication factor is set at the keyspace level in Cassandra. Each keyspace can have its own replication factor.
  • Example CQL command to create a keyspace with a replication factor of 2:
CREATE KEYSPACE my_keyspace WITH replication = {‘class’: ‘SimpleStrategy’, ‘replication_factor’: 2};
  • In the example above, the keyspace “my_keyspace” will have a replication factor of 2.

4. Consistency Levels in Cassandra:

  • Read Consistency Level (RC): The consistency level defines how many nodes must respond to a read operation before it is considered successful.
  • Write Consistency Level (WC): For write operations, the consistency level defines how many replicas must acknowledge the write before it is considered successful.
  • Tunable Consistency: Cassandra allows you to set different consistency levels for read and write operations. This tunable consistency allows you to balance between read and write performance and the level of consistency you require.

5. Example Scenarios:

  • If the replication factor is 3 and the consistency level for a read or write operation is set to `QUORUM`, Cassandra ensures that at least two out of three replicas respond for the operation to be considered successful.
  • If the replication factor is 2 and the consistency level for a read or write operation is set to `ALL`, both replicas must respond for the operation to be considered successful.

6. Strong consistency:

For strong consistency, it’s not necessary that data must be written to all nodes. If RF < RC + WC, then the system will be highly consistent. Below are two examples:

  • If RF = 3, WC = QUORUM, RC = QUORUM, then W (2) + R (2) > RF (3) i.e. system is highly consistent.
  • If RF = 3, W = ALL, R = ONE, then W (3) + R (1) > RF (3) i.e. system is highly consistent.

Consistency Levels Available:

  • ONE: Requires acknowledgement from one replica.
  • QUORUM: Requires acknowledgement from a majority of replicas (`replication_factor / 2 + 1`).
  • LOCAL_QUORUM: Requires acknowledgement from a majority of replicas in the local data center.
  • ALL: Requires acknowledgement from all replicas.

The choice of replication factor and consistency level depends on the specific requirements of the application. Configuring these parameters should be done carefully based on the use case and the desired trade-offs between availability and consistency.

Feel free to leave comments and don’t forget to clap if you liked the content.

--

--

Aakash Goyal
Technical Insights

On a mission to help people break their dis-empowering patterns and motivate them from within.