Read-After-Write Consistency in Production Systems: From Write to Read

 

Imagine a user changes their email address in an application.

The write succeeds on the primary database. A few milliseconds later, the user refreshes the page—but the old email appears.

What happened?

The read may have been routed to a read replica that had not yet received the latest change.

This is the practical problem behind read-after-write consistency.

The Basic Flow

A typical production architecture might look like:

              ┌── Replica A
              │
Application ── Primary
              │
              ├── Replica B
              │
              └── Replica C


Writes go to the primary. Reads may be distributed across replicas.

The important detail is that replication can have some delay.

A successful write acknowledgement means the primary accepted the write according to its configured commit/durability rules. It does not automatically mean every replica has applied the change.

How Does the Next Read Choose a Replica?

After the write, the application can retain a consistency requirement for subsequent reads.

For example:

Required state: 5000

Replica A → 4970 ❌
Replica B → 5000 ✅
Replica C → 4980 ❌

Replica B is sufficiently current, so the read can go there.

If no replica satisfies the requirement, the system may fall back to the primary, retry, or apply another explicitly defined policy.

The exact mechanism depends on the database and replication architecture.

Why Replica Lag Matters

Replica lag is not only a monitoring metric.

It can affect request correctness.

A replica can be: healthy, available, responding quickly,

and still be the wrong destination for a consistency-sensitive read because it is behind the required state.

Don't Permanently Pin Everything to the Primary

One simple solution is to send all reads to the primary after a write.

But that can unnecessarily increase primary load.

A better architecture can use temporary consistency state:

Write
  ↓
Remember required state
  ↓
Check replica progress
  ↓
Eligible replica → Read
No eligible replica → Fallback


Once the consistency requirement has been satisfied, normal replica routing can resume.

What About Failures?

Production systems also need to handle:

  • primary failure,
  • increasing replica lag,
  • unhealthy replicas,
  • replication interruptions,
  • no replica satisfying the required state.

The router should consider both node health and replication state.

An available database node is not necessarily an eligible database node.

Common Mistake

A common misconception is:

“Write to primary, read from replica” is enough.

It isn't.

The application needs to connect:

Write acknowledgement → Consistency requirement → Replica state → Routing → Fallback

The exact implementation varies by database, replication mode, and configuration, so database-specific behavior should always be verified rather than assumed.

Key Takeaway

Read-after-write consistency is about ensuring that a subsequent read sees the state the application requires.

For read-heavy applications, the goal is to balance consistency, latency, primary load, and replica scalability rather than blindly routing every read to the same database node.

Previous Post Next Post