Difficulty: Intermediate
What is eventual consistency, how does it differ from strong consistency, and when is it acceptable to use?
Eventual consistency means that after a write, replicas may briefly return old data, but if no new writes happen, all replicas will converge to the same value. Strong consistency means every read sees the most recent successful write, as if there were only one copy of the data. Eventual consistency gives lower latency and higher availability; strong consistency gives simpler correctness at the cost of coordination.
Distributed databases keep multiple copies of data on different machines, often in different zones or regions. When a write arrives:
The gap between write and convergence is usually milliseconds, but during network problems or heavy load it can stretch to seconds or more.
ConsistentRead: true gives a strongly consistent read at double the read cost.QUORUM and reading at QUORUM gives R + W > N, so reads overlap the latest write.CAP says that during a network partition a system must choose between consistency and availability. PACELC adds the everyday case: even with no partition, you trade latency against consistency. Eventual consistency is the "favour availability and latency" choice.
| Acceptable | Needs strong consistency |
|---|---|
| Like counts, view counts | Wallet or bank balance |
| Social feeds and timelines | Seat or inventory reservation |
| Product reviews, recommendations | Unique username registration |
| Analytics dashboards | Payment status for a UPI transaction |
| Search index updates | Permission and access revocation |
The test: what is the cost if a user sees a value that is a few seconds old? If the answer is "a slightly wrong counter", eventual is fine. If it is "we sold the same seat twice", it is not.
Pure eventual consistency can feel buggy. Systems add session guarantees:
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { DynamoDBDocumentClient, GetCommand } from "@aws-sdk/lib-dynamodb";
const db = DynamoDBDocumentClient.from(new DynamoDBClient({ region: "ap-south-1" }));
// Default: eventually consistent, cheaper, may be slightly stale
const feed = await db.send(new GetCommand({
TableName: "posts", Key: { postId: "p_123" },
}));
// Strongly consistent: reflects all writes acknowledged before the read
const wallet = await db.send(new GetCommand({
TableName: "wallets", Key: { userId: "u_42" },
ConsistentRead: true,
}));
The same database offers both models. Strong reads cost twice the read capacity and are not supported on global secondary indexes.
Eventual Consistency, Strong Consistency, Replication Lag, Quorum, Read-Your-Writes