Event-Driven CQRS for Generative AI: Scaling Asynchronous LLM Workflows with Apache Kafka, Flink, and Redis

Event-Driven CQRS for Generative AI: Scaling Asynchronous LLM Workflows with Apache Kafka, Flink, and Redis

๐Ÿ“– 4 min read

Deploying Large Language Models behind traditional synchronous REST or gRPC request-response cycles is a primary source of production instability.

Autoregressive token generation introduces non-deterministic execution times ranging from 800 milliseconds to over 60 seconds. Under peak traffic spikes, synchronous architectures suffer from:

  • Gateway Timeouts (HTTP 504): Load balancers (NGINX, AWS ALB, Cloudflare) sever HTTP connections after 15โ€“30 seconds, leaving downstream GPUs burning tokens for a client that has already disconnected.
  • Socket Pool Exhaustion: Long-lived synchronous requests hold server threads and open file descriptors, starving other microservices.
  • No Backpressure Buffer: A burst of 5,000 concurrent user requests immediately hammers the underlying LLM provider, triggering catastrophic HTTP 429 rate limits and service degradation.

To build horizontally scalable, resilient AI platforms, enterprise software systems must adopt Command Query Responsibility Segregation (CQRS) combined with Event-Driven Architecture (EDA).

This guide provides the complete architectural blueprint and production code for decoupling AI inference using FastAPI, Apache Kafka, Apache Flink, and Redis Streams.


1. The CQRS Event-Driven Architecture

In this architecture, the Command Side (Write Model) and the Query Side (Read Model) are completely segregated:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ COMMAND PATH (Write Model - Latency < 5ms)                                             โ”‚
โ”‚                                                                                        โ”‚
โ”‚ Client Browser โ”€โ”€โ–บ POST /v1/jobs โ”€โ”€โ–บ FastAPI Gateway (Validates schema via Pydantic)   โ”‚
โ”‚                                           โ”‚                                            โ”‚
โ”‚                                           โ–ผ Emits JobRequestedEvent                    โ”‚
โ”‚                                  Apache Kafka Topic:                                   โ”‚
โ”‚                                  `ai.generation.requests`                              โ”‚
โ”‚                                  (Partitioned by tenant_id)                            โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                            โ”‚
                                            โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ STREAM GOVERNANCE & TELEMETRY LAYER                                                    โ”‚
โ”‚                                                                                        โ”‚
โ”‚ Apache Flink Stateful Processor:                                                       โ”‚
โ”‚ - Sliding-window rate limiting per tenant                                              โ”‚
โ”‚ - Budget quota monitoring (Enforces hard dollar stop before GPU execution)             โ”‚
โ”‚ - Deadlock & runaway loop detection                                                    โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                            โ”‚ Dispatched to Healthy Workers
                                            โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ ASYNCHRONOUS WORKER CLUSTER (vLLM / TensorRT-LLM / Cloud APIs)                         โ”‚
โ”‚                                                                                        โ”‚
โ”‚ GPU Worker Pool (Consumes from Kafka):                                                 โ”‚
โ”‚ 1. Executes autoregressive generation                                                  โ”‚
โ”‚ 2. Publishes token chunks to Redis Streams: `stream:job:{job_id}`                       โ”‚
โ”‚ 3. Publishes completion telemetry to Kafka: `ai.generation.completed`                  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                            โ”‚
                                            โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ QUERY PATH (Read Model - Real-Time Low Latency)                                        โ”‚
โ”‚                                                                                        โ”‚
โ”‚ Client Browser โ—„โ”€โ”€ Server-Sent Events (SSE) โ—„โ”€โ”€ FastAPI SSE Gateway โ—„โ”€โ”€ Redis Streams   โ”‚
โ”‚ (User receives fluid, sub-20ms real-time token streaming with zero dropped connections)โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

2. The Command Side: FastAPI & Kafka Event Producer

When a client submits an AI generation request, the API gateway validates the request with Pydantic, generates an immutable job_id, dispatches an event to Kafka, and immediately returns an HTTP 202 Accepted response with the job tracking URL in under 4 milliseconds:

import uuid
import json
from datetime import datetime, timezone
from fastapi import FastAPI, HTTPException, status
from pydantic import BaseModel, Field
from aiokafka import AIOKafkaProducer

app = FastAPI(title="Event-Driven AI Command Gateway")

# Global Kafka Producer instance
producer: AIOKafkaProducer = None

@app.on_event("startup")
async def startup_event():
    global producer
    producer = AIOKafkaProducer(
        bootstrap_servers="kafka.internal.vpc:9092",
        client_id="ai-command-gateway",
        acks="all",  # Guaranteed write durability
        enable_idempotence=True
    )
    await producer.start()

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

class CreateJobRequest(BaseModel):
    tenant_id: str
    prompt: str = Field(..., max_length=10000)
    model: str = Field(default="gemini-2.5-flash")
    temperature: float = Field(default=0.2, ge=0.0, le=1.0)
    max_tokens: int = Field(default=1024, le=4096)

class JobAcceptedResponse(BaseModel):
    job_id: str
    status: str
    stream_url: str

@app.post("/v1/jobs", status_code=status.HTTP_202_ACCEPTED, response_model=JobAcceptedResponse)
async def submit_ai_job(req: CreateJobRequest):
    job_id = f"job_{uuid.uuid4().hex}"
    
    event_payload = {
        "job_id": job_id,
        "tenant_id": req.tenant_id,
        "prompt": req.prompt,
        "model": req.model,
        "temperature": req.temperature,
        "max_tokens": req.max_tokens,
        "created_at": datetime.now(timezone.utc).isoformat()
    }

    # Partition by tenant_id to guarantee per-tenant FIFO ordering
    await producer.send_and_wait(
        topic="ai.generation.requests",
        key=req.tenant_id.encode("utf-8"),
        value=json.dumps(event_payload).encode("utf-8")
    )

    return JobAcceptedResponse(
        job_id=job_id,
        status="QUEUED",
        stream_url=f"/v1/jobs/{job_id}/stream"
    )

3. The Worker Pool: Consuming Kafka & Streaming to Redis

Asynchronous worker nodes consume from Kafka, execute LLM inference, and stream generated token deltas directly into Redis Streams:

import asyncio
import json
from aiokafka import AIOKafkaConsumer
import redis.asyncio as aioredis
from google import genai

async def start_worker():
    consumer = AIOKafkaConsumer(
        "ai.generation.requests",
        bootstrap_servers="kafka.internal.vpc:9092",
        group_id="llm-worker-cluster",
        auto_offset_reset="earliest",
        enable_auto_commit=False  # Manual commit ensures crash resilience
    )
    await consumer.start()

    r = aioredis.from_url("redis://redis.internal.vpc:6379")
    gemini_client = genai.Client()

    try:
        async for msg in consumer:
            job_data = json.loads(msg.value.decode("utf-8"))
            job_id = job_data["job_id"]
            stream_key = f"stream:job:{job_id}"

            try:
                # Execute streaming generation
                response = gemini_client.models.generate_content_stream(
                    model=job_data["model"],
                    contents=job_data["prompt"]
                )

                for chunk in response:
                    # Append token delta to Redis Stream
                    await r.xadd(
                        stream_key,
                        {"delta": chunk.text, "is_final": "0"}
                    )

                # Signal stream completion
                await r.xadd(stream_key, {"delta": "", "is_final": "1"})
                await r.expire(stream_key, 3600)  # 1 hour retention TTL

                # Commit Kafka offset only after successful completion
                await consumer.commit()

            except Exception as e:
                # If generation crashes (e.g. OOM or 503), do not commit offset
                print(f"Worker failure on {job_id}: {e}")
                # Worker will retry or forward to Dead-Letter Queue (DLQ)
                await asyncio.sleep(2)

    finally:
        await consumer.stop()
        await r.close()

4. The Query Side: Real-Time SSE Streaming from Redis

The client browser connects to the read model via Server-Sent Events (SSE). The endpoint reads the token stream from Redis and yields chunks with sub-millisecond overhead:

from fastapi.responses import StreamingResponse

@app.get("/v1/jobs/{job_id}/stream")
async def stream_job_results(job_id: str):
    r = aioredis.from_url("redis://redis.internal.vpc:6379")

    async def event_generator():
        stream_key = f"stream:job:{job_id}"
        last_id = "0-0"

        while True:
            # XREAD block for up to 5 seconds waiting for new token deltas
            entries = await r.xread({stream_key: last_id}, count=5, block=5000)

            if not entries:
                continue

            for stream, messages in entries:
                for message_id, data in messages:
                    last_id = message_id
                    delta = data.get(b"delta", b"").decode("utf-8")
                    is_final = data.get(b"is_final", b"0").decode("utf-8")

                    yield f"data: {json.dumps({'delta': delta})}\n\n"

                    if is_final == "1":
                        yield "data: [DONE]\n\n"
                        await r.close()
                        return

    return StreamingResponse(event_generator(), media_type="text/event-stream")

5. Architectural Comparison: Synchronous vs Event-Driven CQRS

Architectural Attribute Synchronous REST / gRPC Event-Driven CQRS (Kafka + Redis)
Client Initial Latency 1,500ms โ€“ 45,000ms 3ms โ€“ 5ms (Immediate 202 Accepted)
Worker Crash Resilience Total request loss; client gets 500/504 Automatic Kafka retry with zero data loss
Surge Traffic Spikes Cascading failure; GPU thrashing Smooth Kafka buffer queueing
Client Disconnect Waste GPUs continue burning unneeded tokens Flink consumer cancels abandoned jobs
Audit Compliance Trail Dispersed application logs Immutable Kafka event log (SOC2 / HIPAA ready)
Infrastructure Complexity Low (Standard HTTP API) High (Requires Kafka, Flink, and Redis clusters)

6. Production Best Practices for Enterprise AI Streams

  1. Partition by tenant_id: Always use tenant_id as the Kafka message key. This guarantees that requests from a single customer are processed in order, preventing newer requests from starving older jobs.
  2. Apply Dead-Letter Queues (DLQ): Configure a retry topic (ai.generation.retry) capped at 3 attempts. Malformed prompts that repeatedly trigger safety filter errors are diverted to ai.generation.dlq for human engineering review.
  3. Set Short TTLs on Redis Streams: Token streams in Redis are ephemeral transport vehicles. Always apply an expiry (EXPIRE stream:job:{id} 3600) so inactive streams donโ€™t consume memory.
WEEKLY NEWSLETTER

Get Weekly AI Architect Cost & Strategy Updates

Join 14,000+ developers receiving weekly, data-driven cost-reduction blueprints and production-ready agent guidelines.

comments powered by Disqus