Scrivio Interview library
ScenarioIntermediate6 min read · updated 13 November 2026

The order service writes an order to its PostgreSQL primary and publishes OrderCreated through an outbox. The shipping service consumes the event and calls GET /orders/{id}, which the order service answers from a replica. About 2 percent of these calls return 404.

Explain the race and compare the ways to fix it, with their costs.

The order service publishes OrderCreated within milliseconds of the primary's commit, but the replica that answers GET /orders/{id} may lag by tens or hundreds of milliseconds, so the shipping service's lookup sometimes arrives before the replica has the row. The cleanest fix is to carry the needed data in the event so the consumer never calls back; alternatives are carrying the commit position (LSN) in the event and answering from a replica only once it has replayed that far, or routing internal consumers' reads to the primary. Retrying on 404 hides the race but also hides genuinely missing data, so if used it must be capped, counted and alerted on.

Why this question is asked

This race shows up in almost every system that combines an outbox, events and read replicas, and it is easy to misdiagnose as a bug in the consumer. Interviewers like it because each fix moves a cost somewhere else: into the event contract, into the order service, onto the primary, or into monitoring. In this answer, we will recap the moving parts, trace the race step by step, then compare four fixes with their costs and say which we would choose.

1A Quick Recap of the Moving Parts

The order service writes the order and an outbox row in the same transaction on the PostgreSQL primary. A relay, such as Debezium reading the replication stream or a poller reading the outbox table, publishes OrderCreated to a broker such as Kafka. The shipping service consumes the event and calls the order service's GET /orders/{id}, which, to spare the primary, is answered from a streaming replica.

Each of these steps is correct on its own. The outbox guarantees the event is published if and only if the order is committed. The replica eventually has every committed row. The problem is the word eventually, compared with how quickly the event travels.

2Tracing the Race

The commit happens on the primary at time zero. The relay sees it almost immediately and publishes the event within a few milliseconds; the shipping service consumes it a few milliseconds later and calls back. Meanwhile, the replica receives and replays the commit's WAL, which normally takes a few milliseconds but under load or during a long query on the replica can take 100 milliseconds or more. If the lookup reaches the replica before replay, the row does not exist there yet, and the order service answers 404.

About 2 percent of calls lose this race, which tells us the replica's lag is usually below the event path's latency, but not always. Notice that the consumer did nothing wrong: it reacted to a true fact, the order exists, and asked a system that had not heard about it yet.

Step throughEvent faster than replication
Order servicePrimaryKafkaShippingReplica
commit order 881 + outbox row
relay publishes OrderCreated (5 ms)
deliver event (3 ms)
GET /orders/881
SELECT ... WHERE id = 881
no row yet (replay lag 120 ms)
404 Not Found
WAL for order 881 replayed

8 messages in order. Play them, or step through them one at a time.

The event reached the consumer before the replica had replayed the commit.

3Fix 1: Carry the Data in the Event

If the event already contains what shipping needs, the address, the items and the delivery option, shipping never calls back, and the race disappears. This is called event-carried state transfer. It also removes a runtime dependency: shipping can process orders while the order service is down or slow.

Java
// Before: a notification, the consumer must call back
record OrderCreated(long orderId) {}
// After: the event carries the state shipping needs
record OrderCreated(long orderId, int version, Address shipTo,
List<Line> lines, DeliveryOption delivery, Instant createdAt) {}
// shipping handles it without any GET: no 404 is possible

The costs are real. Events become larger, and the event schema is now a richer contract: every field shipping relies on must be maintained, versioned and evolved compatibly, for example with a schema registry and compatibility checks. And consumers hold a copy of the data as of the event, so later changes, such as an edited address, need their own events. For a core flow like order to shipping, those costs are usually worth it.

4Fix 2: Read at Least the Event's Position

If the callback must stay, the event can carry a version or a commit position. With Debezium, each change record includes the source LSN; with a poller, the relay can record the transaction's commit position or the order's version number. The shipping service passes it along, for example as a header, and the order service only answers from a replica that has replayed that position, otherwise from the primary.

Java
-- GET /orders/881 with header Min-Position: 5C/1A2B3C40
-- on the chosen replica:
SELECT pg_last_wal_replay_lsn() >= '5C/1A2B3C40'::pg_lsn; -- false: 120 ms behind
-- -> answer this request from the primary (or wait up to 200 ms, then use the primary)
SELECT pg_last_wal_replay_lsn() >= '5C/1A2B3C40'::pg_lsn; -- true a moment later: replica is safe

This keeps most reads on replicas while making every read at least as fresh as the event that triggered it. Its costs are complexity in the order service's data access layer and a coupling of the event to the database's internals; a plain version number per order is easier to keep stable across database changes than an LSN.

5Fix 3: Route Internal Consumers to the Primary

The simplest code change is to send reads that come from event consumers to the primary. Those reads usually follow a recent write, so they are exactly the ones a replica may not have yet. The cost is extra load on the primary. If shipping handles 300 orders per second, that is 300 small primary-key lookups per second, which a PostgreSQL primary handles easily; if many services do this for many event types, it adds up, and the primary's capacity becomes the limit again.

6Retrying on 404, and What It Hides

Retrying with backoff, say three attempts at 50, 100 and 200 milliseconds, makes the symptom go away, and many teams stop there. The danger is that a 404 is also how the order service reports that an order genuinely does not exist, because of a bug, a deleted order or an event for the wrong environment. Retries make those cases slow and quiet instead of loud.

If we retry, we cap the attempts, count how often a retry was needed and how often it succeeded, and alert when 404s persist after all retries. The retry count is also a useful lag signal: a sudden increase means replicas are falling behind.

Fixes compared
FixRemoves the race?Main cost
Carry data in the eventyes, no callbacklarger events, richer contract to evolve
Read at event's positionyesdata access complexity, position in contract
Consumers read the primaryyesextra primary load
Retry on 404hides itslower, masks genuinely missing data

7Follow-up questions

Which fix would you choose?

For order to shipping, carrying the data in the event, because it removes the race and the runtime dependency at once. For callbacks that must stay, I would route them to the primary if the volume is small, and use a minimum position or version when it is large.

Would a delay before consuming help?

A fixed delay of, say, one second makes the race rarer but not impossible, adds latency to every order, and still fails when the replica lags more than the delay. It is a weaker form of retrying.

Could the order service read its own replica lag and decide?

Yes, as a coarse version of fix 2: if the replica's lag is below some threshold, use it, otherwise the primary. But lag below a threshold does not prove that a specific commit is visible, so the per-request position is more precise.

8Conclusion

In this answer, we saw that the outbox publishes events within milliseconds of the commit while the replica answering the callback can lag by much more, so a small share of lookups arrive before the row exists there. Carrying the needed state in the event removes the race and a dependency at the cost of a richer contract; passing a commit position or version lets the order service choose a caught-up replica or the primary; routing consumer reads to the primary costs primary load; and retrying on 404 hides the race along with real missing data, so it must be capped, counted and alerted on.

What to say in the interviewAbout 90 seconds aloud

Open with

The race is between two paths that start at the same commit. The outbox relay publishes OrderCreated within a few milliseconds of the primary committing, and shipping consumes it and calls GET orders right away.

Then, if they want more

But the order service answers from a replica, and the replica's replay lag is normally a few milliseconds but sometimes a hundred or more. When the lookup arrives before replay, the replica has no row and we return 404, which happens to about 2 percent of calls. I see four fixes.

The best for this flow is event-carried state transfer: put the address, lines and delivery option in the event so shipping never calls back. That removes the race and the runtime dependency, at the cost of larger events and a richer contract we have to version carefully.

If the callback has to stay, the event can carry a commit position or an order version, and the order service answers from a replica only if it has replayed at least that far, otherwise from the primary.

A simpler option is to route reads from internal event consumers to the primary, which is fine at a few hundred lookups per second but adds primary load.

And retrying on 404 with backoff makes the symptom vanish, but it also hides orders that genuinely do not exist, so if I used it I would cap the retries, count them, and alert when 404s persist.

About this answer

Last updated
13 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 official documentation for the version you use. 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 "Explain the race and compare the ways to fix it, with their costs.": https://scrivio-interview-library.netlify.app/architecture/partitioning-and-replication/ea6878914d/