0Pricing
Advanced Spring Boot 4: Event-Driven Architecture (Kafka) · Lesson

Batch Consumption and Acknowledgment Modes

Learn to consume Kafka records in batches and choose the right acknowledgment mode to balance throughput, latency, and delivery guarantees.

Batch Consumption and Acknowledgment Modes is a free Advanced Spring Boot 4: Event-Driven Architecture (Kafka) lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Advanced Spring Boot 4: Event-Driven Architecture (Kafka) learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Batch Consumption

By default a @KafkaListener processes one record at a time. For high-throughput pipelines, processing records in batches reduces overhead and lets you bulk-write to databases.

Enabling Batch Mode

Set batch-listener to true on the container factory, or configure it in properties.

spring:
  kafka:
    listener:
      type: batch

A Batch Listener Method

When batch mode is on, your listener accepts a List of payloads instead of a single value.

@KafkaListener(topics = "orders")
public void handle(List<Order> orders) {
    log.info("Received batch of {}", orders.size());
    orderRepository.saveAll(orders);
}

Accessing Batch Metadata

You can also receive the full list of ConsumerRecord objects to read keys, partitions, and offsets per record.

@KafkaListener(topics = "orders")
public void handle(List<ConsumerRecord<String,Order>> records) {
    for (var r : records) {
        process(r.value(), r.partition(), r.offset());
    }
}

Controlling Batch Size

The batch size is bounded by max.poll.records. Tune it to control how many records each poll fetches.

spring:
  kafka:
    consumer:
      max-poll-records: 500

Acknowledgment Modes Overview

Spring Kafka offers several AckMode values:

  • BATCH — commit offsets after the whole batch.
  • RECORD — commit after each record.
  • MANUAL / MANUAL_IMMEDIATE — you call ack explicitly.

Setting AckMode

Configure the ack mode on the listener container factory.

factory.getContainerProperties()
    .setAckMode(ContainerProperties.AckMode.BATCH);

Manual Acknowledgment in Batches

With MANUAL mode, inject an Acknowledgment and call acknowledge() only after successful processing of the batch.

@KafkaListener(topics = "orders")
public void handle(List<Order> orders, Acknowledgment ack) {
    orderRepository.saveAll(orders);
    ack.acknowledge();
}

Delivery Guarantees

Acknowledging after processing gives at-least-once delivery: if the app crashes mid-batch, the batch is redelivered.

Make your batch processing idempotent so reprocessing is safe.

Throughput vs Latency

Larger batches improve throughput but increase per-message latency and memory use. Start with a moderate max.poll.records and measure.

Putting It Together

Batch consumption plus the right ack mode lets you build efficient, reliable consumers. Combine type: batch, a tuned poll size, manual ack, and idempotent writes.

Quick Check

Test your understanding of batch consumption.

Recap

You learned batch consumption and acknowledgment modes.

  • Set type: batch to receive a List of records.
  • Tune max.poll.records for batch size.
  • Choose an AckMode to control offset commits.
  • Acknowledge after processing and keep writes idempotent.

Frequently asked questions

Is the “Batch Consumption and Acknowledgment Modes” lesson free?

Yes — the full text of “Batch Consumption and Acknowledgment Modes” is free to read here on the web, and the Advanced Spring Boot 4: Event-Driven Architecture (Kafka) course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Advanced Spring Boot 4: Event-Driven Architecture (Kafka) course, upgrade to CoddyKit PRO.

What will I learn in “Batch Consumption and Acknowledgment Modes”?

Learn to consume Kafka records in batches and choose the right acknowledgment mode to balance throughput, latency, and delivery guarantees. You practise Advanced Spring Boot 4: Event-Driven Architecture (Kafka) with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Advanced Spring Boot 4: Event-Driven Architecture (Kafka)?

No prior experience is required. Advanced Spring Boot 4: Event-Driven Architecture (Kafka) on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Batch Consumption and Acknowledgment Modes” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Advanced Spring Boot 4: Event-Driven Architecture (Kafka) lesson?

Yes. Every Advanced Spring Boot 4: Event-Driven Architecture (Kafka) lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Building Kafka Listener Containers
  2. Consumer Group Management
  3. Deserialization and Message Conversion
  4. Batch Consumption and Acknowledgment Modes
← Back to Advanced Spring Boot 4: Event-Driven Architecture (Kafka)