Course contentsShow
← System Design
Lesson 1359 of 1,91934. Data Replication StrategiesFree lesson

Read-Your-Writes Consistency with Replicas

Ensuring users see their own writes immediately even when reading from eventually-consistent replicas.

Read-Your-Writes Consistency with Replicas

What you'll learn: How to guarantee users always see their own updates, even when reading from eventually-consistent replicas.

The Problem

When you write data to a primary database and it replicates asynchronously to replicas, there's a window (replication lag) where your write hasn't reached all replicas yet. If your next read hits a lagging replica, you might see stale data—even your own recent write could be invisible to you. This creates a jarring user experience.

The Solution: Read-Your-Writes Consistency

Read-Your-Writes (also called "read-after-write") consistency ensures that after a user writes data, any subsequent read by that same user will reflect that write. Other users might still see stale data temporarily, but you always see your own changes immediately.

Implementation Strategies

  1. Route user's reads to primary: For a short time after writing, send that user's reads to the primary instead of replicas. Requires tracking "recently wrote" per user session.

  2. Timestamp-based routing: Tag writes with timestamps. When the user reads, only query replicas caught up past that timestamp. If none qualify, read from primary.

  3. Sticky sessions: Always route a specific user to the same replica that's guaranteed to have received their writes (works when using semi-synchronous replication to that replica).

  4. Client-side timestamps: The client remembers the timestamp of its last write and includes it in read requests, ensuring it only reads from sufficiently up-to-date replicas.

Real-World Example

Think of posting a comment on social media. You submit it and immediately refresh—you'd be confused if your own comment was missing! Read-your-writes ensures your comment appears instantly to you, even if others see it a second later due to replication lag.

Key Takeaway: Read-your-writes consistency guarantees users see their own updates immediately by intelligently routing their reads to primary servers or sufficiently up-to-date replicas, preventing the confusing experience of "lost" writes due to replication lag.