Read-Your-Writes Session Consistency Lab (Interactive)
Round-robin versus LSN cookies versus sticky pins — count stale reads per strategy. Simulate ten user sessions reading replicas seconds after writing, comparing routing strategies on stale reads served and how much load the primary absorbs.
Read-Your-Writes Session Router
10 users update their profile, then reload 60ms–3s later against lagging replicas. Pick a session strategy and count the “my edit vanished” bugs.
Naive round-robin: Naive balancing is the anomaly generator: successive reads from one session land on differently-lagged replicas, producing stale reads and even monotonic-read time travel.
How It Works Under the Hood
Read-your-writes is the most user-visible session guarantee: you save a profile, reload, and your edit is gone because the read hit a replica still replaying the WAL. Systems fix it by routing that session’s reads to the writer (sticky pin), by carrying the write’s log sequence number in a cookie and only serving from replicas caught up to it, or by pinning reads to the primary briefly. This lab runs each strategy against configurable replica lag and shows the cost: cookie routing is precise but needs per-read bookkeeping, sticky pins decay into hot primaries.
Core Architectural Principles
- Per-session LSN check: a replica serves the read only once its drained position meets the cookie.
- Stale-read counter under round-robin quantifies the naive load-balancer failure.
- Primary offload percentage measures how much write traffic each strategy pins home.
Offer this as the cheap fix to replica staleness: “the write returns an LSN in a session cookie; the read router picks replicas reporting applied-LSN past it.” For interviews also mention sticky routing as the coarse fallback and that monotonic reads pair with it.
Guaranteeing your own writes back costs either replica-offload efficiency or per-read sequence bookkeeping.