Quick revision
The short answer
Checkout should not depend on Kafka at all: it writes the OrderPlaced event to an outbox table in the same database transaction as the order, and a separate relay publishes outbox rows with acks=all and idempotence, marking each row sent only after the broker acknowledges it. During a 15-minute outage the relay simply stops advancing, so the outage becomes a backlog rather than lost events, and after recovery it drains rows in order per order id. Because a crash between publishing and marking can resend a row, every event carries a unique id that consumers use to deduplicate.
What to remember
In this answer, we designed the producer side so that a Kafka outage can delay OrderPlaced events but never lose them. Checkout writes the event to an outbox table in the same transaction as the order, costing about a millisecond, and a relay publishes rows in order with acks=all, idempotence and a short delivery timeout, marking them sent only after the acknowledgement. A 15-minute outage becomes a backlog of unsent rows, drained in key order after recovery, and event ids let consumers drop the duplicates a relay crash can cause. Monitoring the oldest unsent row and an outage test in staging complete the design.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Checkout should not depend on Kafka at all: it writes the OrderPlaced event to an outbox table in the same database transaction as the order, and a separate relay publishes outbox rows with acks=all and idempotence, marking each row sent only after the broker acknowledges it. During a 15-minute outage the relay simply stops advancing, so the outage becomes a backlog rather than lost events, and after recovery it drains rows in order per order id. Because a crash between publishing and marking can resend a row, every event carries a unique id that consumers use to deduplicate.
Why this question is asked
The two requirements in the overview pull against each other. 'Never lose an event' tempts us to wait for Kafka on every checkout, and 'add only a few milliseconds' tempts us to fire and forget. Interviewers use this design question to see whether we notice that the producer's memory is not a safe place to keep events for 15 minutes, and whether we can turn an outage into delay instead of loss. In this answer, we will recap why producer settings alone cannot meet the requirement, design the outbox and the relay, choose the client settings, walk through the outage and the recovery, handle ordering and duplicates, and finish with how to size, monitor and test it.
1Why Producer Settings Alone Cannot Meet the Requirement
A first idea is to raise delivery.timeout.ms to 20 minutes and buffer.memory to hold 15 minutes of events. At a peak of 500 checkouts per second with 2 KB events, that is 450,000 events, about 900 MB, held in the memory of the checkout pods. Any deploy or crash during the outage loses it, and once the buffer is full, send() blocks checkout threads.
Waiting synchronously for the acknowledgement fails the other requirement: during the outage, every checkout would hang or fail. So the event has to be stored durably somewhere checkout already depends on, which is the order database.
2The Design: an Outbox Table and a Relay
In the same transaction that inserts the order, checkout inserts a row into an outbox table: the event id, the aggregate key (the order id), the topic, the payload and a creation time. Either both rows commit or neither does, so there can never be an order without its event or an event without its order. The extra insert costs about a millisecond on PostgreSQL, which meets the latency budget, and checkout no longer talks to Kafka at all.
A relay, a small separate process or a background component with a single active instance, reads unsent rows in id order, publishes them, and marks them sent once Kafka acknowledges them. Change data capture is an alternative to polling, as we discuss below.
- Checkout servicewrites order and outbox row in one database transaction
- PostgreSQL outbox tabledurable buffer; holds events for as long as Kafka is down
- Relayreads unsent rows in id order, publishes, marks sent after the ack
- Idempotent producer, acks=allno duplicates from client retries, written to all in-sync replicas
- Kafka topic, min.insync.replicas=2event survives a broker failure once acknowledged
- Consumersdeduplicate by event_id
6 layers, from the top down. Step through them one at a time.
No layer has to be perfect on its own; each one covers the gap left by the one above.
3The Relay's Producer Settings
The relay uses acks=all with enable.idempotence=true, both defaults in current clients, so a written event is on every in-sync replica and client retries cannot duplicate it. The topic has replication factor 3 and min.insync.replicas=2, so an acknowledged event survives the loss of a broker. The key is the order id, so all events for one order go to the same partition and stay in order there.
We keep delivery.timeout.ms bounded, for example 30 s rather than the default 120 s, because the relay has a better place to wait than the producer's memory: the table. If a send fails, the relay stops, leaves the row unsent and tries again after a back-off. A short timeout makes the relay notice an outage quickly and keeps its in-memory state small.
Notice that rows are marked sent only after every send in the chunk succeeded. If one fails, nothing is marked and the chunk is sent again, so some rows may be published twice, which the event id handles.
4Walking Through the 15-Minute Outage
When Kafka becomes unreachable, checkout carries on exactly as before, because it only writes to the database. The relay's sends fail after 30 s; it logs, waits with a growing back-off capped at, say, 30 s, and tries again. Unsent rows accumulate: at 500 checkouts per second, 15 minutes adds 450,000 rows, roughly 1 GB in the table, which PostgreSQL handles easily.
When Kafka comes back, the relay drains in id order, chunk by chunk. At 10,000 events per second the backlog clears in about a minute while new events keep arriving, and we cap that rate so the burst does not overload brokers that have just recovered.
9 messages in order. Play them, or step through them one at a time.
Checkout never waits on Kafka. The outage shows up as the age of the oldest unsent row.
5Ordering and Duplicates After Recovery
Order per order id holds because the relay publishes in id order, uses the order id as the key, and never skips ahead: an unsent row blocks the rows after it. If one relay is not fast enough, several can each own a range of order-id hashes.
Duplicates come from one unavoidable gap: the relay can crash after Kafka acknowledged a row and before it marked the row sent. On restart it publishes that row again. The producer's idempotence does not help here, because the restarted relay is a new producer session. So every event carries its event id, and consumers keep a record of processed ids, for example a unique constraint in their own database, and skip repeats. That is effectively-once processing, which is the honest guarantee for this design.
6Sizing, Monitoring and Testing
The key metric is the age of the oldest unsent row, normally below a second; an alert at 60 s catches an outage or a stuck relay. We also watch the unsent count and publish failures, and delete sent rows after a few days.
Finally, we prove it. In staging we block traffic from the relay to the brokers for 15 minutes under realistic load, then restore it, and verify that checkout latency did not move, that no event was lost, that events per order id arrived in order, and how long the backlog took to drain.
7Follow-up questions
Why not use Kafka transactions instead of an outbox?
Kafka transactions make writes to several partitions atomic with each other and with consumer offsets, but they cannot include our PostgreSQL commit. Checkout would still need two systems to agree, and it would still be blocked or failing during the outage. The outbox puts both writes in one database transaction, which is the atomicity we actually need.
Polling relay or change data capture?
Polling is simple and needs only a query and an index, and at 500 events per second a 100 ms poll is cheap. Change data capture with Debezium reads the write-ahead log, adds no query load and has lower latency, but it is another system to run and monitor, and it needs care with replication slots, which hold the write-ahead log while the connector is down. Teams that already run Kafka Connect usually pick CDC.
8Conclusion
In this answer, we designed the producer side so that a Kafka outage can delay OrderPlaced events but never lose them. Checkout writes the event to an outbox table in the same transaction as the order, costing about a millisecond, and a relay publishes rows in order with acks=all, idempotence and a short delivery timeout, marking them sent only after the acknowledgement. A 15-minute outage becomes a backlog of unsent rows, drained in key order after recovery, and event ids let consumers drop the duplicates a relay crash can cause. Monitoring the oldest unsent row and an outage test in staging complete the design.
Open with
I would not let checkout talk to Kafka at all. Checkout writes the order and an OrderPlaced row into an outbox table in the same database transaction, so the event is exactly as durable as the order and checkout only pays for one extra insert, about a millisecond.
Then, if they want more
A relay reads unsent rows in id order and publishes them with acks=all, idempotence, the order id as the key, and a topic with replication factor three and min.insync.replicas two. It marks rows sent only after the broker acknowledges them. I keep delivery.timeout.ms short, around 30 seconds, because the table is the real buffer, not producer memory.
During a 15-minute outage the relay just fails, backs off and retries, and rows pile up; at 500 orders a second that is about 450,000 rows, which is nothing for PostgreSQL. After recovery it drains them in order, rate-limited so it does not hammer the brokers. Order per key holds because the relay never skips an unsent row.
The one gap is a crash after the ack but before marking the row, which republishes it, so every event carries an event id and consumers deduplicate. I would alert on the age of the oldest unsent row and test the whole thing by cutting the network in staging.
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 documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Apache Kafka documentation
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 "Design the producer side: which client settings, what happens to events during the...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-clients-in-depth/774ddb4dcd/