การจัดเป็นชุด การบีบอัด และการปรับค่า Linger
เรียนรู้ว่าการจัดข้อความเป็นชุดของผู้ผลิต linger.ms และการบีบอัดทำงานร่วมกันอย่างไร เพื่อแลกเปลี่ยนเวลาแฝงกับปริมาณงาน และลดการใช้เครือข่ายกับดิสก์
การจัดเป็นชุด การบีบอัด และการปรับค่า Linger เป็นบทเรียน Apache Kafka & Stream Processing Fundamentals ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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.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.
คำถามที่พบบ่อย
บทเรียน “การจัดเป็นชุด การบีบอัด และการปรับค่า Linger” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การจัดเป็นชุด การบีบอัด และการปรับค่า Linger” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Apache Kafka & Stream Processing Fundamentals ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Apache Kafka & Stream Processing Fundamentals มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การจัดเป็นชุด การบีบอัด และการปรับค่า Linger”
เรียนรู้ว่าการจัดข้อความเป็นชุดของผู้ผลิต linger.ms และการบีบอัดทำงานร่วมกันอย่างไร เพื่อแลกเปลี่ยนเวลาแฝงกับปริมาณงาน และลดการใช้เครือข่ายกับดิสก์ คุณปฏิบัติ Apache Kafka & Stream Processing Fundamentals ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Apache Kafka & Stream Processing Fundamentals หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Apache Kafka & Stream Processing Fundamentals บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การจัดเป็นชุด การบีบอัด และการปรับค่า Linger” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Apache Kafka & Stream Processing Fundamentals นี้ได้ไหม
ได้ บทเรียน Apache Kafka & Stream Processing Fundamentals ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ประสิทธิภาพของผู้ผลิตและผู้ใช้
- การกำหนดค่าและการปรับแต่งโบรกเกอร์
- การเพิ่มประสิทธิภาพดิสก์ I/O และเครือข่าย
- การจัดเป็นชุด การบีบอัด และการปรับค่า Linger