0
0
Kafkadevops~10 mins

Batching and linger configuration in Kafka - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Batching and linger configuration
Message arrives to producer
Add message to batch buffer
Is batch size reached?
NoWait linger.ms time
Send batch
Send batch
Clear batch buffer
Wait for next message
Messages are collected in a batch buffer. When the batch size is reached or linger time expires, the batch is sent.
Execution Sample
Kafka
producer_config = {
  'batch.size': 16384,  # bytes
  'linger.ms': 5       # milliseconds
}

# Messages arrive and are batched before sending
This config sets batch size and linger time to control when messages are sent in batches.
Process Table
StepMessage ArrivedBatch Size (bytes)Batch Full?Linger Timer (ms)ActionOutput
1Msg1 (4000 bytes)4000NoStart 5ms timerAdd to batchNo send
2Msg2 (5000 bytes)9000NoTimer runningAdd to batchNo send
3Msg3 (8000 bytes)17000Yes (17000 > 16384)Cancel timerSend batchBatch sent with Msg1, Msg2, Msg3
4Msg4 (3000 bytes)3000NoStart 5ms timerAdd to batchNo send
5No new message3000NoTimer expiresSend batchBatch sent with Msg4
6-0--Batch clearedReady for new messages
💡 Batch sent when size limit reached or linger timer expires
Status Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
Batch Size (bytes)04000900017000300000
Linger Timer (ms)Off5ms started5ms runningCancelled5ms startedExpiredOff
Batch Content[][Msg1][Msg1, Msg2][Msg1, Msg2, Msg3][Msg4][][]
Key Moments - 3 Insights
Why does the batch send immediately at step 3 instead of waiting for the linger timer?
Because the batch size exceeded the configured limit (17000 > 16384 bytes), the batch sends immediately, cancelling the linger timer as shown in step 3 of the execution_table.
What happens if no new messages arrive before the linger timer expires?
The linger timer triggers sending the current batch even if it is not full, as shown at step 5 where the timer expires and the batch with Msg4 is sent.
Does the linger timer start again after sending a batch?
No, after sending a batch, the batch buffer clears and the linger timer is off until a new message arrives, starting a new timer as shown at step 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the batch size after step 2?
A4000 bytes
B17000 bytes
C9000 bytes
D3000 bytes
💡 Hint
Check the 'Batch Size (bytes)' column at step 2 in the execution_table.
At which step does the linger timer get cancelled due to batch size?
AStep 3
BStep 1
CStep 2
DStep 5
💡 Hint
Look for 'Cancel timer' action in the execution_table.
If the linger.ms was set to 0, what would happen to the batch sending behavior?
ABatches would send immediately when batch size is reached only.
BBatches would send immediately after every message without waiting.
CBatches would never send.
DBatches would send only when linger timer expires.
💡 Hint
Consider how linger.ms controls waiting time before sending batches.
Concept Snapshot
Batching collects messages up to batch.size bytes.
linger.ms sets max wait time before sending batch.
Batch sends when size reached or linger expires.
Helps improve throughput by sending fewer larger requests.
Configurable in Kafka producer settings.
Full Transcript
This visual execution shows how Kafka producer batches messages before sending. Messages add to a batch buffer. If the batch size exceeds the configured batch.size, the batch sends immediately, cancelling any linger timer. If the batch size is not reached, the linger.ms timer starts on the first message and waits that time before sending the batch. This ensures messages are sent timely even if batch size is small. Variables tracked include batch size in bytes, linger timer state, and batch content. Key moments clarify why batches send immediately on size limit and how linger timer triggers sending when no new messages arrive. The quiz tests understanding of batch size changes, timer cancellation, and effects of linger.ms settings. This helps beginners see how batching and linger configuration control message sending in Kafka producers.