Replica Lag (30s)
A user reads their old profile immediately after a successful write.
Topic tools
Layout updated
Prompt
A user reads their old profile immediately after a successful write.
A replica is a delayed photocopy. A user who just changed a fact may need the latest original rather than whichever copy responds first.
Constraints
- Honor the freshness requested by the session.
- Limit waits and primary fallback during replica delays.
Think first
Take five minutes to state the failure, the evidence you need and your first safe move.
Staged reveal
First move
The writer owns committed state and replicas report what they have applied.
What to check
Carry the required commit/version token and compare replica apply progress.
Recovery
Route to an eligible replica, wait within a deadline or use the primary. Return an explicit error if the requested guarantee cannot be met.
What you give up
Read-your-writes applies to the stated session. A stronger global ordering promise needs additional coordination, latency or reduced availability.
Architecture and technical reference
High-level design
- Edge
- State
- Compute
━━ Synchronous┄┄ Asynchronous···· Recovery / control▧ Fault
Each arrow is one interaction. Request, background and recovery branches are not one linear transaction.
Read the 4 flow steps
Problem statement
A replica is 30 seconds behind immediately after the user changed their profile. Which reads can use it?
Functional requirements
- Read data from a primary or replica.
- Let a session request its latest saved version.
- Wait, use the primary or report unavailable fresh data.
Non-functional requirements
- Honor the freshness requested by the session.
- Limit waits and primary fallback during replica delays.
Entities
| Entity / logical owner |
|---|
| Authoritative state |
API design
| Operation / input | Response and retry contract |
|---|---|
Decision contract | Identify acceptance, retry identity, version/ and the response on uncertainty. |
High-level design component walkthrough
Read Client
Needs fresh profile
Why, tradeoff and connected operations
An application or user-facing endpoint starts or receives this flow.
Tradeoff: Stronger guarantees increase latency
Connected operations: Submit request
Consistency Router
Choose freshness policy
Why, tradeoff and connected operations
Centralize admission and routing while keeping business-state ownership downstream.
Tradeoff: Extra branching and coordination
Connected operations: Submit request; stale OK; strong read; no healthy source
PostgreSQL / Lagging Replica
Fast, possibly 30s stale
Why, tradeoff and connected operations
: PostgreSQL. Relational tables and constraints own this state. Logical tables shown within the same business database are not separate databases; only at the defined transaction boundary. Use transactional constraints for related authoritative records; indexes follow the access pattern.
Tradeoff: Can violate read-your-writes
Connected operations: stale OK
PostgreSQL / Primary Database
Latest acknowledged state
Why, tradeoff and connected operations
: PostgreSQL. Relational tables and constraints own this state. Logical tables shown within the same business database are not separate databases; only at the defined transaction boundary. Use transactional constraints for related authoritative records; indexes follow the access pattern.
Tradeoff: Read overload on writer
Connected operations: strong read
Wait / Error Path
When freshness required
Why, tradeoff and connected operations
Isolate this operation so it can scale and fail independently from storage.
Tradeoff: Unavailability / long waits
Connected operations: no healthy source
Engineering control room
Deep dives — decisions, failures and recovery
Open a deep dive to follow the steps, try an example and see how recovery works.
Tradeoffs and alternatives
| Decision | Tradeoff / alternative and recovery implication |
|---|---|
| Compare the read with the session's saved version | Read-your-writes applies to the stated session. A stronger global ordering promise needs additional coordination, latency or reduced availability. |
| Wait briefly or choose a fresh-enough source | If the primary is unavailable, serve old data only under a permitted stale-read mode. Otherwise return an explicit freshness error. |
| What happens if a region fails? | Cross-region synchronous commits reduce missing acknowledged writes but add network latency; asynchronous recovery requires a stated weaker promise. |
| Avoid overloading the primary with fresh reads | More primary fallback improves success until it exhausts writer capacity; waiting preserves load isolation at the cost of latency. |
Production scars & failure modes
A user reads their old profile immediately after a successful write.
Look for: Carry the required commit/version token and compare replica apply progress.
Recovery and invariant: Route to an eligible replica, wait within a deadline or use the primary. Return an explicit error if the requested guarantee cannot be met.
| Component / failure | Impact | Recovery mechanism | Alternative / tradeoff |
|---|---|---|---|
| Primary failure / race | A replica is 30 seconds behind immediately after the user changed their profile. Which reads can use it? | Carry a session commit token and route to the writer or wait until the replica has applied it. Bound waits and return an explicit freshness error if needed. | Read-your-writes is a session guarantee, not automatically global linearizability. Stronger reads cost latency or availability. |
| Recovery follow-up | The primary is unavailable. Explain which fields can be stale and which operation must reject. | Carry a session commit token and route to the writer or wait until the replica has applied it. Bound waits and return an explicit freshness error if needed. | State the guarantee you relax and how you detect recovery. |
90-second interview summary
For replica lag, I would separate fast response from fresh response. The primary records commit order and each replica exposes how far it has applied that history. A reachable replica with two-millisecond latency can still violate a user's read-your-writes requirement.
After a successful write, the client carries a session commit token into the relevant read. The router chooses an eligible replica, waits within a deadline for it to catch up, or uses the primary under a bounded fallback budget. It does not silently weaken the session guarantee when no source qualifies.
Waiting and fallback both need capacity limits. A large wave of refreshes while replicas are delayed can overload the primary or fill the waiting pool, so excess requests receive an explicit freshness error unless a separately selected stale-read mode is allowed.
After regional promotion, the token must still refer to a compatible history. A numerically large position from another timeline is not proof that the original write survived. I would measure applied gaps, waiting age, primary fallback and missing-token recovery. The tradeoff is latency and availability versus explicit freshness, with the guarantee stated per request rather than assumed for every replica read.
Explain it without looking
Explain compare the read with the session's saved version
Reveal answer
The writer owns committed state and replicas report what they have applied.
A replica can answer quickly while still missing the user's latest change.
Read-your-writes applies to the stated session. A stronger global ordering promise needs additional coordination, latency or reduced availability.
Explain wait briefly or choose a fresh-enough source
Reveal answer
Carry the write's version token and route only to a source that covers it.
A fast stale answer is still wrong when the caller explicitly requested its latest write.
If the primary is unavailable, serve old data only under a permitted stale-read mode. Otherwise return an explicit freshness error.
Explain what happens if a region fails?
Reveal answer
Do not claim a fresh session read if the recovery region missed its write.
Session guarantees extend beyond ordinary replica lag when authority changes.
Cross-region synchronous commits reduce missing acknowledged writes but add network latency; asynchronous recovery requires a stated weaker promise.
Explain avoid overloading the primary with fresh reads
Reveal answer
Read-your-writes must have a deadline and a capacity plan.
A consistency guarantee includes what the service does when it cannot meet the guarantee promptly.
More primary fallback improves success until it exhausts writer capacity; waiting preserves load isolation at the cost of latency.
Revision checklist
Revision checklist
- Reconstruct compare the read with the session's saved version: The writer owns committed state and replicas report what they have applied.
- Reconstruct wait briefly or choose a fresh-enough source: Carry the write's version token and route only to a source that covers it.
- Reconstruct what happens if a region fails?: Do not claim a fresh session read if the recovery region missed its write.
- Reconstruct avoid overloading the primary with fresh reads: Read-your-writes must have a deadline and a capacity plan.
- Name the authoritative state, boundary, retry identity and failure-domain durability guarantee.
- Explain one alternative and exactly which guarantee you would relax.
- Redraw the request and recovery paths without looking; mark what required a hint.
On this page
- Prompt
- Constraints
- Think first
- Staged reveal
- High-level design
- Problem statement
- Functional requirements
- Non-functional requirements
- Entities
- API design
- High-level design component walkthrough
- Engineering control room
- Deep dives — decisions, failures and recovery
- Tradeoffs and alternatives
- Production scars & failure modes
- 90-second interview summary
- Explain it without looking
- Revision checklist