Flask API
Real-Time Systems
Kafka
Consumer Applications
Python Programming

Flask API as real time kafka consumer

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Apache Kafka is a popular framework for handling real-time data feeds. Flask, a lightweight and powerful Python web framework, is highly adaptable for writing APIs that consume data from Kafka.

What is Apache Kafka?

Apache Kafka is a distributed event streaming platform capable of handling trillions of events a day. Initially conceived as a messaging queue, Kafka is based on an abstraction of a distributed commit log. It enables users to publish and subscribe to streams of records, store records in a fault-tolerant way, and process them as they occur. Kafka is generally used for two broad classes of applications:

  1. Building real-time streaming data pipelines that reliably get data between systems or applications.
  2. Building real-time streaming applications that transform or react to the streams of data.

What is Flask?

Flask is a micro web framework for Python, based on Werkzeug and Jinja 2. It serves mainly to make web applications quickly with a minimal setup. It's explicitly not designed to handle asynchronous workflows like those required in a high-volume Kafka consumer, but it can manage this with the right tools and libraries.

Integrating Flask as a Kafka Consumer

To set up Flask as a real-time Kafka consumer, the followings steps can be taken:

Step 1: Setting up Kafka

Make sure you have a Kafka instance running. You can set up Kafka locally or use cloud-based services such as Confluent or AWS MSK.

bash
# Example of starting Kafka locally using the Confluent Kafka platform
$ confluent local start kafka

Step 2: Install Required Libraries

Install Flask and Kafka Python client (such as confluent_kafka or kafka-python).

bash
$ pip install Flask confluent_kafka

Step 3: Create a Flask Application

Create a simple Flask application. This application will start a consumer in a background thread.

python
1from flask import Flask
2from confluent_kafka import Consumer, KafkaError
3import threading
4
5app = Flask(__name__)
6
7def kafka_consumer():
8    c = Consumer({
9        'bootstrap.servers': 'localhost:9092',
10        'group.id': 'mygroup',
11        'auto.offset.reset': 'earliest'
12    })
13    c.subscribe(['mytopic'])
14
15    while True:
16        msg = c.poll(timeout=1.0)
17        if msg is None: continue
18        if msg.error():
19            if msg.error().code() == KafkaError._PARTITION_EOF:
20                continue
21            else:
22                print(msg.error())
23                break
24        print(f'Received message: {msg.value().decode("utf-8")}')
25
26    c.close()
27
28@app.route('/')
29def index():
30    return "Check console for kafka messages."
31
32if __name__ == "__main__":
33    thread = threading.Thread(target=kafka_consumer)
34    thread.start()
35    app.run(debug=True)

This script does the following:

  • Sets up a Kafka consumer that subscribes to topic mytopic.
  • Uses a background thread to consume messages so that the main Flask thread remains responsive.
  • Prints out messages to the console as they arrive.

Best Practices and Considerations

  • Thread Safety: Python's Global Interpreter Lock (GIL) can make it challenging to do true parallel execution. Consider using processes instead of threads if this becomes a bottleneck.
  • Kafka Client Configuration: Consumer configurations (like session timeouts, max poll intervals, etc.) should be adjusted based on your specific application needs.
  • Error Handling in Consumers: Robust error handling in the Kafka consumer logic is essential to deal with situations like connection losses, topic rebalances, etc.

Summary Table

ComponentPurposeKey Library/Tool
Apache KafkaHandles real-time data feeds and processingApache Kafka
FlaskWeb framework for API deploymentFlask
Kafka ConsumerConsumes messages from Kafka topicsconfluent_kafka
Background ThreadManages long-running Kafka consumer processthreading module

This setup demonstrates a basic integration. For production systems, more robust consumer management, possibly integrating with Kafka Streams for complex processing, and better deployment strategies are advisable.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.