Python
Kafka
Message Consumption
Containerization
Programming

Consume Kafka messages from a different container in python

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Apache Kafka is a distributed streaming platform that allows applications to publish and subscribe to streams of records. In scenarios where you are running a Kafka broker and consumers in different containers, Python can serve as a robust tool to implement the consumer logic, especially when using libraries like confluent_kafka or kafka-python. Here, we'll explore how to consume Kafka messages from a Kafka broker running in one container from another container using Python.

Understanding Kafka and Docker Containers

Before diving into the code, it's important to grasp the basics of Kafka and Docker containers:

  1. Kafka Broker: It handles the storage and dissemination of messages across the Kafka cluster.
  2. Kafka Consumer: An entity that subscribes to one or more topics and processes the stream of records produced to them.

Containers, such as those managed by Docker, offer a lightweight form of virtualization, allowing services like Kafka to run in isolated environments, thus making them portable and easy to deploy.

Docker Network Considerations

When setting up Kafka and consumers in separate containers, ensure they can communicate over the same Docker network. This setup allows containers to discover and communicate with each other through service names rather than IP addresses, which are dynamically assigned.

Python Package for Kafka Consumption

For consuming messages from Kafka using Python, we mainly use the confluent_kafka package, which is both high-performance and feature-rich. To install it, run:

bash
pip install confluent-kafka

Python Kafka Consumer Example

Here's a simple example of how a Python application can consume messages from Kafka running in another container:

python
1from confluent_kafka import Consumer, KafkaError
2
3# Configuration for Kafka Consumer
4conf = {
5    'bootstrap.servers': 'kafka:9092',  # Assuming 'kafka' is the service name of the Kafka broker in your docker-compose or Docker network
6    'group.id': 'mygroup',
7    'auto.offset.reset': 'earliest'
8}
9
10# Create Consumer instance
11consumer = Consumer(**conf)
12consumer.subscribe(['mytopic'])
13
14try:
15    while True:
16        msg = consumer.poll(timeout=1.0)
17        if msg is None:
18            continue
19        if msg.error():
20            if msg.error().code() == KafkaError._PARTITION_EOF:
21                continue
22            else:
23                print(msg.error())
24                break
25        print('Received message: {}'.format(msg.value().decode('utf-8')))
26
27except KeyboardInterrupt:
28    pass
29finally:
30    consumer.close()

In this script, replace 'kafka:9092' with the actual host and port where your Kafka broker can be accessed within your Docker network.

Handling Consumer Configurations

There are several configurations available for Kafka Consumers that are crucial for efficient message consumption:

  • bootstrap.servers: List of brokers used to connect to the Kafka cluster.
  • group.id: Identifier for the consumer group; allows consumption from multiple consumers in parallel.
  • auto.offset.reset: Determines where to start reading messages if no initial offset is found.

Integrating in Docker-compose

Within a Docker-compose setup, you should define services for both Kafka and the Python application. Here is a minimal example:

yaml
1version: '3'
2services:
3  kafka:
4    image: confluentinc/cp-kafka:latest
5    ports:
6      - "9092:9092"
7    environment:
8      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092
9
10  python-consumer:
11    build: .
12    depends_on:
13      - kafka

In your Python application's Dockerfile, make sure to use the correct base image and include steps to install necessary packages.

Key Points Summary

FeatureDescription
Kafka Consumer Libraryconfluent_kafka provides robust integration.
ConfigurationIncludes server, consumer group, and offset settings.
Docker IntegrationUse Docker-compose for efficient service management.

By understanding Kafka's integration with Docker and utilizing Python's packages, you can effectively manage message consumption across containerized applications. This setup not only provides scalability but also ensures that services are loosely coupled and maintainable.


Course illustration
Course illustration

All Rights Reserved.