Overview
A Kafka Producer’s main duties include:- Serialization: Convert message keys and values into byte arrays.
- Partitioning: Decide which partition each record should go to.
- Batching & Buffering: Group messages to optimize network usage.
- Retries & Error Handling: Automatically retry on transient failures.
-
Acknowledgments: Wait for durability guarantees based on the configured
ackslevel.
By default, Kafka Producers send records asynchronously, yielding high throughput and non-blocking application threads.
Producer Workflow
-
Configure Producer Properties
Define bootstrap servers, serializers, acknowledgments, and other settings. -
Instantiate the Producer
Create aKafkaProducerclient using the configuration. -
Send Records Asynchronously
UseProducerRecordobjects to publish data to a specified topic. -
Handle Callbacks (Optional)
Attach success and error callbacks to monitor delivery results. -
Flush and Close
Ensure all buffered records are sent before shutting down the client.
Basic Python Example
Configuration Reference
| Property | Description | Example | |---------------------|------------------------------------------------------------------|---------------------------| |bootstrap.servers | Initial Kafka broker addresses (host:port) | ['broker1:9092'] |
| key.serializer | Converts the record key to bytes | StringSerializer() |
| value.serializer | Converts the record value to bytes | JsonSerializer() |
| acks | 0, 1, or all (wait for leader/ISR acknowledgment) | 'all' |
| retries | Number of automatic retries on transient failures | 3 |
| linger.ms | Time to wait for additional messages before sending a batch (ms)| 5 |
| batch.size | Maximum batch size in bytes | 16384 |
Setting
acks=0 offers low latency but no durability guarantees. Use with caution in critical data flows.To enable exactly-once semantics, set enable.idempotence=true (Kafka ≥0.11).Best Practices
- Tune batch.size and linger.ms to strike a balance between latency and throughput.
- Implement robust error handling in callbacks for custom retry or logging logic.
- Enable idempotence (
enable.idempotence=true) for exactly-once delivery in supported Kafka versions. - Monitor key producer metrics (e.g.,
record-send-rate,request-latency) via JMX or your monitoring stack.