Kafka Architecture: Topic, Partition, Offset, Consumer Group
Oct 2026 · 6 min read
Topic, partition, offset, and consumer group are demonstrated directly through Kafka's built-in CLI on a local broker, without a project. Readers see for themselves how keys determine partitions, how offsets are stored per group, and how partitions are distributed among group members.
Full code: https://github.com/Sch39/java-spring-boot
Problem
Before writing KafkaTemplate or @KafkaListener, the terms topic, partition, offset, and consumer group are often just memorized from diagrams. As a result, the behavior that shows up in Spring code feels random: why one consumer receives all messages, why two consumers split the messages, and why the offset doesn't change.
Invisible partitions: without seeing the partition distribution and offsets, it's hard to explain why message ordering and distribution behave the way they do.
Offsets belong to the group: the read position is stored per consumer group, not per consumer, so different groups read the same log independently.
Outcome
Readers can:
Create a topic with several partitions and read the leader and ISR from
--describe.Prove that the key determines the partition via the end offset per partition.
Read a group's
CURRENT-OFFSET,LOG-END-OFFSET, andLAG, and see partitions being distributed as group members are added.
Implementation
Setup
Kafka is installed locally, official guide: https://kafka.apache.org/quickstart. The commands below are run from the installation folder and assume the broker is already running on localhost:9092 (KRaft, single node).
cd ~/kafka_2.13-4.3.1The topic and group names are made unique (k0101-orders, k0101-notif, k0101-pair) so they don't collide with other data.
Command Blocks
Block 1: Creating a topic with 3 partitions
bin/kafka-topics.sh --bootstrap-server localhost:9092 --create --topic k0101-orders --partitions 3 --replication-factor 1Created topic k0101-orders.Goal: Create a topic with 3 partitions as the basis for all the following experiments.
Block 2: Reading the leader and ISR of each partition
bin/kafka-topics.sh --bootstrap-server localhost:9092 --describe --topic k0101-ordersTopic: k0101-orders TopicId: rgBGtlr9Rd6EZxybYBRvVw PartitionCount: 3 ReplicationFactor: 1 Configs: min.insync.replicas=1,segment.bytes=1073741824
Topic: k0101-orders Partition: 0 Leader: 1 Replicas: 1 Isr: 1 Elr: LastKnownElr:
Topic: k0101-orders Partition: 1 Leader: 1 Replicas: 1 Isr: 1 Elr: LastKnownElr:
Topic: k0101-orders Partition: 2 Leader: 1 Replicas: 1 Isr: 1 Elr: LastKnownElr: Goal: Show that each partition has its own leader, here all broker 1 because there is only one node.
Block 3: Sending messages with keys
printf 'user-1:order-1\nuser-2:order-2\nuser-1:order-3\nuser-3:order-4\nuser-2:order-5\nuser-1:order-6\n' \
| bin/kafka-console-producer.sh --bootstrap-server localhost:9092 --topic k0101-orders \
--property parse.key=true --property key.separator=:Warning: --property is deprecated and will be removed in a future version. Use --reader-property instead.Goal: Send 6 messages from 3 keys (user-1, user-2, user-3) in key:value format.
Block 4: Seeing the end offset of each partition
bin/kafka-get-offsets.sh --bootstrap-server localhost:9092 --topic k0101-ordersk0101-orders:0:0
k0101-orders:1:0
k0101-orders:2:6Goal: Prove which partition the messages landed on, namely only partition 2 contains the 6 messages.
Block 5: Reading messages as a consumer group
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic k0101-orders --group k0101-notif \
--from-beginning --max-messages 6 --timeout-ms 15000 \
--property print.partition=true --property print.offset=true --property print.key=trueOption --property is deprecated and will be removed in a future version. Use --formatter-property instead.
The consumer rebalance protocol (KIP-848) is production-ready! Set group.protocol=consumer to try it out. See https://kafka.apache.org/documentation/#consumer_rebalance_protocol
Partition:2 Offset:0 user-1 order-1
Partition:2 Offset:1 user-2 order-2
Partition:2 Offset:2 user-1 order-3
Partition:2 Offset:3 user-3 order-4
Partition:2 Offset:4 user-2 order-5
Partition:2 Offset:5 user-1 order-6
Processed a total of 6 messagesGoal: Show the partition and offset of each message, and that the order within a single partition matches the send order.
Block 6: Offsets are stored per group
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group k0101-notif
Consumer group 'k0101-notif' has no active members.
GROUP TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID HOST CLIENT-ID
k0101-notif k0101-orders 0 0 0 0 - - -
k0101-notif k0101-orders 1 0 0 0 - - -
k0101-notif k0101-orders 2 6 6 0 - - -Goal: Prove the group's offset is still stored on the broker even after the consumer has stopped.
Block 7: Lag appears when there are new messages
printf 'user-1:order-7\nuser-2:order-8\nuser-3:order-9\n' \
| bin/kafka-console-producer.sh --bootstrap-server localhost:9092 --topic k0101-orders \
--property parse.key=true --property key.separator=:
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group k0101-notifWarning: --property is deprecated and will be removed in a future version. Use --reader-property instead.
Consumer group 'k0101-notif' has no active members.
GROUP TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID HOST CLIENT-ID
k0101-notif k0101-orders 0 0 0 0 - - -
k0101-notif k0101-orders 1 0 0 0 - - -
k0101-notif k0101-orders 2 6 9 3 - - -Goal: Show lag as the difference between LOG-END-OFFSET and CURRENT-OFFSET, here 9 - 6 = 3.
Block 8: One member holds all partitions
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic k0101-orders --group k0101-pair \
--from-beginning --property print.partition=true --property print.key=true &
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group k0101-pair --members --verbose
GROUP CONSUMER-ID HOST CLIENT-ID #PARTITIONS CURRENT-EPOCH CURRENT-ASSIGNMENT TARGET-EPOCH TARGET-ASSIGNMENT
k0101-pair console-consumer-64c6d41e-9298-40d4-bf86-5cfce7dfb26d /127.0.0.1 console-consumer 3 - k0101-orders:0,1,2 - - Goal: Prove that one group member receives all three partitions.
Block 9: A second member joins, partitions are distributed
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic k0101-orders --group k0101-pair \
--from-beginning --property print.partition=true --property print.key=true &
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group k0101-pair --members --verbose
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group k0101-pair --state
GROUP CONSUMER-ID HOST CLIENT-ID #PARTITIONS CURRENT-EPOCH CURRENT-ASSIGNMENT TARGET-EPOCH TARGET-ASSIGNMENT
k0101-pair console-consumer-f0c9c252-33bb-4adc-a9d0-267d48b4d6f8 /127.0.0.1 console-consumer 1 - k0101-orders:2 - -
k0101-pair console-consumer-64c6d41e-9298-40d4-bf86-5cfce7dfb26d /127.0.0.1 console-consumer 2 - k0101-orders:0,1 - -
GROUP COORDINATOR (ID) ASSIGNMENT-STRATEGY STATE #MEMBERS
k0101-pair localhost:9092 (1) range Stable 2Goal: Prove the partitions are distributed to two members (one holds partition 2, one holds partitions 0 and 1) without any partition being held by two members.
Why It Works
Partition and key: When a key is present and the partition isn't specified, the producer selects the partition using the murmur2 hash of the key, so the same key always goes to the same partition. Kafka only guarantees order within a single partition, not across partitions.
Offset and group: An offset is a single number per partition, namely the position of the next message to be read. Because it's stored per consumer group on the broker, a new group can read again from the beginning and other groups are unaffected. A consumer may rewind to an older offset to reprocess data.
Consumer group: Within a group, each partition is held by exactly one member at a time. That's why the second member takes some partitions from the first member (Block 9), and a group with the range strategy distributes them in order.
Edge Cases
A small sample of keys piles up in one partition: In this experiment, 3 different keys all landed in partition 2 (Block 4), so partitions 0 and 1 are empty. The hash distributes evenly only across a large number of keys, not across 3 keys.
More members than partitions: Because a partition may only be held by one member per group, the 4th member on a 3-partition topic gets no share and sits idle.
Group with no members: A group whose consumers have stopped still keeps its offsets (Block 6), so the next consumer continues from that position, not from the beginning.
Trade-offs
Strategy | Pro | Con |
|---|---|---|
Few partitions (1-3) | Global order is easier to maintain, low overhead | Consumer parallelism is limited by the number of partitions |
Many partitions | Many consumers can process in parallel | More files and metadata on the broker, heavier rebalance |
No key | Load is spread evenly across partitions | No per-entity ordering guarantee |
Best practice: Choose the key from an entity whose order matters (for example userId or orderId), and set the number of partitions from the consumer parallelism target, because the number of partitions is hard to reduce once the topic is in use.
Gotchas I Experienced
Six messages from three different keys (
user-1,user-2,user-3) all went into partition 2, whilekafka-get-offsets.shshowedk0101-orders:0:0,k0101-orders:1:0, andk0101-orders:2:6.Kafka 4.3.1 prints the warning
--property is deprecated and will be removed in a future versionon the console producer (replace with--reader-property) and console consumer (replace with--formatter-property).kafka-consumer-groups.sh --deletefor a group that still has active members fails withGroupNotEmptyException: The group is not empty.
Production Checklist
Is the key chosen from an entity whose order matters? Use
orderIdoruserId, not a random value.Is the number of partitions enough for the planned number of consumers? Calculate from the parallelism target, because partitions are hard to reduce.
Is lag per group monitored? Use
kafka-consumer-groups.sh --describeor metrics, and alert on lag that keeps rising.
References
Apache Kafka, Design (Consumer Position): https://github.com/apache/kafka/blob/trunk/docs/design/design.md
Apache Kafka, Operations (Managing consumer groups): https://github.com/apache/kafka/blob/trunk/docs/operations/basic-kafka-operations.md
Apache Kafka, Introduction (Main Concepts and Terminology): https://github.com/apache/kafka/blob/trunk/docs/getting-started/introduction.md
Apache Kafka, Quickstart: https://kafka.apache.org/quickstart