Read-After-Write Consistency
What you'll learn: How to ensure that users immediately see the data they just wrote, even when using read replicas.
The Problem
When you split reads and writes between a primary database and read replicas (as discussed in lessons 220-223), asynchronous replication creates a window where your replica lags behind. If a user writes to the primary and immediately reads from a replica, they might not see their own change yet—a confusing experience!
Imagine posting a comment on social media and refreshing the page, only to find your comment missing. Even though you wrote it successfully, the read replica serving your page hasn't received the update yet.
The Solution
Read-After-Write Consistency (also called "read-your-writes") guarantees that users always see their own modifications immediately. Here's how systems achieve this:
Route User's Reads to Primary
After a user performs a write, route their subsequent reads to the primary database for a short window (maybe 1-2 seconds). This ensures they read from the same source that has their latest write.
Session-Based Consistency Tracking
Use session information to track which user just wrote data. The routing layer checks: "Did this user recently write? If yes, send their reads to primary. If no, use replicas."
Timestamp Checks
Store the timestamp of the user's last write in their session. Only route to replicas if they're caught up past that timestamp.
Why It Matters
This pattern solves the awkward UX problem where users don't trust your system because they can't see their own actions. It balances consistency (for the writing user) with scalability (replicas for everyone else).
Key Takeaway: Read-After-Write Consistency ensures users immediately see their own writes by intelligently routing their reads to the primary database or using session-aware routing, preventing the confusing experience of "missing" data they just created.