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

批量消费与确认模式

学习批量消费 Kafka 记录,并选择合适的确认模式,在吞吐量、延迟和交付保证之间取得平衡。

批量消费与确认模式 是 CoddyKit 上的免费 Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

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.

常见问题解答

「批量消费与确认模式」课时是免费的吗?

是的 — 「批量消费与确认模式」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 课程的其余内容,请升级到 CoddyKit PRO。 Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 课程共包含 4 节课。

「批量消费与确认模式」这节课中我会学到什么?

学习批量消费 Kafka 记录,并选择合适的确认模式,在吞吐量、延迟和交付保证之间取得平衡。 你通过在浏览器中直接运行的动手代码来练习 Advanced Spring Boot 4: Event-Driven Architecture (Kafka),全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「批量消费与确认模式」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 课中编写并运行代码吗?

能。每节 Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 构建 Kafka 监听器容器
  2. 消费者组管理
  3. 反序列化与消息转换
  4. 批量消费与确认模式
← 返回 Advanced Spring Boot 4: Event-Driven Architecture (Kafka)