Database Concurrency & Scaling interview question · Most asked

A user saves their profile and the next page shows the old data. Reads go to replicas. How do you fix it?

Summary

Provide read-your-writes: route that user's reads to the primary for a window after a write, or record the primary's LSN/GTID after the write and only read from replicas that have replayed past it.

Longer Answer

Asynchronous replicas are always slightly behind. Options:

  • Sticky window: store wrote_at in the session; for N seconds read from the primary.
  • Position tracking: pg_current_wal_insert_lsn() after the write; compare with pg_last_wal_replay_lsn() on replicas (MySQL: WAIT_FOR_EXECUTED_GTID_SET).
  • Monitor lag and stop using replicas beyond your staleness budget.

Key Points

  • Async replicas lag
  • Sticky-to-primary after writes
  • LSN/GTID position checks
  • Lag-aware routing

Deep dive: Read-Your-Writes

More most-asked Database Concurrency & Scaling questions

← All Database Concurrency & Scaling questions