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_atin the session; for N seconds read from the primary. - Position tracking:
pg_current_wal_insert_lsn()after the write; compare withpg_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
- How do you prevent overselling in a flash sale with 50,000 buyers in the first minute?
- After a deploy, logs show hundreds of 'deadlock detected' errors per hour on checkout. Walk through how you'd investigate.
- Two customers buy the last item at the same moment and both orders are confirmed. What happened, and how do you prevent it?
- Your service throws 'connection pool exhausted' errors but database CPU is only 20%. What's going on?
- Why shouldn't you call a payment API inside a database transaction?
- What's the difference between Read Committed and Repeatable Read in practice?
- Database CPU is at 85%. How do you decide what to optimise before buying a bigger server?
- How do you choose a shard key for a multi-tenant SaaS?