Consistency Levels: How Many Replicas Must Answer
With replication factor 3, each row lives on three nodes. Each of these nodes is called a replica. So every read and write raises a question: how many replicas must answer before the query is done?
That number is the consistency level (CL). You can choose it for each query.
Common consistency levels
| Level | Waits for | Good and bad |
|---|---|---|
ONE | 1 replica | Fastest, but a read might get a copy that missed the latest write |
QUORUM | A majority (2 of 3 when RF is 3) | A good balance (see below) |
ALL | Every replica | Strictest, but if one replica is down, the query fails |
The consistency level does not change how many copies get the write. Cassandra sends every write to all the replicas. The level only sets how many of them must confirm before Cassandra tells your app "done".
The overlap rule
You don't need ALL to always read the latest data. You need the reads and the writes to overlap:
R + W > RF
R = replicas a read waits for
W = replicas a write waits for
RF = number of copies
With RF 3, write at QUORUM (2) and read at QUORUM (2). 2 + 2 = 4, which is more than 3. The 2 replicas a read asks must include at least one of the 2 that confirmed the write. So the read always sees the latest value.
This is why QUORUM for both reads and writes is a common setup. Reads see the latest data, and the query still works when one replica is down.
With ONE for both, 1 + 1 = 2, which is not more than 3. A read might return an old value for a short time. Cassandra updates the other copies in the background, and soon all copies agree. This is called eventual consistency. For a count of likes, that is often fine. For a bank balance, use QUORUM.
Setting the level in cqlsh
In cqlsh, the CONSISTENCY command shows the current level:
CONSISTENCY;
Output:
Current consistency level is ONE.
The default in cqlsh is ONE. Add a level to change it for the queries that follow:
CONSISTENCY QUORUM;
Output:
Consistency level set to QUORUM.
In an app, the Cassandra driver lets you set the level for each statement. So one app can write important data at QUORUM and less important data at ONE.
The replication factor decides how many copies exist. The consistency level decides how many must answer right now.
In the exercise, work out how many replicas a QUORUM write waits for.