Scrivio Interview library
LeadershipAdvanced6 min read · updated 12 November 2026

Teams across your company each implemented their own replica routing and lag handling, with different bugs. How would you standardise read scaling and consistency guarantees?

We stop every team from re-solving the same problem by writing down a small menu of consistency models, primary only, bounded staleness, read-your-writes and monotonic reads, and providing one shared library or proxy that implements them with lag metrics and fallback built in. Company-wide defaults set when a replica leaves the pool and when someone is alerted, integration tests simulate lag, and design review checks which model each new service chose and why.

Why this question is asked

When ten teams each write their own replica routing, we get ten slightly different bugs: one sends post-write reads to replicas, one never notices a replica that is an hour behind, one falls back to the primary so eagerly that the primary melts during a lag spike. Each bug is small, but together they make read scaling unpredictable and incidents hard to reason about. Interviewers ask this at senior level to see whether we can turn scattered solutions into a platform without slowing teams down. In this answer, we will see why the bugs keep appearing, define the consistency models in plain words, design the shared implementation, set the defaults, make lag testable and keep the choice visible in design review.

1Why Every Team Gets It Slightly Wrong

Routing reads to replicas looks like a ten-line change: if the transaction is read-only, use the replica DataSource. What makes it hard is everything that ten-line version leaves out. It does not know whether the caller just wrote, it does not know how far behind the replica is, and it has no rule for what to do when replicas are unhealthy. Each team discovers one of these gaps in an incident and patches that one.

So the root problem is not a lack of skill. It is that the question, how fresh must this read be, is never asked explicitly. The fix starts by giving teams words for the answer.

2Writing Down the Consistency Models

We define four models and nothing else, so that every read in the company falls into one of them. Each one is a promise to the caller, and each has a known cost.

The menu of consistency models
ModelPromiseTypical useCost
Primary onlyAlways the latest committed dataPayments, stock checks before saleAll load on the primary
Read-your-writesYou see your own changes immediatelyProfile and settings pagesSome reads go to the primary
Monotonic readsNever older than what you already sawFeeds, order historyStick a user to one replica or carry a token
Bounded stalenessAt most N seconds oldCatalogues, search, dashboardsReplicas over N seconds leave the pool
Every read path names one model in code and in its design document.

3One Shared Implementation

Next we build the models once. For Java services that usually means a Spring Boot starter: a library that, once added as a dependency, configures the routing DataSource, the replica health checks and the metrics. A service then annotates a method or repository with the model it needs, and the library decides where the read goes.

Java
@Service
class ProfileService {
@ReadConsistency(Model.READ_YOUR_WRITES)
public Profile myProfile(String userId) { return repo.findById(userId).orElseThrow(); }
@ReadConsistency(value = Model.BOUNDED_STALENESS, maxStalenessSeconds = 30)
public List<Product> catalogue() { return products.findActive(); }
}
// the starter publishes: db.replica.lag.seconds{replica=r1}, db.read.route{model,target},
// db.read.fallback.total{reason=lag|unhealthy}

Notice the metrics. Every routing decision is counted by model and target, and every fallback to the primary is counted with its reason. That is what turns a lag incident from guesswork into a graph. For services that are not on the JVM, or for databases where a proxy is natural, the same models can live in a proxy layer such as ProxySQL for MySQL or Pgpool-II for PostgreSQL, though a proxy cannot know who just wrote unless the application passes it a hint. Many companies end up with the library for read-your-writes and a proxy or service discovery for health.

4Setting the Defaults Everyone Shares

A library is only as good as its defaults, because most teams never change them. We set company-wide values: a replica leaves the read pool above 5 seconds of replay lag and returns below 1 second; teams are alerted when a replica stays over 30 seconds for five minutes; and when every replica is out of the pool, bounded-staleness reads may fall back to the primary only up to a rate limit, so a lag spike cannot overload it. Teams can override a default, but the override is visible in configuration and reviewed.

These numbers are not magic. They come from our measured normal lag, usually under 100 ms, with a wide margin. The point is that they are the same everywhere, so an on-call engineer who knows one service knows them all.

5Making Lag Testable

Lag bugs survive because test environments have no lag. A local replica, if one exists at all, is always current. So the library ships with a test helper, and the platform provides a lagging replica in integration environments. On PostgreSQL, a standby with recovery_min_apply_delay = '5s' applies every change exactly five seconds late, which makes read-your-writes bugs reproducible on every run instead of once a month in production.

Java
@Test
void userSeesNewAddressImmediately() {
profiles.updateAddress("u1", "12 New Street");
// the integration replica applies WAL 5 s late, so a wrong route returns the old value
assertThat(profiles.myProfile("u1").address()).isEqualTo("12 New Street"); // passes only if routed correctly
}

6Keeping the Choice Visible in Design Review

Finally, the decision moves into the design review template. Every new service states which consistency model each read path uses, why, and what happens when replicas are unavailable. Reviewers check for the usual mistakes: payment checks on bounded-staleness reads, or bounded staleness with no maximum.

For the existing services, we would not demand an immediate rewrite. We would rank them by incident history and traffic, migrate the top few to the shared library with the teams, measure the drop in incidents, and use that result to bring the rest along. Standardisation sticks when teams see that the shared path is easier and safer than their own, not when it is mandated alone.

Step throughRolling out the standard
  1. Inventory existing routing codeand the incidents it caused
  2. Agree the four models and defaultswith team leads
  3. Ship the starter and lagging test replicaplatform team owns them
  4. Migrate the three riskiest servicesmeasure incidents before and after
  5. Add the model to design reviewfor every new read path

5 steps in order. Play them, or step through them one at a time.

7Follow-up questions

Why a library rather than only a proxy?

Read-your-writes needs to know who just wrote, which only the application knows. A proxy can handle health and bounded staleness, but it would need a hint from the application for the other models. A library sits where that knowledge is, and it can still use a proxy for connection management underneath.

What do you do with a team that refuses to adopt the library?

Ask what it lacks. Often a team has a real requirement the library missed, and adding it improves the standard for everyone. If the reason is only preference, the team can keep its own code as long as it meets the same contract: the same models, metrics and defaults, verified by the same lag tests.

How do monotonic reads work in practice?

Monotonic reads mean a user never goes backwards in time. The simplest implementation sticks a user to one replica for a session; a more robust one carries the highest LSN the user has seen and only reads from replicas at or past it, the same token mechanism as read-your-writes.

8Conclusion

In this answer, we saw that teams get replica routing wrong because the freshness a read needs is never stated. We wrote down four consistency models, primary only, read-your-writes, monotonic reads and bounded staleness, implemented them once in a shared starter with metrics and fallback, set common lag thresholds and alerts, made lag reproducible in tests with a delayed replica, and kept the choice visible through design review and a measured rollout.

What to say in the interviewAbout 90 seconds aloud

Open with

I would start by naming the problem: every team answers how fresh does this read need to be in code, implicitly, so each one gets a different edge wrong.

Then, if they want more

So first I would write down a short menu of consistency models: primary only, read-your-writes, monotonic reads and bounded staleness, each with a promise, a typical use and a cost.

Then I would have a platform team build them once, for our Java services as a Spring Boot starter where a method declares its model, and the library handles routing, replica health, fallback to the primary and metrics for lag, routes and fallbacks. Where a proxy fits better it can handle health and staleness, but read-your-writes needs the application's knowledge.

I would set shared defaults, for example a replica leaves the pool above five seconds of lag and returns below one, alerts after thirty seconds sustained, and a rate-limited fallback so a lag spike cannot overload the primary. Then I would make lag testable: an integration replica with recovery_min_apply_delay of five seconds turns rare production bugs into failing tests.

Finally, the model goes into the design review template for new services, and for existing ones I would migrate the riskiest few first, measure the drop in incidents and use that to bring the others along rather than relying on a mandate alone.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Teams across your company each implemented their own replica routing and lag handling...": https://scrivio-interview-library.netlify.app/databases/replication-and-ha/b3f163dea6/