Search code examples
javaapache-kafkaibm-cloudmessage-hub

When does the Apache Kafka client throw a "Batch Expired" exception?


Using the Apache Kafka Java client (0.9), I'm trying to send a long series of records to the broker using the Kafka Producer class.

The asynchronous send method returns immediately for a while, then starts blocking on each call for a short time period. After around thirty seconds, the client starts throwing exceptions (TimeoutException), with the message "Batch expired".

What circumstances cause this exception to be thrown?


Solution

  • This exception indicates you are queueing records at a faster rate than they can be sent.

    When you call the send method, the ProducerRecord will be stored in an internal buffer for sending to the broker. The method returns immediately once the ProducerRecord has been buffered, regardless of whether it has been sent.

    Records are grouped into batches for sending to the broker, to reduce the transport overheard per message and increase throughput.

    Once a record is added a batch, there is a time limit for sending that batch to ensure it has been sent within a specified duration. This is controlled by the Producer configuration parameter, request.timeout.ms, which defaults to thirty seconds.

    If the batch has been queued longer than the timeout limit, the exception will be thrown. Records in that batch will be removed from the send queue.

    Increasing the timeout limit, using the configuration parameter, will allow the client to queue batches for longer before expiring.