Video in progress

Strong Middle 📨 Kafka and messaging

The producer log shows `TimeoutException: Expiring 15 record(s) ... has passed since batch creation`. What is it, and which settings are involved?

The batch wasn't delivered to the broker within delivery.timeout.ms (2 minutes by default) and the producer dropped it. It shows up as an error in the send() callback; if you don't check it, the message silently disappears. The cause is usually on the broker or network side.

📖 Answer

The producer has several time settings, nested inside one another:

delivery.timeout.ms (120 s)  >=  linger.ms + request.timeout.ms (30 s)
  └─ within this time the producer retries `retries` (default: practically unlimited) times

So in a modern client you don't need to set retries by hand: the attempts are in practice limited by delivery.timeout.ms. It's the total time from the send() call to the final response.

When this error shows up, here's what I check:

  • is there a partition leader: a dead broker, a drawn-out election, or writes being rejected because of NotEnoughReplicas;
  • the network: packet loss between producer and broker, DNS;
  • an overloaded broker: request-latency-avg is high, the broker's disk or CPU is saturated;
  • quotas: the broker is throttling (produce-throttle-time-avg).

Two things are mandatory on the application side:

kafkaTemplate.send("orders", key, event).whenComplete((r, ex) -> {
    if (ex != null) {
        metrics.counter("kafka.send.failed").increment();
        fallback.saveForRetry(event);    // for example, to an outbox table
    }
});

Increasing delivery.timeout.ms only hides temporary outages and fills the buffer. For important events a better way is an outbox: the message goes to the DB first, and delivery to Kafka can be retried later.