2022-06-23 18:00:00+00:00

In high-throughput event-driven microservices, Apache Kafka is the industry standard for distributing messages. If your Python backend services use synchronous Kafka libraries (like confluent-kafka) inside an asynchronous web framework (like FastAPI or Sanic), the main event loop will block during message writes, degrading web server performance. aiokafka solves this by providing native asyncio support for Kafka consumer and producer loops.

By implementing non-blocking producer clients and message consumer loops, we can build scalable event-driven backends.


1. Writing the Asynchronous Kafka Producer

We initialize the producer inside FastAPI's startup event hooks and send events asynchronously during requests:

# kafka_producer.py
from aiokafka import AIOKafkaProducer
from fastapi import FastAPI
import json

app = FastAPI()
producer = None

@app.on_event("startup")
async def startup_event():
    global producer
    producer = AIOKafkaProducer(bootstrap_servers='localhost:9092')
    await producer.start()

@app.on_event("shutdown")
async def shutdown_event():
    await producer.stop()

@app.post("/events/publish")
async def publish_event(topic: str, data: dict):
    payload = json.dumps(data).encode('utf-8')
    # Send message non-blockingly
    await producer.send_and_wait(topic, payload)
    return {"status": "published"}

2. Running the Async Consumer Loop

We run a background task loop that continuously consumes messages from a topic and processes them, maintaining system throughput.

Idempotent Consumer Offsets with `aiokafka`

Disable enable_auto_commit in AIOKafkaConsumer and commit offsets only after the downstream PostgreSQL transaction or Elasticsearch bulk index succeeds—guaranteeing at-least-once delivery without silent message loss. See Freelance Event-Driven Systems Programmer Guide.


About the Author — Hire Freelance Programmer Aldian Fazrihady

Aldian Fazrihady is a Top-Rated Senior Freelance Programmer, Freelance Full Stack Developer, and AI Application Engineer with 15+ years of production software engineering experience and Google Cloud Professional Cloud Architect & Data Engineer certifications. He specializes in building custom LLM & autonomous AI agents, RAG pipelines, Next.js/React PWAs, Python (Django/FastAPI), Go microservices, and serverless cloud infrastructure on Google Cloud Run and AWS.

Hire Aldian Fazrihady on Upwork | Book 1-Day Programming Service ($100 USD via Polar) | View Live AI Portfolio | Portfolio Directory | Send a Direct Message

Related Engineering Articles

Freelance Programmer Specializations & Category Hubs