tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Messaging > Apache Kafka > Apache Kafka Producer and Consumer

Apache Kafka Producer and Consumer

Author: Venkata Sudhakar

Apache Kafka is a distributed event streaming platform designed for high-throughput, fault-tolerant, and real-time data pipelines. At its core, Kafka is a distributed commit log where producers write messages to named topics, and consumers read messages from those topics. Unlike traditional message queues such as RabbitMQ, Kafka retains messages on disk for a configurable period (default 7 days), allowing multiple independent consumers to read the same message at different times and at different rates.

A Kafka topic is divided into one or more partitions, which are the unit of parallelism and scalability. Each partition is an ordered, immutable sequence of messages. Within a partition, every message has a unique sequential offset. When you have multiple partitions, producers can write to different partitions in parallel and consumers in a consumer group are each assigned one or more partitions, enabling horizontal scaling of both producers and consumers. Messages with the same key are always written to the same partition, ensuring ordering for that key.

The below example shows how to write a Kafka producer in Java using the KafkaProducer API to publish order events to a topic, with both synchronous and asynchronous send patterns.


It gives the following output,

Sent to partition: 2 offset: 0
Async sent: partition=0 offset: 0
Async sent: partition=1 offset: 0
Async sent: partition=2 offset: 1
Async sent: partition=0 offset: 1
All messages sent.

The below example shows how to write a Kafka consumer in Java that reads order events from the topic, processes them, and commits offsets manually for reliable at-least-once processing.


It gives the following output,

Consumer started. Waiting for messages...
Received: partition=2 offset=0 key=ORD-1001 value={"orderId":"ORD-1001","amount":149.99}
Processing order: ORD-1001
Received: partition=0 offset=0 key=ORD-1002 value={"orderId":"ORD-1002","amount":10020.0}
Processing order: ORD-1002
Received: partition=1 offset=0 key=ORD-1003 value={"orderId":"ORD-1003","amount":10030.0}
Processing order: ORD-1003
Committed offsets for 3 records.

Key Kafka concepts to understand:

Consumer Group - Multiple consumer instances sharing the same group.id form a consumer group. Kafka distributes topic partitions evenly across them, so each partition is consumed by exactly one instance in the group at a time. This enables parallel processing while guaranteeing order within each partition.

Offset Management - The offset is the consumer position in a partition. With enable.auto.commit=false and manual commitSync(), offsets are committed only after successful processing, giving you at-least-once delivery semantics. If a consumer crashes mid-batch, the next consumer restart re-reads from the last committed offset.

Partitioning - Messages with the same key always go to the same partition, preserving order for that key. With a null key, Kafka distributes messages round-robin across partitions.


 
  


  
bl  br