MOBI BOOT CAMP CORP. logoLearning Buddy
  • SIGN IN
  • Foundations
  • The Hadoop Ecosystem: Batch at Scale
  • The Spark Ecosystem: In-Memory Processing
  • Data Pipelines and Transport
    • Apache Kafka
    • Data Pipeline Concepts
    • Apache Beam: A Unified Model
    • Building a GCP Data Pipeline
    • Slides
  • Search & Information Retrieval
  • The Modern Data Stack
  • Glossary

Apache Kafka: The Distributed Event Streaming Platform

So far, we have focused on processing data-at-rest—data that has been collected and stored in a file system like HDFS. However, many modern use cases require processing data-in-motion, or streaming data, as it is generated.

Use cases for stream processing include:

  • Real-time fraud detection in financial transactions
  • Live monitoring of sensor data from IoT devices
  • Instantaneous updates in online gaming and social media feeds
  • Real-time analytics for stock trading systems

Apache Kafka is the de facto open-source standard for handling this type of data. It is a distributed event streaming platform capable of handling trillions of events a day. Originally created at LinkedIn, Kafka is now used by thousands of companies for high-performance data pipelines, streaming analytics, and mission-critical applications.

Why Kafka? Not Just a Messaging Queue

While Kafka is built on the publish-subscribe messaging pattern, it is much more than a traditional message broker like RabbitMQ or ActiveMQ. Kafka was designed from the ground up for:

  • High Throughput: It can handle a massive volume of messages per second.
  • Scalability: It runs as a distributed cluster of servers that can be scaled out horizontally.
  • Durability and Fault Tolerance: Messages are persisted to disk and replicated within the cluster, preventing data loss.
  • Stream Processing: Kafka is not just for transport; its Streams API allows for real-time processing of data as it flows through the system.

Core Concepts of Kafka

To understand Kafka, you need to know its fundamental components:

  • Event (or Message): The basic unit of data in Kafka. It's a simple key-value pair with a timestamp. Think of it as a single record or a row in a database table.
  • Topic: A particular stream of events. A topic is a category or feed name to which events are published. For example, you might have a user_clicks topic or a payment_transactions topic.
  • Producer: An application that publishes (writes) a stream of events to one or more Kafka topics.
  • Consumer: An application that subscribes to (reads and processes) a stream of events from one or more Kafka topics.
  • Broker: A single Kafka server. A Kafka cluster is composed of multiple brokers working together.

How it Works: Topics, Partitions, and Consumer Groups

The real power of Kafka's scalability comes from how it manages topics.

  • Partitions: A topic is broken down into one or more partitions. A partition is an ordered, immutable log of events to which producers append data. Each event within a partition is assigned a unique sequential ID called an offset.
  • Parallelism: By splitting a topic into partitions, Kafka can distribute the load for that topic across multiple brokers in the cluster. This allows multiple consumers to read from a topic in parallel.
  • Consumer Groups: Consumers work together in a consumer group. Each partition of a topic is assigned to exactly one consumer within the group. There is no physical limit to the number of independent consumer groups you can have reading from a topic, but within any single consumer group, the effective upper limit of active consumers is equal to the number of partitions (any extra consumers in that group will remain idle).

Kafka Producers, Consumers, and Partitions

Note

In the diagram above, the single topic user-activity is divided into 4 partitions, while the consumer group contains only 3 consumers. Because the partitions outnumber the consumers, Consumer 1 is automatically assigned to read from both Partition 1 and Partition 2 to ensure no data is missed. If a 4th consumer were added to the group, Kafka would automatically rebalance and assign exactly one partition to each of the 4 consumers. If a 5th consumer were added, it would remain idle.

Kafka in Practice: A Python Example

Here is a simple example of a Kafka producer and consumer written in Python using the popular kafka-python library.

Prerequisites & Setup:

  1. Download & Install Kafka:
    • You can download the binary downloads directly from the official Apache Kafka Downloads Page.
    • For quick local development, running Kafka via Docker is highly recommended.
  2. Official Documentation:
    • Refer to the Apache Kafka Documentation and the Kafka Quickstart Guide for step-by-step installation instructions.
  3. Python Environment:
    • Install the client library: pip install kafka-python.

The Producer (producer.py)

This script connects to Kafka and sends 10 messages to a topic named user-activity.

from kafka import KafkaProducer
import json
import time

# Create an instance of the Kafka producer
producer = KafkaProducer(
    bootstrap_servers="localhost:9092",
    # Encode messages as JSON
    value_serializer=lambda v: json.dumps(v).encode("utf-8"),
)

print("Sending messages...")
for i in range(10):
    # Create a message
    message = {"user_id": f"user_{i % 3}", "action": "click", "timestamp": time.time()}

    # Send the message to the 'user-activity' topic
    producer.send("user-activity", value=message)
    print(f"Sent: {message}")
    # Sleep for 1 second to simulate real-time user clicks
    time.sleep(1)

# Ensure all messages are sent before exiting
producer.flush()
print("All messages sent.")

The Consumer (consumer.py)

This script connects to Kafka, subscribes to the user-activity topic, and prints any messages it receives.

from kafka import KafkaConsumer
import json

# Create an instance of the Kafka consumer
consumer = KafkaConsumer(
    "user-activity",  # The topic to subscribe to
    bootstrap_servers="localhost:9092",
    auto_offset_reset="earliest",  # Start reading at the earliest message
    group_id="my-consumer-group",  # A unique name for the consumer group
    # Decode messages from JSON
    value_deserializer=lambda m: json.loads(m.decode("utf-8")),
)

print("Waiting for messages...")
# The consumer is an iterator that will block and wait for new messages
for message in consumer:
    # message value and key are raw bytes -- decode if necessary!
    # e.g., for unicode: `message.value.decode('utf-8')`
    print(
        f"Received: Topic={message.topic}, Partition={message.partition}, Offset={message.offset}, Key={message.key}, Value={message.value}"
    )

How to Run It:

  1. Make sure your Kafka broker is running.
  2. Open a terminal and run the consumer script: python consumer.py. It will start and wait for messages.
  3. Open a second terminal and run the producer script: python producer.py.
  4. Observe the output in the consumer's terminal. You will see the messages appear as the producer sends them.
Privacy Policy | Terms & Conditions