By default, the Kafka consumer will use auto commit, where the offset will be committed automatically in the background using a given interval.1
If you want to control exactly when the offset is committed, Camel’s manual commit API lets you do that. In this guide I explore the right places to manually commit Kafka offsets, and how to add a Dead Letter Queue (DLQ) for records that fail processing.
Enabling manual commits#
The manual commit API is not enabled by default and requires the following properties:
camel.component.kafka.auto-commit-enable=false
camel.component.kafka.allow-manual-commit=true
The Kafka consumer periodically fetches a batch of records and feeds them to Camel one by one. If an exception is thrown while processing the current record, breakOnFirstError tells Camel to stop processing the remaining records in the batch.
camel.component.kafka.break-on-first-error=true
When should you commit offsets?#
The following route consumes Kafka records from an input topic and forwards them to an output topic. The offset is only committed once a record has been fully processed and written to the output topic, so the next record is only picked up after that.
@Component
public class KafkaRouteBuilder extends RouteBuilder {
@Override
public void configure() {
from("kafka:input")
.routePolicy(new SuspendRoutePolicy())
.process(exchange -> {
// Do work
})
.to("kafka:output")
.process(exchange -> {
// Output record has been successfully written to the output topic
KafkaManualCommit commit = exchange.getIn()
.getHeader(KafkaConstants.MANUAL_COMMIT, KafkaManualCommit.class);
commit.commit();
});
}
}
The SuspendRoutePolicy route policy is required to prevent the Kafka consumer from endlessly retrying the same failed record: instead of retrying, it pauses the consumer.
public class SuspendRoutePolicy extends RoutePolicySupport {
@Override
public void onExchangeDone(Route route, Exchange exchange) {
if (exchange.isFailed()) {
try {
suspendRoute(route); // Synchronously stop consuming records
} catch (Exception e) {
handleException(e);
}
}
}
}
Adding a Dead Letter Queue for failed records#
If an exception is thrown along the route, the onException pipeline takes over and routes the failed record to a DLQ instead of losing it.
@Component
public class KafkaRouteBuilder extends RouteBuilder {
@Override
public void configure() {
onException(Exception.class)
.handled(true)
.useOriginalMessage()
.maximumRedeliveries(0)
.to("kafka:dlq")
.process(exchange -> {
KafkaManualCommit commit = exchange.getIn()
.getHeader(KafkaConstants.MANUAL_COMMIT, KafkaManualCommit.class);
commit.commit();
});
from("kafka:input")
.routePolicy(new SuspendRoutePolicy())
.process(exchange -> {
// Do work
})
.to("kafka:output")
.process(exchange -> {
KafkaManualCommit commit = exchange.getIn()
.getHeader(KafkaConstants.MANUAL_COMMIT, KafkaManualCommit.class);
commit.commit();
});
}
}
Once a record lands in onException, its offset is committed right after the write to the DLQ topic succeeds, so the consumer moves on to the next record instead of retrying the failed one forever.
There is one edge case left: if the write to kafka:dlq itself fails, there is no further exception handler to catch it and onException cannot be re-entered. The record is then marked as failed and SuspendRoutePolicy pauses the consumer, the same way it would for an unhandled exception on the main route.