BBackend CourseLeroy Labs
0/0
Search the course

Kafka, event sourcing & CQRS

Kafka's log model (topics, partitions, offsets, consumer groups, retention), when to pick it over RabbitMQ, event sourcing (state as a fold over events), and CQRS read models.

● expert

Kafka in one picture

Topic "journal-events"   (retained for 7 days, or forever with compaction)
  Partition 0: [e0][e1][e2][e3][e4] ...            <- append-only, ordered within a partition
  Partition 1: [e0][e1][e2] ...
  Partition 2: [e0][e1][e2][e3] ...
                         ^ consumer group "reporting" offset=3
                   ^ consumer group "audit" offset=1   (independent, can replay)
  • Producer writes with a key (e.g. companyId). The same key always goes to the same partition, which gives per-key ordering.
  • Consumer groups: each partition is read by one consumer in a group, so parallelism is bounded by the number of partitions.
  • Offsets are a consumer’s bookmark. Reset them to replay history.
  • Messages are retained after they’re read, unlike a RabbitMQ queue.

Kafka vs RabbitMQ

RabbitMQ Kafka
Model Message broker (queues; delete on ack) Distributed commit log (retain, replay)
Strength Routing, per-message acks, task queues, low latency Throughput (millions/s), replay, many independent consumers, stream processing
Ordering Per queue Per partition (key)
Typical use Send email, generate export, RPC Event streaming, CDC (Debezium), analytics pipelines, event sourcing

Rule of thumb: “do this job” means RabbitMQ or SQS; “this happened, and many systems care, possibly later” means Kafka.

Event sourcing

Instead of storing current state, store every event, and derive state by replaying them:

Account 42 events:
  AccountOpened(42)
  MoneyDeposited(42, 100)
  MoneyWithdrawn(42, 30)
  MoneyDeposited(42, 50)
=> balance = fold(events) = 120

Pros:

  • A perfect audit trail.
  • Time travel (“what was the state on 31 Dec?”).
  • New projections can be built from history.
  • It’s a natural fit for money, which is why a double-entry ledger is event sourcing.

Cons:

  • Schema evolution of events (upcasting).
  • Replaying long streams needs snapshots.
  • Eventual consistency of read models.
  • A steep learning curve, and a poor fit for CRUD-ish domains.

SperoTek’s immutable posted journals, reversals and append-only audit log are event-sourcing ideas inside a relational model. That’s often the sweet spot.

CQRS (Command Query Responsibility Segregation)

Separate the write model (commands, invariants, normalized) from the read model (queries, denormalized, optimised per screen):

Command -> Write model (JPA aggregates, Postgres) --events--> Projector --> Read model (denormalized table / Elastic / Redis)
Query   -----------------------------------------------------------------> Read model

You already do light CQRS:

  • Hostel has Summary vs Detail DTOs and native projection queries.
  • IR3D has AdminDashboardQueryRepository.
  • SperoTek’s reporting is driven by JournalPosted.

Full CQRS adds separate stores and asynchronous projections, at the cost of eventual consistency.

Axon Framework and EventStoreDB (event sourcing), Debezium (CDC into Kafka), Kafka Streams / Flink (stream processing), Schema Registry (Avro/Protobuf event contracts).

Check yourself

1. Kafka guarantees ordering…
2. A new analytics service needs last year’s events. Best fit?
3. In event sourcing, current state is…