Consumo in batch e modalità di acknowledgment
Impari a consumare i record Kafka in batch e a scegliere la modalità di acknowledgment appropriata per bilanciare throughput, latenza e garanzie di consegna.
Consumo in batch e modalità di acknowledgment è una lezione Advanced Spring Boot 4: Event-Driven Architecture (Kafka) gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Advanced Spring Boot 4: Event-Driven Architecture (Kafka), e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Advanced Spring Boot 4: Event-Driven Architecture (Kafka) include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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: batchA 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: 500Acknowledgment 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: batchto receive a List of records. - Tune
max.poll.recordsfor batch size. - Choose an
AckModeto control offset commits. - Acknowledge after processing and keep writes idempotent.
Domande Frequenti
La lezione «Consumo in batch e modalità di acknowledgment» è gratuita?
Sì — il testo completo di «Consumo in batch e modalità di acknowledgment» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Advanced Spring Boot 4: Event-Driven Architecture (Kafka), passa a CoddyKit PRO. Il corso Advanced Spring Boot 4: Event-Driven Architecture (Kafka) include 4 lezioni in totale.
Cosa imparerò in «Consumo in batch e modalità di acknowledgment»?
Impari a consumare i record Kafka in batch e a scegliere la modalità di acknowledgment appropriata per bilanciare throughput, latenza e garanzie di consegna. Eserciti Advanced Spring Boot 4: Event-Driven Architecture (Kafka) con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Advanced Spring Boot 4: Event-Driven Architecture (Kafka)?
Non è richiesta alcuna esperienza precedente. Advanced Spring Boot 4: Event-Driven Architecture (Kafka) su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Consumo in batch e modalità di acknowledgment»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Advanced Spring Boot 4: Event-Driven Architecture (Kafka)?
Sì. Ogni lezione Advanced Spring Boot 4: Event-Driven Architecture (Kafka) include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Creare container listener Kafka
- Gestione dei gruppi di consumatori
- Deserializzazione e conversione dei messaggi
- Consumo in batch e modalità di acknowledgment