Overview

What brokers, topics, partitions and offsets are, and when Kafka is the right choice instead of a message queue or Redis.

Updated CompTech Team

Apache Kafka is a log. Producers append records to the end. Consumers read at their own pace and remember where they got to. The record stays after it is read, until retention says otherwise. That one property is why a bank can replay Tuesday's payments, and why a second application can read the same payments without asking the first to send them again.

This page names the parts. The rest of the series builds them on Streams for Apache Kafka, on the OpenShift cluster we use in the lab.

The four words

Word Meaning
Broker A Kafka process. It stores partitions and serves produce and fetch requests. A cluster is several brokers
Topic A named stream of records, such as payments. Producers and consumers agree on the name
Partition An ordered, append-only slice of a topic. Order is guaranteed inside one partition, and nowhere else
Offset The position of a record inside its partition. A consumer commits the offset it has finished

A topic with six partitions can be read by up to six consumers in one group at the same time. A seventh consumer in that group sits idle. A different group has its own offsets, so a fraud scorer and a warehouse loader can both read payments from the start or from the tip.

The key on a record decides the partition. Records for the same account go to the same partition, which is how you keep one account's payments in order. The topic design lab is that decision, written down.

Kafka, a queue, or Redis

Kafka A message queue Redis
After a consumer reads The record stays until retention The record is gone once acknowledged The key stays until its TTL or an eviction
Read it again Yes, from an earlier offset No Only if you stored it for that
Order Per key, if the key maps to one partition Per queue, roughly, under one consumer Not what it is for
Many independent readers Natural. Each group has its own offset A fan-out you have to design Pub/sub, without a durable offset
Fits An event log, CDC, anything you may replay A work queue: each job done by one worker A cache, a lock, the latest value

Use a queue when each message is a task that one worker should take and nobody should see again. Use Redis when you need the current balance in memory and you already have the durable copy somewhere else. Use Kafka when more than one system needs the same facts, or when you will want yesterday's facts again.

A cache in front of Kafka is normal. A Kafka topic used as a cache, storing only the latest value, is a compacted topic, and it is still a log. Topic design covers compaction.

What the labs build

The cluster is Streams for Apache Kafka in KRaft mode. There is no ZooKeeper. Brokers, topics, users and connectors are custom resources, so they go through the same GitOps flow as any other manifest.

Lab What you leave with
Kafka on OpenShift A KRaft cluster, node pools and a topic
Producers and consumers Spring Boot, Quarkus, .NET, and the HTTP Bridge
Delivery guarantees A payment that is not posted twice
Schema registry Avro, Protobuf or JSON Schema, evolved on purpose
Kafka Connect and CDC Databases in and out of the log
Stream processing Filters, joins and windows in Kafka Streams and Flink
Kafka for AI A fresh RAG index, and a fraud score on the way past
Security, monitoring TLS, ACLs, lag alerts
Scaling and upgrades, DC/DR More brokers, a rolling upgrade, a second site

A record, end to end

A producer in the payments service appends {id, account, amount} to payments, with acks=all, so the leader and the in-sync replicas have it before the call returns. A consumer group posting reads the partition, posts the payment in the core system if that id is new, and commits the offset. A second group warehouse reads the same partitions an hour later. Neither group can see the other's position.

That is the whole system. The rest is what happens when the producer retries, the consumer crashes after posting and before committing, or the schema gains a field. Those are the next labs, not extra products.

Order, and what "read again" costs

Take two payments for account a1 and one for a2. If the producer keys by account, both of a1's records land on one partition, in the order they were sent. a2 may land on another partition. A consumer that reads partition 0 and a consumer that reads partition 1 are not ordered against each other. Any rule of the form "the debit before the credit" has to be the same key, or it has to be enforced in the ledger, not assumed from the topic.

Reading again is moving the group back, or starting a new group. It is free in the sense that the bytes are still there, and it is not free in the sense that every consumer of the new group will re-apply the record. The ledger in the delivery lab treats a repeated payment id as success and does nothing. A consumer that appends a row without that check will double the balance the moment someone replays the topic. Retention is the other limit: once the record is older than the topic's retention, there is nothing to replay. Seven days on payments means a warehouse loader that was down for eight days cannot catch up from Kafka. It has to come from the ledger.

Those two constraints, the key and the retention, are decided in topic design and then left alone. Changing the partition count later moves which partition a key hashes to, so the "one partition per account" property is only true for records produced after the change.

What you will log in to

In the lab the cluster is a Kafka custom resource named lab, in the kafka project, on Streams for Apache Kafka. Clients inside the cluster use lab-kafka-bootstrap.kafka.svc:9092 until the security lab moves them to the TLS listener on 9093. Topics, users and connectors are custom resources in the same project, reviewed in Git with the GitOps flow.

oc get kafka,kafkanodepool,kafkatopic -n kafka

An empty list means you are at the start of Kafka on OpenShift, which is the right next page. A cluster that is Ready and a topic called payments means you can skip to producers. Do not create topics with a shell command on a broker if the topic operator is running. The operator's view and the cluster's view will disagree, and the next reconcile will surprise you.

A consumer group is the unit of scaling and the unit of who has read a record.

  • Two programs that must each see every payment are two groups.
  • Two pods of one program are one group. Kafka divides the partitions between the pods.
  • Offsets are committed per group. Resetting posting does not move fraud.
  • A group with no committed offset follows auto.offset.reset. A group that has committed does not.
  • You cannot have more active consumers in a group than there are partitions. Extra pods wait.

That split is why a fraud scorer and a ledger poster can share payments without sharing a failure. The Kafka for AI lab uses it on purpose.

What's next

  1. Install the cluster in Kafka on OpenShift.
  2. Decide partitions, keys and retention in topic design.
  3. Write a producer that does not double-post in delivery guarantees.