Kafka Producers & Consumers
Learn to build robust applications that publish and subscribe to Kafka topics.Producer API
Producers publish data to the topics of their choice.Key Responsibilities
- Partitioning: Deciding which partition to send the message to.
- Serialization: Converting key/value objects to bytes.
- Compression: Reducing network bandwidth (Snappy, Gzip, LZ4, Zstd).
- Batching: Grouping messages for efficiency.
Java Example
Python Example (kafka-python)
Consumer API
Consumers read data from topics. They subscribe to one or more topics and pull data.Consumer Groups & Rebalancing
- Consumer Group: A pool of consumers that share the work.
- Rebalancing: When a consumer joins/leaves, partitions are reassigned.
Java Example
Delivery Semantics
At Most Once
Messages may be lost, but never duplicated.
Commit offset before processing.
At Least Once
Messages are never lost, but may be duplicated.
Commit offset after processing. (Default/Preferred)
Exactly Once
Each message is delivered exactly once.
Requires Transactional API.
Important Configurations
Producer Configs
| Config | Description | Recommended |
|---|---|---|
acks | How many replicas must acknowledge | all (for durability) |
retries | Retry count on transient errors | Integer.MAX_VALUE |
enable.idempotence | Prevent duplicates | true |
compression.type | Compression algorithm | snappy or lz4 |
Consumer Configs
| Config | Description | Recommended |
|---|---|---|
group.id | Unique ID for the consumer group | Required |
auto.offset.reset | What to do if no offset exists | earliest (start from beginning) |
enable.auto.commit | Auto-commit offsets | false (manual control) |
max.poll.records | Max records per poll | Tuned to processing speed |
Best Practices
Handle Rebalancing
Handle Rebalancing
Handle
WakeupException and close consumers gracefully to trigger a rebalance immediately rather than waiting for a timeout.Idempotent Processing
Idempotent Processing
Since “At Least Once” is common, ensure your processing logic handles duplicates (e.g., using a database unique constraint).
Monitor Lag
Monitor Lag
Consumer Lag is the difference between the latest offset in the partition and the consumer’s current offset. High lag means consumers are too slow.
Exactly-Once Semantics (EOS)
Exactly-once is the holy grail of message delivery. Kafka supports it through Idempotent Producers and Transactions.Idempotent Producer
Prevents duplicates caused by producer retries.- Producer assigns a Producer ID (PID) and sequence number to each message
- Broker deduplicates based on PID + sequence number
- If retry sends duplicate, broker recognizes and discards it
Transactional Producer
For atomic writes across multiple partitions/topics.Transactional Consumer
Read only committed messages:| Isolation Level | Behavior |
|---|---|
read_uncommitted | See all messages (including aborted) |
read_committed | Only see committed transactions |
Consumer Group Rebalancing Deep Dive
Rebalancing is one of the most misunderstood Kafka concepts.What Triggers Rebalancing?
- Consumer joins the group
- Consumer leaves the group (graceful or crash)
- Consumer fails to send heartbeat within
session.timeout.ms - Topic partition count changes
- Consumer subscription changes
Rebalancing Strategies
| Strategy | Behavior | Use Case |
|---|---|---|
| Range | Consecutive partitions to each consumer | Co-partitioned topics |
| RoundRobin | Evenly distributed | General purpose |
| Sticky | Minimizes partition movement | Reduce reprocessing |
| Cooperative Sticky | Incremental rebalance (no stop-the-world) | Production recommended |
Configuring Cooperative Rebalancing
Preventing Unnecessary Rebalances
Producer Partitioning Strategies
Default Partitioner
Custom Partitioner
Offset Management
Manual vs Auto Commit
| Mode | Config | Behavior | Risk |
|---|---|---|---|
| Auto | enable.auto.commit=true | Commits every 5s | Data loss on crash |
| Manual Sync | consumer.commitSync() | Blocks until committed | Slower |
| Manual Async | consumer.commitAsync() | Non-blocking | May fail silently |
Best Practice: Commit After Processing
Seek to Specific Offset
Interview Questions & Answers
What is the difference between At-Least-Once and Exactly-Once?
What is the difference between At-Least-Once and Exactly-Once?
| Delivery | Description | How to Achieve |
|---|---|---|
| At-Most-Once | May lose messages | Commit before processing |
| At-Least-Once | May have duplicates | Commit after processing |
| Exactly-Once | No loss, no duplicates | Idempotent + Transactions |
enable.idempotence=true- Transactional producer
isolation.level=read_committedfor consumers
How do you handle consumer lag?
How do you handle consumer lag?
Diagnosis:Solutions:
- Add consumers: More consumers = more parallelism (up to partition count)
- Increase partitions: Allows more consumers
- Optimize processing: Batch database writes, use async I/O
- Increase batch size:
max.poll.records - Skip old data: Reset offset to latest
What happens if a consumer takes too long to process?
What happens if a consumer takes too long to process?
If time between
poll() calls exceeds max.poll.interval.ms:- Consumer is considered dead
- Rebalance is triggered
- Partitions are reassigned
- Consumer may process same messages again (duplicates)
max.poll.interval.ms or reduce max.poll.recordsHow do you ensure message ordering?
How do you ensure message ordering?
Within a partition: Guaranteed by KafkaFor a specific key: Use a key when producingGotcha with retries: Set
max.in.flight.requests.per.connection=1 or use idempotent producer to prevent reordering on retry.What is the difference between commitSync and commitAsync?
What is the difference between commitSync and commitAsync?
| Method | Behavior | Use Case |
|---|---|---|
commitSync() | Blocks until committed | Safety critical |
commitAsync() | Non-blocking, callback | High throughput |
commitAsync() in loop, commitSync() on shutdown:Common Pitfalls
Next: Kafka Streams →