Solving Read After Write Consistency with Routing Policies
The Read-After-Write Problem
Read-after-write consistency guarantees that if a user writes data and immediately reads it back, they see their own write. This is surprisingly difficult with asynchronous replicas. Consider a user posting a comment: the write commits to primary at time T0, then at T0 plus 50ms the user refreshes the page.
With replication lag of 10-100ms, there is significant probability the replica has not applied the change. The user sees no comment and files a bug report. This is correct system behavior from the databases perspective—eventual consistency working as designed—but terrible user experience.
Session Pinning
Session pinning routes all requests from a user session to the primary for a configurable window after any write. If a user writes at T0, all their reads until T0 plus 5 seconds go to primary. This guarantees read-after-write consistency within a session without requiring global strong consistency.
Implementation requires tracking write timestamps per session. Store the last-write timestamp in a cookie, session store, or request context. On each read, compare current time to last-write timestamp. If within the pinning window, route to primary; otherwise route to replicas.
Causal Consistency with Tokens
A more precise approach uses causal consistency tokens. When a write completes, the system returns a token representing that writes position in the replication stream (often the Log Sequence Number or LSN—a monotonically increasing identifier for each write operation). Subsequent reads include this token; the system routes to any replica that has applied at least that position.
This avoids unnecessary primary reads: if a replica has caught up past your token, it can serve the read. If no replica is caught up, fall back to primary. The token travels with the request through microservices, maintaining causal ordering across service boundaries.
Cross-Device Challenges
Session-based solutions fail when users switch devices or share URLs. A user posts from their phone, then opens the same page on their laptop—different session, different routing decisions. The laptop session has no knowledge of the phone write and may read from a lagging replica.
Solutions include user-level tracking (tie consistency tokens to user ID, not session), client-side storage (embed last-write tokens in URLs for sharing), or accepting that cross-device scenarios have weaker guarantees. The right choice depends on product requirements and how critical immediate consistency is for the use case.