Expand description
Idempotent producer client for Apache Kafka in Rust.
It builds on [krabka_client_core] for transport, and adds full
idempotent-producer semantics: InitProducerId on connect, per-batch
(producer_id, producer_epoch, base_sequence), and retries that re-frame
the same RecordBatch so the broker’s dedup catches them.
It also supports transactional, exactly-once production:
init_transactions, begin_transaction, which returns a Transaction
guard whose commit/abort finishes it, and send_offsets_to_transaction
for the consume-process-produce pattern of KIP-447.
§Quick start
use std::time::Duration;
use bytes::Bytes;
use krabka_client_producer::{Acks, Compression, Producer, ProducerRecord};
let producer = Producer::builder()
.bootstrap("localhost:9092")
.compression(Compression::Lz4)
.acks(Acks::All)
.linger(Duration::from_millis(5))
.build()
.await?;
let metadata = producer
.send(ProducerRecord {
topic: "my-topic".into(),
value: Some(Bytes::from("hello")),
..Default::default()
})
.await?;
producer.flush().await?;
producer.close().await?;§Pipelined delivery
Use Producer::enqueue to queue records without waiting for each
acknowledgement. Each DeliveryHandle returns one producer result.
let mut deliveries = Vec::new();
for record in records {
deliveries.push(producer.enqueue(record).await?);
}
for delivery in deliveries {
let metadata = delivery.await?;
}§Capabilities and boundaries
This crate owns producer-facing semantics: batching, compression,
idempotence, retries, per-record partition overrides, transactional RPCs,
and send_offsets_to_transaction for consume-process-produce flows. The
built-in partitioner follows Kafka’s: keyed records hash to a partition,
and keyless records use an adaptive sticky partition (KIP-480, KIP-794). Set
ProducerRecord::partition to pin an individual record. Serialization is
deliberately owned by the caller: key and value are raw Bytes, so a
schema-registry or serde integration can sit on top without constraining the
producer API.
Structs§
- Compression
Levels - Validated compression levels of the producer (KIP-390).
- Consumer
Group Metadata - The identity that a consumer presents to a transactional producer for KIP-447 offset-commit fencing.
- Delivery
Handle - The delivery result of a record queued by
crate::Producer::enqueue. - EndTransaction
Error - Error returned by
Transaction::commit,abort, and theOwnedTransactionequivalents. - Header
- Owned
Transaction - Same contract as
Transaction, but owns anArc<Producer>instead of borrowing it. - Partition
Info - One partition of a topic, as Kafka’s
PartitionInfodescribes it to a customPartitioner. - Prepared
Transaction State - Stable identity of a transaction prepared for external two-phase commit.
- Prepared
Transaction State Parse Error - Error returned when a prepared transaction token is malformed.
- Producer
- Producer
Flush Timeout - Validated deadline for flushing all buffered and in-flight records.
- Producer
Record - Producer
Retry Policy - Validated producer retry and transaction timing.
- Producer
Throughput Policy - Validated producer batching and compression policy.
- Record
Metadata - What the producer learned about a record that the broker acknowledged.
- Transaction
- An open transaction, borrowing the
Producerthat opened it.
Enums§
- Acks
- Compression
- Producer
Error - Record
Size Limit - The producer limit that a record is larger than.
Constants§
- DEFAULT_
PRODUCER_ ACKS - Default producer acknowledgement mode. Kafka’s
acksdefault isall. - DEFAULT_
PRODUCER_ BATCH_ BYTES - Default producer batch size in bytes.
- DEFAULT_
PRODUCER_ BUFFER_ MEMORY - Default producer buffer memory in bytes.
- DEFAULT_
PRODUCER_ COMPRESSION - Default producer compression.
- DEFAULT_
PRODUCER_ COMPRESSION_ GZIP_ LEVEL - Default gzip compression level. Kafka’s
compression.gzip.leveldefault isCompressionType.GZIP.defaultLevel(), which isDeflater.DEFAULT_COMPRESSION(-1). - DEFAULT_
PRODUCER_ COMPRESSION_ LZ4_ LEVEL - Default lz4 compression level. Kafka’s
compression.lz4.leveldefault is 9. - DEFAULT_
PRODUCER_ COMPRESSION_ ZSTD_ LEVEL - Default zstd compression level. Kafka’s
compression.zstd.leveldefault is 3. - DEFAULT_
PRODUCER_ DELIVERY_ TIMEOUT - Default producer delivery timeout. Kafka’s
delivery.timeout.msdefault is 120000. - DEFAULT_
PRODUCER_ FLUSH_ TIMEOUT - Default deadline for flushing all buffered and in-flight records.
- DEFAULT_
PRODUCER_ INIT_ RETRY_ TIMEOUT - Default producer-ID initialization retry timeout.
- DEFAULT_
PRODUCER_ LINGER - Default delay before sending a partial producer batch.
- DEFAULT_
PRODUCER_ MAX_ BLOCK - Default longest time that
sendwaits for the metadata of its topic. - DEFAULT_
PRODUCER_ MAX_ IN_ FLIGHT - Default cross-partition in-flight request limit.
- DEFAULT_
PRODUCER_ MAX_ REQUEST_ SIZE - Default largest serialized record that
sendaccepts, in bytes. - DEFAULT_
PRODUCER_ METADATA_ MAX_ AGE - Kafka’s producer
metadata.max.age.msdefault (5 minutes). - DEFAULT_
PRODUCER_ METADATA_ MAX_ IDLE - Kafka’s producer
metadata.max.idle.msdefault (5 minutes). - DEFAULT_
PRODUCER_ PARTITIONER_ ADAPTIVE_ PARTITIONING_ ENABLE - Default of
partitioner_adaptive_partitioning_enable. Kafka’spartitioner.adaptive.partitioning.enabledefault istrue. - DEFAULT_
PRODUCER_ PARTITIONER_ AVAILABILITY_ TIMEOUT - Default of
partitioner_availability_timeout. Kafka’spartitioner.availability.timeout.msdefault is 0, which turns the check off. - DEFAULT_
PRODUCER_ PARTITIONER_ IGNORE_ KEYS - Default of
partitioner_ignore_keys. Kafka’spartitioner.ignore.keysdefault isfalse. - DEFAULT_
PRODUCER_ PARTITIONER_ RACK_ AWARE - Default of
partitioner_rack_aware. Kafka’spartitioner.rack.awaredefault isfalse. - DEFAULT_
PRODUCER_ RECEIVE_ BUFFER - Kafka’s producer
receive.buffer.bytesdefault (32 KiB), half the consumer and admin default. - DEFAULT_
PRODUCER_ REQUEST_ TIMEOUT - Default producer request timeout.
- DEFAULT_
PRODUCER_ RETRIES - Default retries after a batch’s initial send.
- DEFAULT_
PRODUCER_ RETRY_ BACKOFF - Default producer retry backoff.
- DEFAULT_
PRODUCER_ RETRY_ BACKOFF_ MAX - Default upper bound of the exponential retry backoff. Kafka’s
retry.backoff.max.msdefault is 1000. - DEFAULT_
PRODUCER_ SEND_ BUFFER - Kafka’s producer
send.buffer.bytesdefault (128 KiB). - DEFAULT_
PRODUCER_ TRANSACTION_ TIMEOUT - Default transaction timeout.
- MIN_
PRODUCER_ METADATA_ MAX_ IDLE - Kafka’s lower bound of
metadata.max.idle.ms(ProducerConfigatLeast(5000)).
Traits§
- Partitioner
- A caller-supplied
partitioner.classhook (Kafka’sPartitionerinterface).
Functions§
- partition_
for_ key - Pick the partition that Kafka picks for
key.