0Pricing
Apache Kafka & Stream Processing Fundamentals · 课时

批处理、压缩与 Linger 调优

学习生产者批处理、linger.ms 和压缩如何相互作用,在延迟与吞吐量之间进行权衡,并减少网络和磁盘使用。

批处理、压缩与 Linger 调优 是 CoddyKit 上的免费 Apache Kafka & Stream Processing Fundamentals 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Apache Kafka & Stream Processing Fundamentals 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Apache Kafka & Stream Processing Fundamentals 课程共包含 4 节课。

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

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.ms elapses, 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.ms and 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.ms control batch size and the latency trade-off.
  • Compression (lz4/zstd) saves network, disk, and replication bandwidth.
  • buffer.memory bounds in-flight data.
  • Verify with batch-size-avg and compression-rate-avg metrics.

常见问题解答

「批处理、压缩与 Linger 调优」课时是免费的吗?

是的 — 「批处理、压缩与 Linger 调优」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Apache Kafka & Stream Processing Fundamentals 课程的其余内容,请升级到 CoddyKit PRO。 Apache Kafka & Stream Processing Fundamentals 课程共包含 4 节课。

「批处理、压缩与 Linger 调优」这节课中我会学到什么?

学习生产者批处理、linger.ms 和压缩如何相互作用,在延迟与吞吐量之间进行权衡,并减少网络和磁盘使用。 你通过在浏览器中直接运行的动手代码来练习 Apache Kafka & Stream Processing Fundamentals,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Apache Kafka & Stream Processing Fundamentals 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Apache Kafka & Stream Processing Fundamentals 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「批处理、压缩与 Linger 调优」课时需要多长时间?

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

我能在这节 Apache Kafka & Stream Processing Fundamentals 课中编写并运行代码吗?

能。每节 Apache Kafka & Stream Processing Fundamentals 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 生产者与消费者性能
  2. 代理配置与调优
  3. 磁盘 I/O 与网络优化
  4. 批处理、压缩与 Linger 调优
← 返回 Apache Kafka & Stream Processing Fundamentals