Agrupación, compresión y ajuste de linger
Aprenda cómo interactúan la agrupación de mensajes del productor, linger.ms y la compresión para intercambiar latencia por rendimiento y reducir el uso de red y disco.
Agrupación, compresión y ajuste de linger es una lección gratuita de Apache Kafka & Stream Processing Fundamentals en CoddyKit. Esta es la lección 4 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Apache Kafka & Stream Processing Fundamentals, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Apache Kafka & Stream Processing Fundamentals incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
The Throughput Lever
Sending one record per network call is slow. Kafka producers batch records per partition before sending, amortizing overhead and dramatically raising throughput.
batch.size
batch.size sets the maximum size (in bytes) of a single batch per partition.
- Larger batches = better throughput, more memory.
- A batch is sent when it fills or when linger expires.
// 64 KB batches
props.put(ProducerConfig.BATCH_SIZE_CONFIG, 65536);linger.ms
linger.ms tells the producer to wait a short time for more records before sending, even if the batch isn't full.
0(default) = send as soon as possible, lowest latency.- A few ms = bigger batches, higher throughput.
props.put(ProducerConfig.LINGER_MS_CONFIG, 10);How They Interact
Think of it as fill or wait:
- The batch ships when it reaches
batch.size. - Or when
linger.mselapses, whichever comes first.
Raising both increases batch sizes at the cost of latency.
Compression Basics
compression.type compresses each batch before it goes over the wire and to disk.
Bigger batches compress better, so compression pairs naturally with linger tuning.
props.put(ProducerConfig.COMPRESSION_TYPE_CONFIG, "lz4");Choosing a Codec
Common codecs and their trade-offs:
- lz4 — fast, good ratio, popular default.
- snappy — very fast, lower ratio.
- zstd — best ratio, slightly more CPU.
- gzip — high ratio, higher CPU.
End-to-End Compression
Kafka stores batches compressed and serves them compressed; consumers decompress.
This means compression saves network, disk, and replication bandwidth — not just the producer link.
buffer.memory
buffer.memory caps total memory for unsent records.
If batches accumulate faster than they ship, the producer blocks (up to max.block.ms) — a sign brokers can't keep up or batching is too aggressive.
props.put(ProducerConfig.BUFFER_MEMORY_CONFIG, 33554432);A High-Throughput Profile
A typical throughput-oriented configuration:
props.put(ProducerConfig.BATCH_SIZE_CONFIG, 131072);
props.put(ProducerConfig.LINGER_MS_CONFIG, 20);
props.put(ProducerConfig.COMPRESSION_TYPE_CONFIG, "zstd");
props.put(ProducerConfig.ACKS_CONFIG, "all");Latency vs. Throughput
There is no free lunch:
- Lower
linger.msand smaller batches = lower latency. - Higher values + compression = higher throughput, more latency.
Tune to your SLA, then measure.
Measuring the Effect
Watch producer JMX metrics to confirm tuning works:
batch-size-avg— are batches actually larger?compression-rate-avg— how well does data compress?record-queue-time-avg— added latency from linger.
Quick Check
Test your understanding of batching and linger.
Recap
You learned producer batching and compression tuning.
batch.size+linger.mscontrol batch size and the latency trade-off.- Compression (lz4/zstd) saves network, disk, and replication bandwidth.
buffer.memorybounds in-flight data.- Verify with batch-size-avg and compression-rate-avg metrics.
Preguntas frecuentes
¿La lección «Agrupación, compresión y ajuste de linger» es gratis?
Sí — el texto completo de «Agrupación, compresión y ajuste de linger» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Apache Kafka & Stream Processing Fundamentals, actualiza a CoddyKit PRO. El curso de Apache Kafka & Stream Processing Fundamentals incluye 4 lecciones en total.
¿Qué aprenderé en «Agrupación, compresión y ajuste de linger»?
Aprenda cómo interactúan la agrupación de mensajes del productor, linger.ms y la compresión para intercambiar latencia por rendimiento y reducir el uso de red y disco. Practicas Apache Kafka & Stream Processing Fundamentals con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar Apache Kafka & Stream Processing Fundamentals?
No se requiere experiencia previa. Apache Kafka & Stream Processing Fundamentals en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 4 de 4.
¿Cuánto tiempo toma la lección «Agrupación, compresión y ajuste de linger»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de Apache Kafka & Stream Processing Fundamentals?
Sí. Cada lección de Apache Kafka & Stream Processing Fundamentals incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Rendimiento de productores y consumidores
- Configuración y ajuste de brokers
- Optimización de E/S de disco y red
- Agrupación, compresión y ajuste de linger