spring-cloud-stream icon indicating copy to clipboard operation
spring-cloud-stream copied to clipboard

Clarify support for manual ack/nack with batch mode + DLQ; potential bug in acknowledge(index) path

Open ferblaca opened this issue 8 months ago • 3 comments

Describe the issue We found potential issues when using Spring Cloud Stream Kafka Binder with batch consumers + DLQ + manual acknowledgments.

According to the docs, in batch mode with DLQ enabled, all records from the previous poll are sent to DLQ. That matches current behavior.
However, in manual-ack scenarios we observed:

  1. A likely bug with partial commits using acknowledge(index) + DLQ.
  2. A general improvement opportunity: DLQ handling appears to ignore already acknowledged/committed progress and republishes records that were already processed successfully.

Repro project (public): https://github.com/ferblaca/demoSCSKafka/tree/batch-consumer-dlq

To Reproduce

  1. Clone and open the repro project:
git clone -b batch-consumer-dlq https://github.com/ferblaca/demoSCSKafka.git
cd demoSCSKafka
  1. Start Kafka/Zookeeper/Kafka UI using the provided compose file:
docker compose -f src/main/resources/compose/docker-compose.yml up -d
  1. In src/main/resources/application.yml, set one function definition at a time:

    • batchConsumerAckManual (scenario A)
    • batchConsumerNackManual (scenario B)
  2. Run the application.

  3. Produce 100 records to input-topic-batch (the project includes the producer binding foo-out-0 to that topic, per config).

  4. In the batch consumer logic, force an exception at index 20.

  5. Inspect topic/DLQ behavior in Kafka UI:

    • http://localhost:9090

Scenario A: acknowledge(index) (partial commit) + DLQ

Observed:

  • After calling acknowledgment.acknowledge(19), the internal partial state is set (partial=19).
  • During DLQ handling for the failed batch, acknowledgments continue over the full polled batch.
  • The failure starts when processing approximately record 80/100, because the effective index reaches 100 (80 + partial(19) + 1 = 100), which exceeds the batch bounds.
  • Then it throws: IllegalArgumentException: index (100) is out of range (0-99)
  • The batch is retried multiple times.
  • Final DLQ count becomes much larger than input (observed: 810 (10 retries x 81 messages) DLQ records for 100 input records).

Consumer behavior:

  • Partial ack every 20 records: acknowledgment.acknowledge(i)
  • Forced exception at i == 20

Observed:

  • After partial ack (acknowledge(19)), internal partial state is set.
  • During DLQ flow for the failed batch, an index overflow occurs: IllegalArgumentException: index (100) is out of range (0-99)
  • Batch is retried multiple times.
  • Final DLQ count is much larger than input (observed: 810 DLQ records for 100 input records).

This looks like a bug in the interaction between partial batch ack state and DLQ processing:

Caused by: java.lang.IllegalArgumentException: index (100) is out of range (0-99)

Scenario B: nack(index, Duration) + DLQ

Same batch/DLQ/manual setup, but using:

  • acknowledgment.nack(i, Duration.ofMillis(100)) on failure at index 20

Observed:

  • No index out-of-range exception in this path.
  • But DLQ still receives records in repeated waves as re-seek/retry progresses.
  • Final DLQ count is also amplified (observed: 280 DLQ records for 100 input records).

Expected behavior

  1. Clarify/document whether manual ack (acknowledge/nack) is officially supported with DLQ in batch mode.
  2. If supported, fix the apparent bug for acknowledge(index) + DLQ (index out-of-range).
  3. Improve DLQ error handling so that records already acknowledged/committed are not resent to DLQ (or provide a configurable strategy), reducing duplicate DLQ traffic and unnecessary reprocessing.

Additional context

  • We understand Kafka processing is at-least-once and consumers must handle duplicates.
  • Even so, current behavior appears to produce avoidable DLQ amplification in manual-ack batch use cases.
  • We also ask for guidance on recommended patterns when using:
    • acknowledge(index)
    • nack(index, Duration)
    • BatchListenerFailedException(index) together with DLQ in batch mode.

Version of the framework

  • Spring Cloud: 2025.1.1
  • Spring Boot: 4.0.2

ferblaca avatar Mar 23 '26 17:03 ferblaca

The DLQ amplification here is especially interesting — particularly the case where already-acknowledged records get republished and 100 input records can turn into hundreds of DLQ records through retries.

We’ve been working on this broader recovery problem with ReplayGuard: making reprocessing an explicit, scoped operation rather than allowing recovery to unintentionally replay more data than intended.

Our workflow focuses on selecting exactly what should be recovered, validating the scope before execution, controlled redrive/replay, rate controls, pause/resume/stop, and auditability.

This issue is obviously about framework-level DLQ semantics, which ReplayGuard doesn’t replace, but the operator impact you described is very close to the problem we’re addressing.

If you’re open to it, I’d value a short discussion about how you currently handle recovery once records reach the DLQ.

https://replayguard.in

ravithejatech714-create avatar Aug 28 '26 09:08 ravithejatech714-create

I investigated this against the current Spring Cloud Stream main branch. The reported out-of-range exception appears to have a Spring Kafka root cause: after acknowledge(19), the older batch Acknowledgment.acknowledge() implementation advanced only to the next index. Spring Kafka addressed that in GH-4726, integrated as bc143ec on September 29. That commit came after the v4.2.0-M2 tag, and I have not yet verified the full binder reproducer with a Spring Kafka build containing the fix.

There seems to be a separate binder question. In batch mode, getErrorMessageHandler iterates over every source record for DLQ publishing, and DlqSender.sendToDlq calls acknowledge() in finally for each record when ack mode is manual. With the upstream fix, repeated calls should no longer overflow, but the first call can commit the remainder before later DLQ sends finish. The binder also sends the already-partially-acknowledged prefix to the DLQ; that matches the current batch-DLQ documentation, so I do not want to change that policy without your direction.

Would you welcome a bounded Spring Cloud Stream PR that makes batch DLQ acknowledgment happen once after processing the batch, with regression coverage for manual and manual-immediate acknowledgment (including a prior partial acknowledge(index))? Should this preserve the documented “entire polled batch to DLQ” behavior, or do you want the already-acknowledged prefix excluded? The latter likely needs an explicit way to identify committed progress; the binder cannot infer it reliably from the Acknowledgment interface alone. I can reproduce the accepted scope against the appropriate Spring Kafka version before implementing.

sudhanshubliz avatar Oct 07 '26 14:10 sudhanshubliz

Update: validation with newer dependencies

I retested the reproducer with Spring Boot 4.1.2 and Spring Cloud 2025.1.4. Maven resolves Spring Cloud Stream 5.0.4 and Spring Kafka 4.1.2. With a clean Kafka broker, the manual-ack batch consumer partially acknowledges the batch and then fails at index 20. The run completed without the reported IllegalArgumentException: index (100) is out of range; 100 input records resulted in 100 DLQ records.

Spring Kafka 4.1.2 includes the fix for GH-4726, which makes acknowledge() commit the remainder after a partial batch acknowledgment. This appears to resolve the index-overflow failure in this reproducer.

The binder still sends the entire polled batch to the DLQ, including the partially acknowledged prefix. That matches the current batch-DLQ documentation, so this test does not establish that acknowledged records should be excluded. I have not retested the nack(index, Duration) scenario.

Note: the Spring Cloud 2025.1.4 BOM declares Spring Boot 4.0.9 as its associated version; the Boot 4.1.2 / Cloud 2025.1.4 combination tested here is outside that declared pairing.

Could you confirm whether the index-overflow issue can be considered resolved by the Spring Kafka fix, with the full-batch DLQ behavior remaining as documented, or whether you would prefer to track any remaining binder-specific concern separately?

ferblaca avatar Oct 28 '26 17:10 ferblaca