Apache Kafka Tutorial 0/111 lessons ~6 min read Lesson 79

    CQRS

    What is CQRS?

    Course progress0%
    Focus
    9 guided sections
    Practice signal
    Examples included
    Career prep
    Foundation builder

    Introduction

    What is CQRS? CQRS separates command writes from query reads.

    Understanding the topic

    What happens — CQRS:

    • Define RPO/RTO and data sovereignty.
    • Replicate selected topics cross-cluster.
    • Plan failover and conflict handling.
    • Capacity-plan for peak + replay.
    TermDescription
    MirrorMaker 2Cluster-to-cluster replication.
    RPO/RTORecovery point/time objectives.
    Event sourcingState from event replay.
    CQRSSeparate write and read models.

    Visual explanation

    Pipeline view:

    text
    Region A Kafka
    ↓ replicate selected topics
    Region B Kafka
    local consumers + DR plan
    Trade-off: latency, cost, consistency

    Step-by-step explanation

    1. Design — Clusters, topics, mapping.
    2. Replicate — MM2 or cluster link.
    3. Failover — Test DR runbooks.
    4. Govern — Schema and ownership.

    Informative example

    Example:

    bash
    kafka-topics --bootstrap-server localhost:9092 --create --topic cqrs --partitions 6 --replication-factor 3
    kafka-console-producer --bootstrap-server localhost:9092 --topic cqrs
    kafka-console-consumer --bootstrap-server localhost:9092 --topic cqrs --from-beginning
    kafka-consumer-groups --bootstrap-server localhost:9092 --describe --group cqrs-service

    Execution workflow

    1CQRS workflow
    1 / 4

    Design

    Clusters, topics, mapping.

    Best practices

    • Define RPO/RTO before choosing replication design.
    • Replicate only topics with clear ownership and purpose.
    • Document failover and failback procedures.
    • Model event sourcing around immutable business facts.

    Common mistakes

    • Bi-directional replication without loop/conflict strategy.
    • Event sourcing every table instead of meaningful domain events.
    • Ignoring replay cost during capacity planning.

    Summary

    CQRS — Make trade-offs explicit: RPO/RTO, ordering, sovereignty, replay cost, schema governance, and operational ownership.

    Ready to mark this lesson complete?Track your journey across the entire course.