sync_problem

Consistency
Models

When user A updates their profile, when does user B see it? Immediately? In 5 seconds? Never? This "when" is the consistency model.

Strong Consistency

lock

Linearizability

The system behaves as if there is only one copy of the data and all operations are atomic.

1

User A writes X = 5. Success.

2

User B reads X. MUST see 5.

Cost:

High latency (must replicate to all nodes before returning success). Low availability (if one node is down, write might fail).

Eventual Consistency

schedule

"Ideally, eventually."

If no new updates are made to a given data item, eventually all accesses to that item will return the last updated value.

1

User A writes X = 5 to Node 1.

2

User B reads X from Node 2. Might see 4 (old).

3

100ms later... Node 1 syncs to Node 2.

4

User B reads X. Now sees 5.

Benefit:

Extremely fast writes (return immediately). High availability. Standard for DNS, Social Feeds, Comments.

Causal Consistency

A middle ground. Operations that are causally related must be seen by everyone in the same order. Unrelated operations can be seen in any order.

The Chat Example

Alice: "I am breaking up with you." (Msg A)
Bob: "Whatever, I never loved you." (Msg B, caused by A)
Eventual Consistency

Charlie might see Bob's reply BEFORE Alice's message.
"Whatever..." → "I am breaking up..."
(Makes no sense)

Causal Consistency

Guarantees that if B depends on A, everyone sees A before B.

Linearizability vs Serializability

Linearizability

Single Object

Guarantee about a single register (key-value pair). Reads return the absolute latest write.

"Freshness guarantee."

Serializability

Multiple Objects (Transactions)

Guarantee about transactions (groups of operations). Execution ensures the same result as if transactions ran one after another.

"Isolation guarantee."

Strict Serializability = Linearizability + Serializability. (The holy grail, very expensive).

Quorums (R + W > N)

groups

How to configure Consistency?

In systems like Cassandra/Dynamo, you choose your consistency level per request.

  • N = Total replicas (e.g., 3)
  • W = Write quorum (nodes that must confirm write)
  • R = Read quorum (nodes that must confirm read)
Rule: R + W > N

(Ensures read and write sets overlap by at least one node)

Configurations

Fast Read:W=N, R=1

Write to all, read from any one.

Fast Write:W=1, R=N

Write to one, read from all (to find latest).

Balanced:W=QUORUM, R=QUORUM

Usually (N/2 + 1). E.g., N=3, W=2, R=2.