Course contentsShow
← Data Engineering
Lesson 509 of 8258. Real-time Data Streaming with KafkaFree lesson

Configuring Producer Throughput and Latency Trade-offs

You will learn to tune batch sizes and linger settings to balance high-throughput data ingestion against low-latency requirements for real-time streaming.

In this lesson, you will learn how to balance Kafka producer performance by adjusting batching mechanisms to optimize for either high-throughput data ingestion or ultra-low-latency delivery.

The Balancing Act

In Kafka, the producer doesn’t send every single message to the broker immediately. Sending individual requests over the network is inefficient because of the overhead associated with each request (TCP headers, broker request handling, etc.). To improve throughput, Kafka groups messages into batches.

However, batching creates a fundamental tension:

  • High Throughput: If you force the producer to wait until a batch is full, you minimize the number of network requests and maximize total data volume. The trade-off is higher latency, as your individual messages sit in the buffer waiting for their "bus" to fill up.
  • Low Latency: If you send messages the moment they arrive, you minimize wait time but significantly reduce total throughput due to network congestion and increased request overhead on the broker.

Tuning the Knobs

Two primary producer configurations control this behavior:

  1. batch.size: This defines the maximum memory (in bytes) used for a single batch of messages. Increasing this helps throughput for high-volume producers, but be careful—if you set it too high, you consume more memory on the producer machine.
  2. linger.ms: This introduces an explicit artificial delay. If you set linger.ms=5, the producer will wait up to 5 milliseconds to see if more messages arrive to fill the current batch before sending it.
# High Throughput Configuration
batch.size=65536
linger.ms=20

# Low Latency Configuration
batch.size=16384
linger.ms=0

By setting linger.ms to 0, you prioritize immediate delivery, sacrificing batching efficiency. By increasing linger.ms, you allow the producer to aggregate more records, drastically improving throughput at the expense of a slight delay in record availability. Finding the "sweet spot" depends on your specific use case: real-time trading requires low linger.ms, while log aggregation or historical data ingestion thrives on higher batching settings.

Key Takeaway: Increasing batch.size and linger.ms improves throughput by reducing request overhead, but introduces higher latency for individual messages; tuning these depends on whether your application prioritizes speed of delivery or volume of data.