Kafka producers send records to topics. The efficiency, reliability, and performance of data production can be significantly influenced by how producers are configured.
- Efficiency and Throughput: Configurations like batch size and compression type can greatly affect the throughput.
- Data Integrity: Settings such as
acks
andretries
ensure data integrity and delivery guarantees. - Latency: Configurations like
linger.ms
can be tuned to balance between latency and throughput. - Reliability: Features like idempotence and transactions ensure reliable data delivery.
- Simplicity: Producers are conceptually simpler than consumers since they do not require group coordination.
- Broker and Partition Awareness: Producers automatically know which broker and partition to write to.
- Partitioning: A producer partitioner maps each message to a topic partition, and the producer sends a produce request to the leader of that partition.
- Message Key Guarantee: The partitioners shipped with Kafka ensure that all messages with the same non-empty key are sent to the same partition.
- Round-Robin Sending: If producers send data without a key, the data is sent round-robin to all available brokers.
- Acknowledgements: Producers can choose to receive acknowledgements of data writes.
- Default: 1
- Description: The number of acknowledgments the producer requires from brokers. Options are 0, 1, or 'all'.
- Trade-offs: More
acks
increase reliability but may reduce throughput.
- Default: 0
- Description: The delay to aggregate batches of records. Higher values allow larger and more efficient batches.
- Trade-offs: Increases latency but improves throughput and efficiency.
- Default: 16384 (16KB)
- Description: Maximum batch size in bytes. Larger batches are more efficient but take more memory and may delay transmission.
- Trade-offs: Balances memory usage with throughput.
- Default: 5
- Description: The maximum number of unacknowledged requests the client will send on a single connection before blocking.
- Trade-offs: Higher values can improve throughput but may compromise ordering guarantees, especially with retries.
- Default: false
- Description: Ensures exactly-once delivery by preventing duplicate records.
- Trade-offs: May slightly impact throughput but significantly increases reliability.
- Default: none
- Description: The compression type for all data generated by the producer. Options are 'gzip', 'snappy', 'lz4', and 'zstd'.
- Trade-offs: Compression reduces the size of data sent over the network at the cost of CPU usage.
- Default: 2147483647 (Integer.MAX_VALUE)
- Description: Setting a high value allows Kafka to retry indefinitely on transient errors.
- Trade-offs: Ensures delivery at the potential cost of ordering if
max.inflight.requests.per.connection
is greater than 1.
- Default: 60000 (1 minute)
- Description: Maximum time a transaction can remain open. Relevant for exactly-once semantics.
- Trade-offs: Longer transactions can hold resources but ensure completeness of operations.
- Default: 1048576 (1MB)
- Description: The maximum size of a request. Limits the size of a message that can be sent.
- Trade-offs: Larger sizes can improve throughput but risk overwhelming brokers or dropping connections on large messages.
- Default: 33554432 (32MB)
- Description: Total memory available to the producer for buffering.
- Trade-offs: More memory allows more batching and in-flight messages, improving throughput but increasing memory usage.
- Default: null
- Description: Unique identifier for transactional messages. Necessary for exactly-once semantics.
- Trade-offs: Enables transaction support at the cost of additional overhead for maintaining state.
Kafka classifies errors as retriable or non-retriable, affecting how producers react to them.
Retriable errors are temporary and often resolved through retries. Kafka producers automatically retry these errors up to the retries
limit. Common retriable errors include:
- Leader Not Available: Occurs when a new leader is being elected. The producer automatically recovers.
- Not Leader for Partition: The targeted leader is not the current leader for the partition. The producer automatically recovers.
- Not Enough Replicas: Not enough replicas are available for the producer to satisfy the
acks
configuration.
Producers handle these errors by:
- Retrying with a delay specified by
retry.backoff.ms
. - Continuing retries up to the
retries
limit.
Non-retriable errors indicate issues not resolved by retrying and often require configuration changes or other interventions:
- Message Too Large: The message size exceeds
max.request.size
. Requires adjusting the message size or configuration. - Invalid Topic Exception: The specified topic is invalid or does not exist.
- Topic Authorization Failed: The producer lacks permission to publish to the specified topic.
- Offset Out of Range: The specified offset is not within the range of the topic.
- Broker Not Available: The broker is not available for connections.
- Invalid Required Acks: The
acks
configuration is set to an invalid value.
Non-retriable errors demand an intervention or change in configuration or permissions. They point to conditions that retries alone cannot overcome, often requiring administrative action or code changes.
Handling non-retriable errors requires:
- Catching and logging the exception.
- Reviewing the error to understand its cause.
- Adjusting the configuration or addressing the permission issues as needed.
- Monitoring: Continuously monitor log files for error patterns to proactively address emerging issues.
- Smart Retrying: Use the
retry.backoff.ms
setting wisely to avoid overwhelming the system. - Idempotent Producers: Enable
enable.idempotence
to ensure that messages are not duplicated in the event of retries.