Skip to content
kafka-consumer-implementation logo

Kafka Consumer Implementation

kafka-consumer-implementation

Implement type-safe Kafka consumers for event consumption with msgspec deserialization. Use when building async consumers that process domain events (order messages, transactions) with offset management, error recovery, graceful shutdown, and distributed tracing. Handles consumer configuration, m...

SKILL.md

Full skill instructions

Kafka Consumer Implementation

Purpose

Implement production-grade Kafka consumers that reliably consume and process domain events with high performance, type safety, and comprehensive error recovery. Covers msgspec deserialization, confluent-kafka configuration, offset management, OpenTelemetry tracing, and anti-corruption layer patterns for translating message schemas to domain models.

When to Use This Skill

Use when building event-driven systems that consume domain events from Kafka topics with "implement Kafka consumer", "consume events from Kafka", "process order messages", or "set up event consumer".

Do NOT use when mocking Kafka consumers in unit tests (use pytest-adapter-integration-testing), implementing producers (use kafka-producer-implementation), or testing with testcontainers (use kafka-integration-testing).

Quick Start

Create a high-performance Kafka consumer in 3 steps:

  1. Define message schema:
import msgspec

class OrderEventMessage(msgspec.Struct, frozen=True):
    order_id: str
    created_at: str
    customer_name: str
    total_price: float
  1. Implement consumer:
from confluent_kafka import Consumer
import msgspec

class OrderEventConsumer:
    def __init__(self, brokers: list[str], topic: str, group_id: str) -> None:
        config = {
            "bootstrap.servers": ",".join(brokers),
            "group.id": group_id,
            "auto.offset.reset": "earliest",
            "enable.auto.commit": False,
        }
        self.consumer = Consumer(config)
        self.consumer.subscribe([topic])
        self.decoder = msgspec.json.Decoder(OrderEventMessage)

    def consume(self, timeout: float = 1.0) -> OrderEventMessage | None:
        msg = self.consumer.poll(timeout)
        if msg is None or msg.error():
            return None
        return self.decoder.decode(msg.value())

    def commit(self) -> None:
        self.consumer.commit(asynchronous=False)
  1. Use in application:
consumer = OrderEventConsumer(["localhost:9092"], "orders", "loader")
message = consumer.consume()
if message:
    process(message)
    consumer.commit()

Implementation Steps

1. Consumer Configuration

Key configuration for exactly-once processing:

config = {
    "bootstrap.servers": ",".join(brokers),
    "group.id": group_id,
    "auto.offset.reset": "earliest",  # Start from beginning
    "enable.auto.commit": False,  # Manual offset management
    "session.timeout.ms": 300000,  # 5 minute timeout
    "max.poll.interval.ms": 300000,  # 5 minutes for processing
}

2. Consumer Adapter

Implement consumer with error handling:

  • msgspec deserialization (10-20x faster than Pydantic)
  • OpenTelemetry distributed tracing
  • Manual offset management for exactly-once semantics
  • Comprehensive error logging

See references/​detailed-implementation.md for complete consumer adapter code.

3. Anti-Corruption Layer

Translate Kafka messages to domain entities:

class OrderEventTranslator:
    @staticmethod
    def to_domain_order(message: OrderEventMessage) -> Order:
        # Validate
        if not message.order_id:
            raise ValueError("order_id is required")

        # Convert types (str -> OrderId, float -> Money)
        created_at = datetime.fromisoformat(message.created_at)
        order_id = OrderId(message.order_id)
        total_price = Money(Decimal(str(message.total_price)))

        return Order(order_id, created_at, message.customer_name, total_price, [])

4. Processing Loop

Main consumer loop pattern:

  1. Poll for messages (5s timeout)
  2. Translate to domain objects
  3. Process (load into storage)
  4. Commit offset (only after success)
  5. Handle errors without stopping loop

See references/​detailed-implementation.md for complete processing loop code.

5. Lifecycle Management

Use context managers for clean shutdown:

@asynccontextmanager
async def managed_consumer(brokers, topic, group_id):
    consumer = OrderEventConsumer(brokers, topic, group_id)
    try:
        yield consumer
    finally:
        consumer.close()

Requirements

  • confluent-kafka>=2.3.0 - Production-grade Kafka client
  • msgspec>=0.18.6 - Ultra-fast deserialization
  • structlog>=23.2.0 - Structured logging
  • opentelemetry-api>=1.22.0 - Distributed tracing
  • Kafka/​Redpanda broker running (3.x or later)
  • Python 3.11+ with type checking enabled

Consumer Groups and Offset Management

Consumer Groups: Consumers in the same group share responsibility for topic partitions. Kafka automatically rebalances when members join/​leave.

Manual Offset Management (exactly-once-per-restart):

  1. Disable auto-commit: "enable.auto.commit": False
  2. Commit only after successful processing: consumer.commit()
  3. Offset reset behavior: "auto.offset.reset": "earliest" starts from beginning

See references/​detailed-implementation.md for complete offset management patterns, consumer lag monitoring, and rebalancing behavior.

Error Handling

Key error handling strategies:

  • Deserialization failures: Log error, commit offset to skip poison pill
  • Processing failures: Don't commit, message will be retried on restart
  • Commit failures: Log error, continue (will retry on next message)

See references/​error-handling.md for comprehensive error handling strategies and dead letter queue patterns.

Testing

Use testcontainers for integration tests:

from testcontainers.kafka import KafkaContainer

@pytest.fixture
def kafka_container():
    with KafkaContainer() as kafka:
        yield kafka

def test_consumer_roundtrip(kafka_container):
    brokers = [kafka_container.get_bootstrap_server()]
    # Test consumer/​producer workflow

See examples/​integration-examples.md for complete integration test patterns.

See Also

  • references/​detailed-implementation.md - Complete consumer adapter and processing loop code
  • references/​error-handling.md - Comprehensive error handling strategies
  • examples/​integration-examples.md - Real-world integration patterns
  • kafka-producer-implementation skill - For producing events
  • kafka-schema-management skill - For schema design

More skills from majiayu000

xiaohongshu logo
majiayu000/claude-arsenal

xiaohongshu

xiaohongshu

286 148
View
agent-task-conductor logo
majiayu000/claude-skill-registry

agent-task-conductor

Conduct multi-agent task orchestration and workflow coordination.

663 1
View
conductor-setup logo
majiayu000/claude-skill-registry

conductor-setup

Initialize project with Conductor artifacts (product definition,

663 1
View
animation-designer logo
majiayu000/claude-skill-registry

animation-designer

Expert in web animations, transitions, and motion design using Framer Motion and CSS

663 1
View
diagramming logo
majiayu000/claude-skill-registry

diagramming

Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...

663 1
View
h3-pg logo
majiayu000/claude-skill-registry-data

h3-pg

PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.

23 1
View
conductor-development logo
majiayu000/claude-skill-registry

conductor-development

Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...

663 1
View
conductor-status logo
majiayu000/claude-skill-registry

conductor-status

Display project status, active tracks, and next actions

663 1
View
dockerization logo
majiayu000/claude-skill-registry

dockerization

Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.

663 1
View

Popular AI tools

Kaiber logo
Video

Kaiber

Generate, edit, and beat-sync AI video with leading models in one workspace.

Paid
View
Vimcal logo
Productivity

Vimcal

The world's fastest calendar for remote work

Free
View

Transform Your Design with AI Designer by ImgCreator.ai

Freemium
View
Akool AI logo
Content & writing

Akool AI

Revolutionizing Video Production with AI-Powered Creativity

Paid
View

Extend an image past the frame and let AI fill the new aspect ratio.

Freemium
View
StarByFace logo
Security

StarByFace

Discover your celebrity doppelgänger with StarByFace!

Free
View
C

ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.

Freemium
View
Opus Clip logo
Coding & apps

Opus Clip

Opus.ai: Revolutionize Your Web Experience

Free
View