Apache Kafka Tutorial 0/111 lessons ~6 min read Lesson 10
Producers Overview
What is Kafka Producer?
Course progress0%
Focus
9 guided sections
Practice signal
Examples included
Career prep
Foundation builder
Introduction
What is Kafka Producer? A Kafka Producer is a client application that sends records to Kafka topics for real-time pipelines and event-driven systems.
Understanding the topic
What happens — Kafka Producer:
- You construct a message — e.g. "User signed up".
- The producer publishes it to a topic (a channel for similar events).
- Kafka stores it in a partitioned, replicated log.
- Downstream services consume the message in real time.
| Term | Description |
|---|---|
| Topic | A named channel where producers publish and consumers read — e.g. user_signups or payment_logs. |
| Partition | A topic is split into partitions for parallel storage and processing. Ordering is per partition. |
| ProducerRecord | The object you send: topic name, optional key, value (payload), and optional headers. |
| Serialization | Converting your data (String, JSON, Avro) into bytes — Kafka only stores bytes. |
| Key | Optional field used to pick a partition. Same key → same partition → ordered delivery for that key. |
| Broker | Kafka server that stores messages and serves producers and consumers. |
| Ack (Acknowledgment) | How many replicas must confirm receipt before the send is considered successful (acks=0, 1, or all). |
Visual explanation
Pipeline view:
text
App creates message (e.g. "User signed up")↓Producer sends to topic (e.g. user_signups)↓Kafka stores in partitioned log (replicated)↓Consumers / analytics / alerts read in real time
Step-by-step explanation
- Initialization — Producer connects to bootstrap servers and receives metadata (topics, partitions, leaders).
- Message creation — Application builds a ProducerRecord with topic, key, value, and optional timestamp.
- Serialization — Key and value serializers convert objects to bytes (e.g. StringSerializer).
- Partitioning — Hash of key picks partition; no key → round-robin / sticky partitioner.
- Batching — Records accumulate in the RecordAccumulator per partition for efficient network use.
- Sending — Background sender thread ships batches to the partition leader broker.
- Acknowledgement — Broker responds; producer retries on failure based on acks and retry settings.
Informative example
Send methods:
java
// Fire-and-forget (fastest, no delivery guarantee)producer.send(new ProducerRecord<>("topic", "key", "value"));// Synchronous — blocks until broker respondsRecordMetadata meta = producer.send(record).get();// Asynchronous with callback (recommended)producer.send(record, (metadata, exception) -> {if (exception != null) { /* handle error */ }else { /* success: metadata.topic(), metadata.partition() */ }});
Production-ready producer setup:
java
Properties props = new Properties();props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());props.put(ProducerConfig.ACKS_CONFIG, "all");props.put(ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG, "true");try (KafkaProducer<String, String> producer = new KafkaProducer<>(props)) {ProducerRecord<String, String> record =new ProducerRecord<>("orders", "order-123", "{\"status\":\"CREATED\"}");producer.send(record, (metadata, error) -> {if (error != null) error.printStackTrace();else System.out.println(metadata.topic() + "-" + metadata.partition());});}
Execution workflow
1Kafka Producer workflow
1 / 7Initialization
Producer connects to bootstrap servers and receives metadata (topics, partitions, leaders).
Best practices
- Use acks=all with min.insync.replicas for critical events.
- Enable idempotence for retry safety.
- Choose keys intentionally; random keys destroy ordering.
- Monitor record-error-rate, request-latency, batch-size, and retries.
Common mistakes
- Ignoring asynchronous send failures.
- Using null keys for events that require aggregate ordering.
- Making producer timeouts too low and failing during normal broker leader movement.
Summary
The Kafka Producer starts real-time data pipelines. Understand the workflow, tune batching and acks, and use idempotence and callbacks for reliable delivery at scale.
Ready to mark this lesson complete?Track your journey across the entire course.