Bird
Raised Fist0
Kafkadevops~5 mins

Batching and linger configuration in Kafka - Commands & Configuration

Choose your learning style10 modes available
Introduction
When sending messages to Kafka, batching groups multiple messages to send together, improving speed. Linger time sets how long to wait before sending a batch, allowing more messages to join the batch and reduce network calls.
When you want to improve Kafka producer throughput by sending messages in groups instead of one by one
When your application sends many small messages rapidly and you want to reduce network overhead
When you want to balance latency and throughput by controlling how long the producer waits before sending messages
When you want to optimize resource use on the Kafka broker by reducing the number of requests
When tuning Kafka producer performance for better efficiency in a high-load environment
Config File - producer.properties
producer.properties
bootstrap.servers=localhost:9092
key.serializer=org.apache.kafka.common.serialization.StringSerializer
value.serializer=org.apache.kafka.common.serialization.StringSerializer
batch.size=16384
linger.ms=10

bootstrap.servers: Kafka broker address to connect to.

key.serializer and value.serializer: Define how keys and values are converted to bytes.

batch.size: Maximum size in bytes for a batch before sending.

linger.ms: Time in milliseconds to wait for more messages before sending the batch.

Commands
Starts a Kafka producer using the configuration file with batching and linger settings to send messages to 'example-topic'.
Terminal
kafka-console-producer --broker-list localhost:9092 --topic example-topic --producer.config producer.properties
Expected OutputExpected
> This is a test message > Another message
→
--broker-list - Specifies the Kafka broker address to connect to
→
--topic - Specifies the topic to send messages to
→
--producer.config - Loads producer configuration including batching and linger settings
Reads the first two messages from 'example-topic' to verify that messages sent with batching and linger settings are received correctly.
Terminal
kafka-console-consumer --bootstrap-server localhost:9092 --topic example-topic --from-beginning --max-messages 2
Expected OutputExpected
This is a test message Another message
→
--bootstrap-server - Specifies the Kafka broker address to connect to
→
--topic - Specifies the topic to read messages from
→
--from-beginning - Reads messages from the start of the topic
→
--max-messages - Limits the number of messages to read
Key Concept

If you remember nothing else from this pattern, remember: batching groups messages to send together and linger.ms sets how long to wait to form a batch, balancing speed and latency.

Common Mistakes
Setting linger.ms to 0 and expecting batching to improve throughput
With linger.ms at 0, the producer sends messages immediately without waiting to batch, reducing batching benefits.
Set linger.ms to a small positive value like 10 ms to allow messages to accumulate before sending.
Setting batch.size too small causing frequent sends
Small batch size limits how many messages can be grouped, increasing network calls and reducing throughput.
Use a reasonable batch.size like 16384 bytes to allow efficient batching.
Summary
Configure batch.size and linger.ms in the Kafka producer properties to control message batching.
Use kafka-console-producer with the config file to send messages with batching and linger settings.
Verify messages are received correctly using kafka-console-consumer reading from the topic.

Practice

(1/5)
1. What does the linger.ms configuration control in Kafka producer settings?
easy
A. The maximum size of a message batch in bytes
B. The maximum time to wait before sending a batch of messages
C. The number of retries for sending a message
D. The timeout for consumer group rebalancing

Solution

  1. Step 1: Understand the role of linger.ms

    The linger.ms setting defines how long the producer waits before sending a batch, allowing more messages to accumulate.
  2. Step 2: Differentiate from other settings

    Other options like batch size control bytes, retries control attempts, and consumer timeout are unrelated to linger time.
  3. Final Answer:

    The maximum time to wait before sending a batch of messages -> Option B
  4. Quick Check:

    linger.ms = wait time before batch send [OK]
Hint: linger.ms sets wait time before sending batch [OK]
Common Mistakes:
  • Confusing linger.ms with batch size
  • Thinking linger.ms controls retries
  • Mixing producer and consumer settings
2. Which of the following is the correct way to set the batch size to 32 KB in Kafka producer configuration?
easy
A. "batch.size": 32768
B. "batch.size": "32KB"
C. "batch.size": 32000
D. "batch.size": "32kb"

Solution

  1. Step 1: Understand batch.size unit

    The batch.size is specified in bytes, so 32 KB equals 32768 bytes.
  2. Step 2: Check option formats

    Options with strings like "32KB" or "32kb" are invalid because batch.size expects an integer number of bytes.
  3. Final Answer:

    "batch.size": 32768 -> Option A
  4. Quick Check:

    batch.size = bytes number, 32 KB = 32768 [OK]
Hint: batch.size must be integer bytes, not string [OK]
Common Mistakes:
  • Using string values instead of integers
  • Confusing KB with bytes count
  • Using lowercase 'kb' which is invalid
3. Given the following Kafka producer settings:
{
  "batch.size": 16384,
  "linger.ms": 100
}

If messages arrive every 10 ms, what is the expected behavior?
medium
A. Messages are sent immediately without batching
B. Messages are delayed indefinitely until batch size is reached
C. Messages are sent only when batch size reaches 16384 bytes, ignoring linger.ms
D. Messages are batched and sent every 100 ms or when batch size is reached

Solution

  1. Step 1: Analyze linger.ms and batch.size interaction

    The producer waits up to 100 ms (linger.ms) to accumulate messages or sends earlier if batch size (16384 bytes) is reached.
  2. Step 2: Consider message arrival rate

    Messages arrive every 10 ms, so batching will likely happen before 100 ms or batch size limit, whichever comes first.
  3. Final Answer:

    Messages are batched and sent every 100 ms or when batch size is reached -> Option D
  4. Quick Check:

    linger.ms = max wait, batch.size = max batch [OK]
Hint: linger.ms sets max wait, batch.size sets max batch [OK]
Common Mistakes:
  • Ignoring linger.ms and assuming immediate send
  • Thinking linger.ms is minimum wait time
  • Assuming messages delay indefinitely
4. You configured Kafka producer with:
{
  "batch.size": 0,
  "linger.ms": 500
}

What is the problem with this configuration?
medium
A. batch.size cannot be zero; it disables batching causing errors
B. linger.ms must be zero or less to disable delay
C. batch.size zero means messages are sent immediately without batching
D. No problem; this configuration is valid and efficient

Solution

  1. Step 1: Understand batch.size = 0 effect

    Setting batch.size to zero disables batching, so each message is sent immediately.
  2. Step 2: Check linger.ms impact

    Even with linger.ms = 500 ms, messages won't batch because batch.size = 0 forces immediate send.
  3. Final Answer:

    batch.size zero means messages are sent immediately without batching -> Option C
  4. Quick Check:

    batch.size=0 disables batching, linger.ms ignored [OK]
Hint: batch.size=0 disables batching, sends immediately [OK]
Common Mistakes:
  • Assuming batch.size=0 causes errors
  • Thinking linger.ms overrides batch.size=0
  • Believing this config improves efficiency
5. You want to optimize Kafka producer for low latency but still batch messages efficiently. Which configuration is best?
hard
A. "batch.size": 65536, "linger.ms": 5
B. "batch.size": 16384, "linger.ms": 500
C. "batch.size": 0, "linger.ms": 0
D. "batch.size": 32768, "linger.ms": 1000

Solution

  1. Step 1: Define low latency and efficient batching

    Low latency means sending messages quickly; efficient batching means grouping enough messages to reduce overhead.
  2. Step 2: Evaluate options

    "batch.size": 65536, "linger.ms": 5 uses a large batch size (65536 bytes) and a very low linger.ms (5 ms), allowing quick sends with good batching. Other options have too high linger.ms or batch.size=0 disables batching.
  3. Final Answer:

    "batch.size": 65536, "linger.ms": 5 -> Option A
  4. Quick Check:

    Low linger.ms + large batch = low latency + efficient batching [OK]
Hint: Low linger.ms + large batch size = fast and efficient [OK]
Common Mistakes:
  • Choosing linger.ms too high for low latency
  • Setting batch.size to zero disables batching
  • Ignoring balance between batch size and linger.ms
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard