Updated 5 min read

Kafka Tutorial for Beginners: Topics, Producers, Consumers

Kafka tutorial for beginners covering Apache Kafka basics, topics, partitions, producers, consumers, brokers, event streaming, and real-world use cases.

kafka kafka-tutorial kafka-for-beginners distributed-systems tutorial beginner streaming microservices
On this page · 11 sections
  1. What is Apache Kafka?
  2. Why Use Kafka?
  3. Common Use Cases
  4. Getting Started with Kafka
  5. Understanding Topics and Partitions
  6. Producer and Consumer Basics
  7. Consumer Groups
  8. Example: user activity tracking
  9. Habits worth starting with
  10. Common Pitfalls to Avoid
  11. Where to go next

Kafka is a distributed log: producers append events, consumers read them at their own pace, and the events stay on disk for as long as you configure. LinkedIn built it to move activity data between systems, and it is now the default choice for event pipelines.

This tutorial covers the terms you need on day one (topics, partitions, brokers, producers, consumers, offsets and consumer groups) and ends with a small working example you can run locally.

What is Apache Kafka?

Apache Kafka is a distributed event streaming platform designed to handle high-throughput, fault-tolerant data pipelines. Think of it as a super-fast, reliable messaging system that can process millions of messages per second.

Key Concepts

Topics: Categories or feeds where messages are published Partitions: Topics are split into partitions for parallel processing Producers: Applications that send messages to topics Consumers: Applications that read messages from topics Brokers: Kafka servers that store and manage topics

For how these pieces fit together (replication, the KRaft controller and failover), see Kafka architecture explained.

Why Use Kafka?

1. High Throughput

Kafka can handle millions of messages per second, making it perfect for real-time data processing.

2. Fault Tolerance

Data is replicated across multiple brokers, ensuring no data loss even if servers fail.

3. Scalability

Easily scale by adding more brokers and consumers to handle increased load.

4. Real-time Processing

Process data as it arrives, enabling real-time analytics and decision-making.

Common Use Cases

1. Real-time Analytics

Process user interactions, clicks, and events in real-time for immediate insights.

2. Log Aggregation

Collect logs from multiple services into a centralized system for monitoring and analysis.

3. Event Sourcing

Store all changes to application state as a sequence of events.

4. Microservices Communication

Enable loose coupling between microservices through event-driven architecture.

Getting Started with Kafka

Installation

# Download Kafka
wget https://downloads.apache.org/kafka/2.8.1/kafka_2.13-2.8.1.tgz
tar -xzf kafka_2.13-2.8.1.tgz
cd kafka_2.13-2.8.1

Starting Kafka

# Start Zookeeper
bin/zookeeper-server-start.sh config/zookeeper.properties

# Start Kafka (in a new terminal)
bin/kafka-server-start.sh config/server.properties

Creating Your First Topic

# Create a topic named 'my-first-topic'
bin/kafka-topics.sh --create --topic my-first-topic --bootstrap-server localhost:9092 --partitions 3 --replication-factor 1

Producing Messages

# Start a producer
bin/kafka-console-producer.sh --topic my-first-topic --bootstrap-server localhost:9092

Consuming Messages

# Start a consumer
bin/kafka-console-consumer.sh --topic my-first-topic --from-beginning --bootstrap-server localhost:9092

Understanding Topics and Partitions

Topics

Topics are like database tables or folders where messages are stored. Each topic has a name and can be divided into multiple partitions.

Partitions

Partitions allow Kafka to:

  • Distribute data across multiple brokers
  • Enable parallel processing
  • Scale horizontally

Partition Keys

When producing messages, you can specify a key that determines which partition the message goes to:

# Messages with the same key go to the same partition
producer.send('user-events', key='user123', value='user logged in')
producer.send('user-events', key='user123', value='user clicked button')

Producer and Consumer Basics

Producer

Producers send messages to Kafka topics. They can:

  • Choose which partition to send messages to
  • Batch messages for better performance
  • Handle acknowledgments and retries

Producers in the Kafka course covers acks, retries, idempotence and batching settings.

Consumer

Consumers read messages from topics. They can:

  • Read from specific partitions
  • Join consumer groups for parallel processing
  • Commit offsets to track progress

Consumer Groups

Consumer groups allow multiple consumers to work together to process messages from a topic:

# Start multiple consumers in the same group
bin/kafka-console-consumer.sh --topic my-first-topic --group my-consumer-group --bootstrap-server localhost:9092

Each message is delivered to only one consumer in the group, enabling parallel processing. A group can’t usefully have more consumers than the topic has partitions; extra ones sit idle. Kafka consumer groups: offsets and threads covers when to commit offsets, delivery semantics and the consumer settings worth changing.

Example: user activity tracking

Let’s build a simple user activity tracking system:

1. Create Topics

# User events topic
bin/kafka-topics.sh --create --topic user-events --bootstrap-server localhost:9092 --partitions 3 --replication-factor 1

# Analytics topic
bin/kafka-topics.sh --create --topic analytics --bootstrap-server localhost:9092 --partitions 3 --replication-factor 1

2. Producer Code (Python)

from kafka import KafkaProducer
import json
import time

producer = KafkaProducer(
    bootstrap_servers=['localhost:9092'],
    value_serializer=lambda x: json.dumps(x).encode('utf-8')
)

# Simulate user events
events = [
    {'user_id': 'user1', 'action': 'login', 'timestamp': time.time()},
    {'user_id': 'user2', 'action': 'click', 'page': '/products', 'timestamp': time.time()},
    {'user_id': 'user1', 'action': 'purchase', 'amount': 99.99, 'timestamp': time.time()}
]

for event in events:
    producer.send('user-events', value=event)
    print(f"Sent: {event}")

producer.close()

3. Consumer Code (Python)

from kafka import KafkaConsumer
import json

consumer = KafkaConsumer(
    'user-events',
    bootstrap_servers=['localhost:9092'],
    value_deserializer=lambda m: json.loads(m.decode('utf-8'))
)

for message in consumer:
    event = message.value
    print(f"Received: {event}")
    
    # Process the event (e.g., update analytics)
    if event['action'] == 'purchase':
        print(f"Processing purchase: ${event['amount']}")

Habits worth starting with

1. Start Simple

Begin with single-broker setups before moving to clusters.

2. Understand Partitioning

Choose partition keys carefully to ensure even distribution.

3. Monitor Performance

Use Kafka’s built-in metrics to monitor throughput and latency.

4. Handle Failures

Implement proper error handling and retry logic.

5. Plan for Scale

Design your topics and partitions with future growth in mind.

Common Pitfalls to Avoid

1. Too Many Partitions

More partitions don’t always mean better performance. Start with 3-6 partitions per topic.

2. Ignoring Consumer Lag

Consumer lag is the log-end offset minus the group’s committed offset, per partition. kafka-consumer-groups.sh --describe --group <name> prints it; alert when it stays high and keeps growing, not on every burst. The consumer lag lesson has a tested script and the alert rule.

3. Poor Key Selection

Random keys can lead to uneven partition distribution.

4. Not Planning Retention

Set appropriate retention policies for your data.

5. No Schema on a Shared Topic

The producer above sends purchase events with an amount field and login events without one, and the consumer only works because it checks action first. Plain JSON has no contract: the day someone renames amount to total, every consumer reading event['amount'] raises KeyError, and the bad records stay in the topic for the whole retention period. Once more than one team reads a topic, put the format in a Kafka Schema Registry, which rejects an incompatible schema when the producer registers it instead of when a consumer reads it.

Where to go next

Start the free Kafka course at lesson 1. If you want the big picture first, Kafka architecture explained shows how brokers, partitions and replicas fit together on one page.

AlgoSimplified logo

AlgoSimplified

Practical engineering courses and technical notes

Share: X LinkedIn

Related articles