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 is consumed by exactly one consumer within the group. If you have a topic with 4 partitions, you can have up to 4 consumers in a group, each reading from one partition, to achieve maximum parallelism.

Kafka Producers, Consumers, and Partitions

This architecture allows Kafka to achieve massive scale. You can increase the number of partitions for a topic and add more consumers to a consumer group to scale up your read throughput as your data volume grows.

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:

  1. A running Kafka instance (e.g., via Docker).
  2. The Python library installed: 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}")
    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