Skip to main content
Hello, and welcome back. In this lesson we’ll set up a Kafka producer and produce messages to a topic. The walkthrough uses a local Kafka broker, the kafka-python client, and the Kafdrop UI to inspect messages and partitions. Follow the steps in order to reproduce the demo in your lab environment. We are in our lab environment, where Apache Kafka is already up and running.
The image shows a KodeKloud Kafka playground with a task description on the left and a terminal interface on the right displaying a KodeKloud ASCII logo.

Prerequisites

  • Kafka broker running on localhost:9092
  • Access to the Kafka installation directory (scripts live under bin/)
  • Python 3 with venv support (we’ll create an isolated virtual environment)
  • kafka-python client library (installed into the venv)
  • Optional: Kafdrop or another Kafka UI to inspect topics and messages

1) Inspect the Kafka CLI utilities

From the Kafka installation bin directory you can list the available CLI scripts. Example truncated output:
You will use kafka-topics.sh to create and manage topics in the next step.

2) Create a topic with multiple partitions

Create a topic named multi-partition-topic with 3 partitions and a replication factor of 1:
Tip: Once the topic is created, you can verify its configuration using kafka-topics.sh --describe --topic multi-partition-topic --bootstrap-server localhost:9092 or inspect it visually with Kafdrop.
The image shows a Kafdrop web interface displaying a Kafka Cluster Overview, including details about bootstrap servers, topics, partitions, and brokers.

3) Prepare a Python virtual environment and install the client

To avoid modifying the system Python, create and activate a virtual environment and install kafka-python: Update package lists (example):
Create and activate the venv, then install the client:
Example pip output:

4) Example Python producer script

Create kafka-producer-example.py. The script below:
  • configures logging,
  • creates a KafkaProducer connected to localhost:9092,
  • composes sample “coffee shop” messages,
  • sends 10 messages to multi-partition-topic with an explicit key (so partitioning is deterministic for identical keys),
  • waits for each send to complete and flushes before exit.
Note: When you provide a message key, Kafka’s partitioner uses it to determine the target partition. Messages with the same key are guaranteed to go to the same partition. Without a key, the producer distributes messages across partitions (modern producers may use sticky batching for throughput).

5) Run the producer and observe delivery

Run the script from the activated virtual environment:
Example logs:
The logs indicate successful deliveries and the partition targets for each message.
The image shows a Kafdrop dashboard displaying details of a Kafka topic named "multi-partition-topic," including an overview of partitions, replicas, and consumers.

6) Verify messages in Kafdrop

Refresh the Kafdrop UI and inspect multi-partition-topic. The message count should reflect the number of messages you sent (10 in this demo). Click “View Messages” to inspect message contents.
The image shows a Kafdrop interface displaying topic messages from a Kafka topic named "multi-partition-topic," detailing coffee shop information such as name, location, and rating.
If you initially see messages for only one partition, use the partition selector in Kafdrop to view partitions 0, 1, and 2 individually. The messages will be distributed across partitions (e.g., 4/2/4 or another distribution depending on the keys and partitioner).

Why partitions matter

  • Partitions enable parallelism: multiple consumers in a consumer group can process partitions in parallel.
  • Keys ensure ordering per key: records with the same key are written to the same partition and consumed in order.

Quick reference — Common commands

References

That concludes this demo on producing messages to Kafka using a Python producer. See you in the next lesson.

Watch Video

Practice Lab