Scrivio Interview library
DebuggingIntermediate7 min read · updated 13 November 2026

During a 2-minute network partition between one availability zone and the brokers, a service's producer logs errors like "Expiring 37 record(s) for orders-7: 120012 ms has passed since batch creation".

The team's code calls kafkaTemplate.send() and never looks at the result. What happened to those records, and what should the service change?

Those 37 records were never written: they waited in the producer's buffer and retries for longer than delivery.timeout.ms (120 s by default), so the producer failed them with a TimeoutException. Because the code ignores the CompletableFuture that kafkaTemplate.send() returns, the failure went nowhere and the service believes it published the events. The service must handle the result, logging and counting failures and saving failed events to a durable place such as an outbox table, or failing the business operation so the caller retries.

Why this question is asked

This is one of the most common ways Kafka loses data in practice, and the dangerous part is that nothing crashes. The producer did its job, reported the failure, and the application threw the report away. Interviewers like this scenario because it checks whether we know what the producer does during an outage, what that log line means, and how to design a service that does not silently drop events. In this answer, we will decode the log message, see where the records went, look at the code before and after the fix, discuss when a longer timeout helps and when it does not, and end with the ordering trap that appears when failed records are resent later.

1Reading the Log Line

The message says 37 records for partition orders-7 expired because 120,012 ms had passed since their batch was created. Two settings explain it. The producer appends records to batches in an in-memory accumulator, and a background thread sends those batches to the partition's leader, retrying on errors such as a lost connection. delivery.timeout.ms, 120,000 ms by default, is the total time a record may spend after send() returns, including waiting, sending and retrying. When it runs out, the producer gives up on the batch and completes each record's future with a TimeoutException.

So during the two-minute network partition, the brokers were unreachable from this zone. The producer kept the records and kept retrying, but the partition lasted just long enough that the oldest batches crossed the 120 s line. Those records were dropped from the client, never written to the log.

2Where the Failure Went

Spring's KafkaTemplate wraps the producer. Its send() returns a CompletableFuture<SendResult<K, V>> almost at once; the future completes later, with the partition and offset on success or with an exception on failure. The team's code looks like this.

Java
@Transactional
public void placeOrder(Order order) {
orders.save(order); // committed to the database
kafkaTemplate.send("orders", order.id(), toEvent(order)); // returns a future; nobody looks at it
// method returns normally: the caller is told the order was placed and the event published
}
// 125 s later, on the producer thread:
// Expiring 37 record(s) for orders-7: 120012 ms has passed since batch creation
Step throughThe failure nobody listened to
OrderServiceKafkaTemplateProducer bufferBroker
send(order event)
CompletableFuture, ignored
append to batch for orders-7
produce request: network partition
retry until 120 s have passed
TimeoutException: batch expired
future completed exceptionally, no one waiting

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

The service answered its caller long before the producer gave up.

Notice that by the time the failure happens, placeOrder() returned long ago and the HTTP response has been sent. The order exists in the database, but downstream services such as billing and shipping never hear about it. Spring Kafka does log the failure through its producer listener, which is why the team saw the error lines, but the code that cares about the event never learns of it.

3Fix 1: Handle the Result

The minimum change is to attach a handler to the future so that every failure is logged with context, counted in a metric, and acted on.

Java
kafkaTemplate.send("orders", order.id(), event)
.whenComplete((result, ex) -> {
if (ex == null) {
sent.increment(); // offset in result.getRecordMetadata()
} else {
failed.increment(); // alert when this is above 0
log.error("order event {} not published", event.eventId(), ex);
fallback.store(event); // durable retry, see below
}
});

The handler runs on the producer's thread, so it should stay quick. Counting failed sends per service turns a silent loss into an alert. But logging alone does not bring the events back, so we need a decision about what happens to them.

4Fix 2: Give Failed Events a Durable Home

There are two honest options. The first is to make the business operation depend on the publish: wait for the future with a timeout, and if it fails, fail the request so the caller retries. That is simple and correct, but it makes checkout availability depend on Kafka's availability and adds the publish latency to every request.

The second, usually better, option is the transactional outbox. The service writes the event to an outbox table in the same database transaction as the order, and a separate relay reads unsent rows and publishes them, marking each one sent only after the broker acknowledges it. A two-minute partition then delays events but cannot lose them, because they are stored in the database rather than in the producer's memory.

Java
@Transactional
public void placeOrder(Order order) {
orders.save(order);
outbox.save(new OutboxRow(event.eventId(), "orders", order.id(), toJson(event))); // same transaction
}
// relay, every 200 ms: read unsent rows in id order, send, wait for the ack, mark sent

Storing failed records in a fallback from the callback, as in Fix 1, is a lighter version of the same idea, but it only works while the process is alive. The outbox survives crashes too.

5Would a Longer delivery.timeout.ms Have Helped?

Partly. If we raise delivery.timeout.ms to, say, 10 minutes, and make buffer.memory big enough to hold 10 minutes of records, the producer would have ridden out this two-minute partition and delivered everything once the network healed. That is a reasonable change for a known kind of outage.

But it moves the risk rather than removing it. Records held in the producer's memory are lost if the pod is restarted, scaled down or crashes during the outage. And if the buffer fills up, send() starts blocking callers for up to max.block.ms (60 s), which turns a Kafka problem into a latency problem for the whole service. Durable recovery needs storage outside the producer, which is what the outbox provides.

6The Ordering Trap When Resending Later

Resending failed records later brings one more problem. Suppose order 42's 'placed' event failed during the partition, and after recovery its 'paid' event was published normally. If the fallback then resends 'placed', consumers see 'paid' before 'placed' for the same key. A projection that simply applies events in arrival order would end up with the order in the wrong state.

The fix is to make events self-describing: include a version number per entity, or at least the time of the change, and let consumers ignore an event older than the state they already hold. An outbox relay that publishes rows in insertion order per key avoids most of this, because it never skips ahead of an unsent row for the same key.

Choosing what to change
ChangeFixesLeaves open
Handle the future and count failuresSilent loss becomes visibleEvents are still lost
Fail the request when publish failsNo loss; caller retriesCheckout depends on Kafka being up
Larger delivery.timeout.ms and buffer.memoryShort outages absorbedCrash during the outage still loses them
Transactional outbox with a relayNo loss across outages and crashesPossible duplicates; consumers dedupe by id

7Follow-up questions

Why did send() not block or throw during the partition?

Because the producer already had metadata for the topic and enough free space in its 32 MB buffer, so send() could append to a batch and return immediately. If the outage had lasted longer, the buffer would have filled, and send() would then have blocked for up to max.block.ms and failed on the caller's thread, which is a different and more visible symptom.

Is calling kafkaTemplate.send(...).get() the right fix?

It makes the failure visible, but it turns every publish into a synchronous wait of up to the full delivery timeout, two minutes by default, inside a request thread. If we choose to wait, we should use get with a short timeout and fail the request, accepting the dependency on Kafka. In most services the outbox is a better trade.

How would we have noticed this earlier?

By monitoring the producer's record-error-rate metric and the template's failure count per service, and by alerting when either is above zero. Producer metrics are exposed through Micrometer in Spring Boot, so a dashboard of failed sends per service is cheap to build.

8Conclusion

In this answer, we saw that the expiring records waited longer than delivery.timeout.ms during the network partition, so the producer failed them with a TimeoutException and never wrote them. The code ignored the returned CompletableFuture, so the service believed the events were published. Handling the result makes the loss visible, and an outbox table or failing the request makes it recoverable. A longer timeout with more buffer memory absorbs short outages but not crashes, and resent events need a version so consumers can ignore stale updates.

What to say in the interviewAbout 90 seconds aloud

Open with

Those records were never written. The log line means they sat in the producer's accumulator and retries for longer than delivery.timeout.ms, which is two minutes by default, so the producer gave up on the batch and completed each record's future with a TimeoutException.

Then, if they want more

The code calls kafkaTemplate.send and ignores the CompletableFuture it returns, so by the time the failure arrived the request had already succeeded and nobody was listening. The service thinks it published those order events, and downstream services never saw them.

The first fix is to handle the result: log the failure with the event id, count it, and alert on it. But that only makes the loss visible.

To actually not lose events, I would either fail the business operation when the publish fails, so the caller retries, or better, use a transactional outbox: write the event into a table in the same database transaction as the order and have a relay publish it and mark it sent after the acknowledgement.

I could also raise delivery.timeout.ms and buffer.memory to ride out short partitions, but anything held in producer memory is lost if the pod restarts. Finally, resending later can put an old event behind a newer one for the same key, so I would put a version on each event and have consumers ignore stale ones.

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 "The team's code calls kafkaTemplate.send() and never looks at the result. What happened...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-clients-in-depth/942e1bd8be/