Skip to content

Checking your reading list…

Retry

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

Kafka Producer Deep Dive: Serialization, Partitioning, Compression & Idempotency

This article explains Kafka Producer's workflow including serialization, partitioning strategies, compression, message batching, and idempotent delivery using Producer IDs and sequence numbers, covering key configurations like batch.size, linger.ms, acks, and bootstrap.servers.

Producer Role

The Kafka Producer receives configuration and messages, then sends them to the appropriate Kafka Broker. During sending, it serializes messages, selects partitions, compresses data, and uses load balancing to distribute messages across partitions for efficiency.

Producer architecture diagram
Producer architecture diagram

Configuration Properties

Key producer configurations include: bootstrap.servers: Initial connection addresses for the Kafka cluster. acks: Acknowledgment level required for a successful write. batch.size: Maximum bytes per message batch. linger.ms: Maximum time to wait before sending a batch. key.serializer: Serializer for message keys. value.serializer: Serializer for message values.

Producer Records

Each record must contain a target topic name and message value. Partition, timestamp, and key are optional.

Producer record structure
Producer record structure

Broker & Metadata Discovery

Bootstrap Server

Any broker can act as a bootstrap server. Configuring at least two bootstrap servers improves connection reliability.

Bootstrap server metadata fetch
Bootstrap server metadata fetch

The producer connects to a bootstrap server to fetch cluster metadata: available brokers, topics, partitions, and replicas. It then locates the leader replica for the target partition and sends records directly to that broker.

Workflow

The producer workflow consists of five steps:

Producer workflow diagram
Producer workflow diagram

Serialization

Partitioning

Compression

Message Accumulation

Group by Broker and Send

Serialization

The producer converts keys and values to byte arrays using configured serializers (e.g., StringSerializer, ByteArraySerializer, ByteBufferSerializer).

Partitioning

Default partitioner uses the Murmur2 hash algorithm. If a key is provided, its hash determines the partition, ensuring same-key messages go to the same partition. Without a key, a round-robin approach distributes messages evenly. Custom partitioners are supported for flexible routing.

Partitioning illustration
Partitioning illustration

Messages with the same key are stored in arrival order within the partition, enabling ordered consumption for same-key messages.

Compression

Compression reduces network transfer and storage overhead. Supported types are shown below. Compression also lowers replica synchronization traffic, improves throughput, reduces latency, and saves disk space.

Supported compression types
Supported compression types

Message Accumulation

Messages are buffered per partition in a Record Accumulator. When accumulated bytes reach batch.size, a batch is formed and queued for sending.

Record accumulator diagram
Record accumulator diagram

Send Thread & Batching

The sender thread groups batches by destination broker and transmits them. A batch is sent when either:

Accumulated size reaches batch.size (immediate send).

Time in buffer reaches linger.ms (send even if batch not full).

Send thread timing diagram
Send thread timing diagram

Idempotent Producer (Duplicate Detection)

Network failures may cause the producer to retry a message already written by the broker, creating duplicates. Since Kafka 0.11, idempotent producers use a Producer ID (PID) and a monotonically increasing Sequence Number per partition to uniquely identify each message.

Idempotent producer duplicate detection
Idempotent producer duplicate detection

On retry, if the broker sees the same PID and sequence number, it treats the message as a duplicate, skips writing, and returns an acknowledgment. This guarantees exactly-once semantics per partition.

Other Important Properties

buffer.memory

: Total memory for buffering unsent messages. retries: Maximum retry attempts on send failure (default 0). Retries may reorder messages. max.in.flight.requests.per.connection: Max unacknowledged requests per connection (default 5). Setting to 1 prevents reordering but reduces throughput. max.request.size: Maximum bytes per request (default 1 MB).

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

ConfigurationserializationKafkaidempotencyProducerCompressionpartitioningMessage Batching

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion