0Pricing
Advanced Spring Boot 4: Event-Driven Architecture (Kafka) · บทเรียน

การรับข้อมูลเป็นชุดและโหมดการยืนยัน

เรียนรู้การรับเรกคอร์ด Kafka เป็นชุด และเลือกโหมดการยืนยันที่เหมาะสมเพื่อสร้างสมดุลระหว่างปริมาณงาน เวลาแฝง และการรับประกันการส่ง

การรับข้อมูลเป็นชุดและโหมดการยืนยัน เป็นบทเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส 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 ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 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)