Optimizing Producer Performance
1. Tuning Batch Size
| Config | Description | Default |
|---|---|---|
| batch.size | Max batch bytes/partition | 16384 |
| Larger | Higher throughput | More memory |
| Full Batch | Sent immediately | Regardless of linger |
2. Configuring Linger Time
| Config | Description | Default |
|---|---|---|
| linger.ms | Wait to fill batch | 0 |
| Tradeoff | Latency vs throughput | Higher = fewer requests |
| Typical | 5-20ms throughput | Batching |
3. Optimizing Compression
| Codec | Ratio | CPU |
|---|---|---|
| lz4 | Good | Low |
| zstd | Best | Medium |
| snappy | Moderate | Very low |
4. Setting Buffer Memory
| Config | Description | Default |
|---|---|---|
| buffer.memory | Total send buffer | 33554432 |
| max.block.ms | Block when full | 60000 |
| Backpressure | send() blocks if full | Flow control |
5. Configuring Max In-Flight Requests
| Config | Description | Default |
|---|---|---|
| max.in.flight. requests.per.connection |
Unacked requests | 5 |
| Idempotence | ≤5 keeps ordering | Safe |
| Higher | More throughput | Reorder risk |
6. Using Idempotent Producer
| Aspect | Description | Detail |
|---|---|---|
| enable.idempotence | Exactly-once per partition | Default true |
| Requires | acks=all, retries>0 | Auto-set |
| Cost | Negligible | Recommended |
7. Optimizing Serialization
| Aspect | Description | Detail |
|---|---|---|
| Binary | Avro/Protobuf compact | vs JSON |
| Reuse | Cache serializers | No realloc |
| Schema ID | Small wire footprint | Registry |
Example: Compact serializer
props.put("value.serializer",
"io.confluent.kafka.serializers.KafkaAvroSerializer");
8. Tuning Request Timeout
| Config | Description | Default |
|---|---|---|
| request.timeout.ms | Per-request wait | 30000 |
| Too Low | False timeouts | Spurious retry |
| Bound | ≤ delivery.timeout.ms | Constraint |
9. Configuring Retries
| Config | Description | Detail |
|---|---|---|
| retries | Resend attempts | Bounded by timeout |
| acks=all | Durability | Pair with retries |
| Idempotence | No dup on retry | Enable |
10. Monitoring Producer Metrics
| Metric | Description | Watch |
|---|---|---|
| record-send-rate | Throughput | Trend |
| batch-size-avg | Batching efficiency | Tune linger |
| record-error-rate | Failures | Alert |