Home/Labs/Session Consistency Router
All 280 Labs
INTERACTIVE LAB🍪

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.

p99 replication lag500 ms
Sticky-pin window (3× lag heuristic)1500 ms
Applies to the time-based primary pin strategy. Set it near 3× p99 lag.
Stale reads
6/10
Reads on primary
0/10
Replica offload
100%
Session ledger (write LSN vs served LSN)
user 1 · read +100ms · replica 1LSN 1000/1001 ✗ STALE
user 2 · read +400ms · replica 2LSN 1001/1002 ✗ STALE
user 3 · read +800ms · replica 0LSN 1003/1003 ✓ fresh
user 4 · read +200ms · replica 1LSN 1003/1004 ✗ STALE
user 5 · read +1500ms · replica 2LSN 1005/1005 ✓ fresh
user 6 · read +500ms · replica 0LSN 1005/1006 ✗ STALE
user 7 · read +90ms · replica 1LSN 1006/1007 ✗ STALE
user 8 · read +3000ms · replica 2LSN 1008/1008 ✓ fresh
user 9 · read +2000ms · replica 0LSN 1009/1009 ✓ fresh
user 10 · read +60ms · replica 1LSN 1009/1010 ✗ STALE

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.
Interview Round Script

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.

Key Trade-Offs

Guaranteeing your own writes back costs either replica-offload efficiency or per-read sequence bookkeeping.

Related Curriculum Chapter

Read-Your-Writes & Session Consistency

Read Full Chapter Blueprint

Explore More Interactive Labs

View All 280 Labs