Designing Data-Intensive Applications
Ch. 9

Consistency Guarantees

Consistency models define what reads can return after writes — from eventual to linearizable.

When data is replicated, reads may not reflect the latest write. Consistency models formalize what clients can expect. The spectrum runs from eventual (replicas converge eventually) to linearizable (behaves like a single copy).

In practice

DynamoDB offers eventual (default) or strong (R+W>N) reads per request. MongoDB readConcern: majority waits for replication to a quorum. Redis async replication means reads from replicas may be stale — many teams read only from the primary for consistency-sensitive data. Caching in Redis amplifies consistency questions: what TTL is acceptable for stale catalog data?

Facebook at scale

News feed tolerates seconds of staleness in Redis cache-aside — a slightly old post order is acceptable. Account balance reads require strong consistency from the primary, not a cached replica.

typescript — Cache-aside with tunable staleness
// Facebook feed cache-aside pattern
async function getFeed(userId: string): Promise<Post[]> {
  const cacheKey = `feed:${userId}`;
  const cached = await redis.get(cacheKey);
  if (cached) return JSON.parse(cached);

  const posts = await db.posts.findMany({
    where: { userId },
    orderBy: { createdAt: "desc" },
    take: 50,
  });
  await redis.setex(cacheKey, 300, JSON.stringify(posts));
  return posts;
}
Key Takeaways
  • Eventual consistency: replicas converge if no new writes occur.
  • Causal consistency preserves cause-and-effect ordering.
  • Linearizability: every operation appears instantaneous at some point between start and end.
  • Stronger guarantees cost latency and availability during partitions.
  • Choose the weakest consistency model that satisfies your application invariants.
  • DynamoDB tunable consistency, MongoDB read concerns, and Redis replication illustrate the spectrum.
eventual consistencyDynamoDBMongoDBRediscausal consistencylinearizability