A user places an order, then immediately opens My Orders and does not see it (read replica lag). How do you guarantee they always see their own latest write?
How I frame it
Classic read-your-writes violation: write hit the primary, list UI hit a lagged replica. Global strong consistency on every read is expensive. I guarantee RYW for the actor who just wrote, not for the whole world.
Approaches (pick / combine)
- Sticky read-after-write window: After
POST /orders, setX-Read-Primary: until=<ts+N>cookie / header. Order-list for that session reads primary for 5–30s. - Return the resource in the write response: Client optimistic-inserts into local state / cache before navigating to My Orders — no immediate list refetch required.
- Causal token: Write returns
replication_lsn/ GTID. Read sends it; replica waits until caught up or falls back to primary. - Monotonic reads per user: Session affinity to one replica after first read — weaker than RYW but stops flip-flopping.
POST /orders → primary (order #991) GET /orders → replica (lag 800ms) → missing #991 ✗ Fix: GET /orders with read_your_writes=1 → primary or client merges #991 from POST body into list
What I would ship
Combine: (1) POST returns full order DTO, client seeds list; (2) sticky primary reads for that user's order/payment paths for a short TTL; (3) replica for anonymous catalog and other users' traffic. Monitor replica lag; if lag > threshold, failover reads to primary or shed secondary regions.
Weak answer
"Always read from primary." Kills scale. "Tell the user to refresh." Not a system design.
Follow-up questions
Another device (phone) should see the order too?
Sticky cookie is device-local. Use causal token in a short-lived user cache (Redis user:ryw_until) checked by all devices, or accept eventual for second device within lag window.
Multi-region primary?
Harder — RYW may require reading the region that took the write. Route by booking_home / user shard region.
Is this the same as linearizability?
No — only the writer (and maybe session) sees the write immediately. Others can still be stale for a bit.