Discard Strategy

Purpose

Within Kafka consumer error handling, the Discard Strategy addresses the challenge of dealing with problematic messages that cause polling exceptions. Instead of retrying or stopping the consumer, this strategy opts to **skip over the faulty message** by advancing the consumer's offset to the next message. This prevents the consumer from getting stuck on poison messages and ensures continued consumption without manual intervention.

Functionality

The Discard Strategy implements the `PollExceptionStrategy` interface, providing explicit behavior when a poll exception occurs:

This behavior relies on a utility method (`SeekUtil.seekToNextOffset`) that performs the offset seeking on the Kafka consumer instance.

Key Method Snippet

@Override
public void handle(long partitionLastOffset, Exception exception) {
    LOG.warn("Requesting the consumer to discard the message and continue to the next based on polling exception strategy");

    // skip this poison message and seek to the next message
    SeekUtil.seekToNextOffset(consumer, partitionLastOffset);
}

@Override
public boolean canContinue() {
    return true;
}

Integration

The Discard Strategy is one of several poll exception strategies integrated into the Kafka consumer component’s error handling mechanism. When a poll exception occurs, the consumer delegates to the configured `PollExceptionStrategy` instance, which determines how to proceed.

Diagram

flowchart TD
    PollKafka[Poll Kafka Broker] -->|Exception Occurs| HandleError[DiscardErrorStrategy.handle()]
    HandleError --> SeekNext[Seek to Next Offset]
    SeekNext --> Continue[Continue Consuming]
    Continue --> PollKafka

This flowchart illustrates the core process of the Discard Strategy during Kafka polling exceptions: upon error, the consumer seeks to the next offset and resumes normal polling without stopping or retrying the failed message.