Table of Contents
Introduction: Messaging Systems with Kafka
- What Are Messaging Systems?
- Why Use Messaging Systems?
- Real World Use Cases
- Why Kafka?
Core Concepts of Messaging Systems and Kafka
- Core Concepts of Messaging Systems
- Core Concepts in Kafka
- Kafka in Action: Example of a Ride Hailing App
Hands-On: Setting Up Kafka
- What Is Required to Run Kafka?
Installation Options
- Using Docker (Recommended)
- Native Installation
- Verifying the Installation
- Exploring Kafka Topics
- Producing Messages
- Consuming Messages
- Simulating Real World Scenarios
Building Your First Kafka Application
- Setting Up the Environment
- Writing a Kafka Producer
- Writing a Kafka Consumer
- Enhancing the Application
- Testing the Application
Conclusion
I. Introduction: Messaging Systems with Kafka
1. What Are Messaging Systems?
Messaging systems are the backbone of modern distributed architectures, enabling communication between independent services or applications. They work by transmitting messages (small packets of information) from one system to another, ensuring these systems can work asynchronously and independently.
Imagine an e-commerce platform where a customer places an order. Here’s what happens:
Order Service: Accepts and stores the order details.
Payment Service: Processes the payment.
Inventory Service: Updates stock levels.
Notification Service: Sends an email or SMS confirmation.
Rather than directly linking these services, a messaging system ensures each service receives and processes the required information through messages in a decoupled manner.
2. Why Use Messaging Systems?
Asynchronous Communication: Producers (senders) and consumers (receivers) don’t need to interact directly or operate simultaneously.
Scalability: Systems can handle high loads by scaling producers, consumers, or both.
Resilience: Messages persist even if a consumer is temporarily unavailable.
Flexibility: Multiple services can act on the same data, enabling publish-subscribe patterns.
3. Real World Use Cases
Realtime Analytics:
- Streaming live data to dashboards for insights (e.g., stock prices, website metrics).
Log Aggregation:
- Collecting logs from various servers for centralized monitoring and analysis.
Event Driven Applications:
- Handling user actions (e.g., clicking a button) and triggering downstream processes.
Microservices Communication:
- Decoupling independent services in a microservices architecture.
4. Why Kafka?
Apache Kafka is a distributed messaging system originally built by LinkedIn and now maintained by the Apache Software Foundation. Kafka goes beyond simple message queues, offering features that make it a powerful choice for handling real-time data streams and building event-driven systems.
Key Benefits of Kafka
High Throughput and Low Latency
Kafka can handle millions of messages per second with minimal delay, making it ideal for applications requiring real-time processing, such as financial transactions, log aggregation, and IoT data streams.
Why It Matters: Systems like RabbitMQ and ActiveMQ are great for traditional queuing but struggle under extremely high loads due to their broker-centric architectures. Kafka’s distributed design overcomes this limitation.
Scalability
Kafka scales horizontally by adding brokers (servers). Data is distributed across partitions, which can be processed independently by consumers.
Comparison: RabbitMQ relies on adding more queues for scale, but this approach can become complex to manage in larger systems. Kafka’s native partitioning simplifies scaling.
Fault Tolerance and Durability
Kafka replicates data across brokers, ensuring no messages are lost even if a broker fails. This is achieved through configurable replication factors.
Compared to Others: Systems like RabbitMQ require specific configurations or external tools for similar fault tolerance, while Kafka includes it natively as part of its design.
Event Streaming at Scale
Kafka isn’t just a messaging queue; it’s an event-streaming platform. Kafka supports replaying messages by retaining data for a configurable time, enabling applications to process historical data.
Advantage Over Queues: Traditional messaging systems delete messages once consumed. Kafka stores them for the duration of the retention policy, allowing multiple consumers to process the same data independently.
Flexible Communication Patterns
Kafka supports both publish-subscribe and point-to-point messaging models, making it versatile for various use cases.
Comparison: While RabbitMQ excels in transactional, one-to-one communications, Kafka’s multi-consumer design is better for analytics, streaming, and large-scale applications.
Integration Ecosystem
Kafka integrates seamlessly with a wide range of tools and frameworks:
Kafka Connect: For syncing with databases, Elasticsearch, Hadoop, etc.
Kafka Streams: For processing data in real time.
KSQL: For querying and transforming data using SQL-like syntax.
Compared to Others: While RabbitMQ or ActiveMQ may need third-party plugins for complex integrations, Kafka provides these capabilities natively.
When to Choose Kafka?
Use Kafka if:
- Your system needs to process large volumes of data in real time.
- You require scalable, fault-tolerant messaging.
- You’re building a data pipeline or event-streaming system (e.g., log aggregation, metrics collection, user activity tracking).
- Multiple consumers need to process the same data independently (e.g., notifications and analytics).
Consider Other Solutions if:
- You need lightweight, transactional messaging with lower setup complexity (RabbitMQ or ActiveMQ might be better for simple use cases).
- Your system is event-driven but does not process massive amounts of data.
Kafka’s Versatility in Real-World Applications
Log Aggregation: Collect logs from multiple servers into a central repository for real-time monitoring.
Event Sourcing: Track all user interactions (clicks, purchases, etc.) for downstream analysis.
Real-Time Analytics: Stream data to dashboards or analytics systems for instant insights.
Decoupling Microservices: Enable services to communicate asynchronously without direct dependencies.
II. Core Concepts of Messaging Systems and Kafka
To effectively work with Kafka and messaging systems, it’s crucial to understand their core building blocks. This section introduces the foundational concepts that underpin messaging systems in general and Kafka in particular.
1. Core Concepts of Messaging Systems
Producers and Consumers
Producers:
- Applications that send data (messages) to a messaging system.
- In Kafka, producers send messages to topics.
Consumers:
- Applications that read messages from the messaging system.
- Kafka consumers subscribe to topics and process messages either individually or as part of a group.
Topics
- A topic is a category or feed to which messages are sent by producers and from which consumers retrieve messages.
- Kafka topics are partitioned for scalability and performance.
- Example:
- Topic:
user-signups
- Messages:
{"user_id": 101, "action": "signup"},{"user_id": 102, "action": "signup"}
- Messages:
- Topic:
Queues and Publish-Subscribe Models
Queue (Point-to-Point):
- Messages are delivered to a single consumer.
- Example: A task queue where only one worker processes each task.

Publish-Subscribe:
- Messages are broadcast to multiple consumers.
- Example: A topic that multiple services subscribe to, such as notifications and analytics.

Message Delivery Semantics
- Ensures how messages are delivered:
At-least-once: Messages might be delivered multiple times but never lost.
At-most-once: Messages might be lost but never duplicated.
Exactly-once: Messages are delivered precisely once (Kafka supports this with transactional guarantees).
- Ensures how messages are delivered:
2. Core Concepts in Kafka
Kafka builds on these general concepts, adding its own unique architecture and features.
and download the latest binary release.
tar -xzf kafka_2.13-<version>.tgz
cd kafka_2.13-<version>
Start Zookeeper:
- Use the built-in Zookeeper script to start Zookeeper:
bin/zookeeper-server-start.sh config/zookeeper.properties
Expected Output:
[INFO] binding to port 0.0.0.0/0.0.0.0:2181
[INFO] Snapshot taken
[INFO] Started admin server on port [8080]
If you see "binding to port" and "Started admin server," Zookeeper is running successfully.
Start Kafka Broker:
- Open a new terminal and start Kafka:
- Open a new terminal and start Kafka:
bin/kafka-server-start.sh config/server.properties
Expected Output:
Kafka server started
INFO Registered broker 0 at path /brokers/ids/0
Verify Kafka is Running:
- Test your installation as described in 3. Verifying the Installation.
### 3. Verifying the Installation
Step 1: List Existing Topics
- Test your installation as described in 3. Verifying the Installation.
bin/kafka-topics.sh --list --bootstrap-server localhost:9092
What This Does:
- Lists all topics currently managed by the Kafka broker.
- Confirms Kafka is running properly.
Expected Output:
(no output, as no topics exist yet)
Step 2: Create a Topic
bin/kafka-topics.sh --create --topic test-topic --partitions 3 --replication-factor 1 --bootstrap-server localhost:9092
What This Does:
- Creates a new topic named
test-topic. - Splits the topic into 3 partitions (for parallel processing).
- Sets the replication factor to 1 (no redundancy).
Expected Output:
Created topic test-topic.
Step 3: Describe the Topic
bin/kafka-topics.sh --describe --topic test-topic --bootstrap-server localhost:9092
What This Does:
- Displays details about the topic, including partition count, replication factor, and leader assignments.
Expected Output:
Topic: test-topic PartitionCount: 3 ReplicationFactor: 1 Configs:
Partition: 0 Leader: 0 Replicas: 0 Isr: 0
Partition: 1 Leader: 0 Replicas: 0 Isr: 0
Partition: 2 Leader: 0 Replicas: 0 Isr: 0
4. Exploring Kafka Topics
Create Multiple Topics
Let’s create two topics, user-signups and order-events, each tailored for specific use cases.
Createuser-signups:
bin/kafka-topics.sh --create --topic user-signups --partitions 2 --replication-factor 1 --bootstrap-server localhost:9092
user-signupscaptures new user registrations.- Two partitions enable parallel processing.
Expected Output:
Created topic user-signups.
Createorder-events:
bin/kafka-topics.sh --create --topic order-events --partitions 3 --replication-factor 1 --bootstrap-server localhost:9092
order-eventsrecords e-commerce transactions.- Three partitions ensure scalability for high-volume data.
Expected Output:
Created topic order-events.
List All Topics
To confirm the topics were created:
bin/kafka-topics.sh --list --bootstrap-server localhost:9092
Expected Output:
user-signups
order-events
5. Producing Messages
Send Data to user-signups:
- Start a producer:
bin/kafka-console-producer.sh --topic user-signups --bootstrap-server localhost:9092
- Enter user signup events:
{"user_id": 1, "name": "Alice", "email": "alice@example.com"}
{"user_id": 2, "name": "Bob", "email": "bob@example.com"}
- Producing user signups mimics a real-world registration system.
Send Data toorder-events: - Start another producer:
bin/kafka-console-producer.sh --topic order-events --bootstrap-server localhost:9092
- Enter order events:
{"order_id": 101, "user_id": 1, "total": 99.99}
{"order_id": 102, "user_id": 2, "total": 49.50}
- Simulates e-commerce transactions.
6. Consuming Messages
Consume Data from user-signups:
- Start a consumer:
bin/kafka-console-consumer.sh --topic user-signups --from-beginning --bootstrap-server localhost:9092
Expected Output:
{"user_id": 1, "name": "Alice", "email": "alice@example.com"}
{"user_id": 2, "name": "Bob", "email": "bob@example.com"}
Consume Data from order-events:
- Start another consumer:
bin/kafka-console-consumer.sh --topic order-events --from-beginning --bootstrap-server localhost:9092
Expected Output:
{"order_id": 101, "user_id": 1, "total": 99.99}
{"order_id": 102, "user_id": 2, "total": 49.50}
7. Simulating Real World Scenarios
Offset Management
- Produce additional messages to
order-events:
{"order_id": 103, "user_id": 1, "total": 75.00}
- Start a consumer and consume from the latest message only:
bin/kafka-console-consumer.sh --topic order-events --bootstrap-server localhost:9092
Expected Output:
{"order_id": 103, "user_id": 1, "total": 75.00}
- Rewind to read all messages:
bin/kafka-console-consumer.sh --topic order-events --from-beginning --bootstrap-server localhost:9092
Expected Output:
{"order_id": 101, "user_id": 1, "total": 99.99}
{"order_id": 102, "user_id": 2, "total": 49.50}
{"order_id": 103, "user_id": 1, "total": 75.00}
Using Consumer Groups
- Start Consumer Group A for
order-events:
bin/kafka-console-consumer.sh --topic order-events --group group-a --bootstrap-server localhost:9092
- Consumer groups allow multiple consumers to process messages in parallel.
- Produce more messages to
order-events:
{"order_id": 104, "user_id": 3, "total": 120.00}
- Start Consumer Group B for
order-events:
bin/kafka-console-consumer.sh --topic order-events --group group-b --bootstrap-server localhost:9092
Expected Outputs:
Consumer Group A: Continues consuming from its last offset.
{"order_id": 104, "user_id": 3, "total": 120.00}
Consumer Group B: Reads all messages from the beginning if it's new.
{"order_id": 101, "user_id": 1, "total": 99.99}
{"order_id": 102, "user_id": 2, "total": 49.50}
{"order_id": 103, "user_id": 1, "total": 75.00}
{"order_id": 104, "user_id": 3, "total": 120.00}
Testing Replication
- Create a topic with replication:
bin/kafka-topics.sh --create --topic replicated-topic --partitions 2 --replication-factor 2 --bootstrap-server localhost:9092
- Check the topic details:
bin/kafka-topics.sh --describe --topic replicated-topic --bootstrap-server localhost:9092
Expected Output:
Topic: replicated-topic PartitionCount: 2 ReplicationFactor: 2 Configs:
Partition: 0 Leader: 0 Replicas: 0,1 Isr: 0,1
Partition: 1 Leader: 1 Replicas: 1,0 Isr: 1,0
IV. Building Your First Kafka Application
Now that Kafka is set up and you’ve explored its features through the CLI, let’s build a simple producer and consumer application using a programming language. For this section, we’ll use Python with the kafka-python library, a popular choice for interacting with Kafka.
1. Setting Up the Environment
Step 1: Install Python
Ensure Python is installed on your system:
python --version
Expected Output:
Python 3.x.x
Step 2: Install kafka-python
Install the Kafka client library for Python:
pip install kafka-python
What This Does:
- Installs
kafka-python, which provides tools for producing and consuming messages in Kafka.
### 2. Writing a Kafka Producer
The producer sends messages to a Kafka topic.
Step 1: Create a File for the Producer
Create a new file namedproducer.pyand paste the following code:
from kafka import KafkaProducer
import json
# Initialize Kafka Producer
producer = KafkaProducer(
bootstrap_servers='localhost:9092', # Kafka broker address
value_serializer=lambda v: json.dumps(v).encode('utf-8') # Serialize messages to JSON
)
# Topic Name
topic = 'user-signups'
# Send Messages
messages = [
{"user_id": 1, "name": "Alice", "email": "[email protected]"},
{"user_id": 2, "name": "Bob", "email": "[email protected]"}
]
for message in messages:
producer.send(topic, value=message)
print(f"Sent: {message}")
# Close Producer
producer.close()
Explanation:
KafkaProducer:
- Connects to the Kafka broker (
localhost:9092). - Sends JSON-encoded messages.
- Connects to the Kafka broker (
Topic:
- Sends messages to the
user-signupstopic.
Step 2: Run the Producer
Run the script:
- Sends messages to the
python producer.py
Expected Output:
Sent: {'user_id': 1, 'name': 'Alice', 'email': '[email protected]'}
Sent: {'user_id': 2, 'name': 'Bob', 'email': '[email protected]'}
Verify in Kafka:
Start a Kafka consumer to confirm the messages:
bin/kafka-console-consumer.sh --topic user-signups --from-beginning --bootstrap-server localhost:9092
Expected Output:
user_id": 1, "name": "Alice", "email": "alice@example.com"}
{"user_id": 2, "name": "Bob", "email": "bob@example.com"}
3. Writing a Kafka Consumer
The consumer retrieves messages from a Kafka topic.
Step 1: Create a File for the Consumer
Create a new file named consumer.py and paste the following code:
from kafka import KafkaConsumer
import json
# Initialize Kafka Consumer
consumer = KafkaConsumer(
'user-signups', # Topic to subscribe to
bootstrap_servers='localhost:9092',
auto_offset_reset='earliest', # Start reading from the beginning
group_id='user-signups-group', # Consumer group
value_deserializer=lambda x: json.loads(x.decode('utf-8')) # Deserialize messages from JSON
)
# Consume Messages
print("Listening for messages...")
for message in consumer:
print(f"Received: {message.value}")
Explanation:
KafkaConsumer:
- Subscribes to the
user-signupstopic. - Starts reading from the earliest offset.
- Subscribes to the
Group ID:
- Ensures that multiple consumers can work together to process the same topic.
Step 2: Run the Consumer
Run the script:
- Ensures that multiple consumers can work together to process the same topic.
python consumer.py
Expected Output:
Listening for messages...
Received: {'user_id': 1, 'name': 'Alice', 'email': '[email protected]'}
Received: {'user_id': 2, 'name': 'Bob', 'email': '[email protected]'}
4. Enhancing the Application
Add a Producer to Another Topic
Expand the producer to send data to a second topic (order-events):
# Additional Topic
order_topic = 'order-events'
orders = [
{"order_id": 101, "user_id": 1, "total": 99.99},
{"order_id": 102, "user_id": 2, "total": 49.50}
]
for order in orders:
producer.send(order_topic, value=order)
print(f"Sent to {order_topic}: {order}")
Add a Second Consumer
Create a new consumer for the order-events topic:
# Initialize Kafka Consumer for another topic
order_consumer = KafkaConsumer(
'order-events',
bootstrap_servers='localhost:9092',
auto_offset_reset='earliest',
group_id='order-events-group',
value_deserializer=lambda x: json.loads(x.decode('utf-8'))
)
# Consume Messages
print("Listening for order events...")
for order in order_consumer:
print(f"Order Received: {order.value}")
5. Testing the Application
Step 1: Start Both Consumers
- Run
consumer.pyto listen touser-signups. - Run the new consumer for
order-events.
Step 2: Run the Producer
Runproducer.pyto send messages to both topics.
Expected Outputs:
User Signups Consumer:
CODEReceived: {'user_id': 1, 'name': 'Alice', 'email': '[email protected]'}
Received: {'user_id': 2, 'name': 'Bob', 'email': '[email protected]'}
Order Events Consumer:
CODEOrder Received: {'order_id': 101, 'user_id': 1, 'total': 99.99}
Order Received: {'order_id': 102, 'user_id': 2, 'total': 49.50}
V. Conclusion
Kafka has revolutionized how modern systems handle messaging and real-time data processing. By decoupling producers and consumers, it allows for scalable, fault-tolerant, and flexible architectures that are crucial for today’s distributed systems. Through this course, you’ve gained a foundational understanding of Kafka’s core components, such as topics, partitions, brokers, and consumer groups, as well as hands-on experience with producing and consuming messages.
As a highly versatile platform, Kafka can serve as the backbone for a wide variety of applications, including real-time analytics, event-driven systems, and log aggregation pipelines. While this course focused on the basics, Kafka’s ecosystem extends far beyond messaging. Tools like Kafka Streams, KSQL, and Kafka Connect empower developers to build powerful stream processing applications and integrate seamlessly with other systems.
Whether you’re building microservices, processing IoT data, or scaling enterprise systems, Kafka provides the tools and reliability needed for success. With this introduction as your foundation, you’re now ready to explore Kafka’s advanced features and unlock its full potential for solving complex, real-world challenges.
SOCIAL SHARE CARD GENERATOR