Core concepts: broker, topic, partition, offset
Junior Always asked 8 min
On this page
The question
What is Kafka, and what is it used for?
30-second answer
Kafka is a distributed event streaming platform: a cluster of brokers that stores streams of records in topics, as replicated, append-only logs. Producers append records; consumers read them by offset, at their own pace, and the records stay until retention deletes them. It is used for event-driven microservices, activity tracking, log and metrics pipelines, change data capture and stream processing, wherever high throughput, ordering per key and replay matter.
Understand it
The parts of a Kafka cluster
Tap the partsSelect a part to see what it does.
Tap a part. The top row holds your applications. Below, the left column is how data is laid out, and the right column is the servers that store it.
View as text
Eight boxes in two columns. On top, the two kinds of client: a producer that writes records and a consumer group that reads them. Below, on the left, how data is laid out: a topic, split into partitions, where each record has an offset. On the right, the servers: brokers, the replicas of each partition, and the controller.
Producer: A producer is your application sending records: a key, a value, headers and a timestamp.
Consumer group: Consumers that share a group ID split the partitions of a topic: each partition goes to one consumer of the group.
Topic: A topic is a named stream of records, such as orders.
Broker: A broker is one Kafka server.
Partition: A partition is an ordered, append-only log on disk.
Replica: Each partition is copied to several brokers, the replication factor, often 3.
Offset: An offset is the position of a record in its partition: 0, 1, 2 and on, never reused.
Controller: The controller manages cluster metadata: which brokers are alive and which replica leads each partition.
Eight boxes in two columns. On top, the two kinds of client: a producer that writes records and a consumer group that reads them. Below, on the left, how data is laid out: a topic, split into partitions, where each record has an offset. On the right, the servers: brokers, the replicas of each partition, and the controller.
Producer: A producer is your application sending records: a key, a value, headers and a timestamp.
Consumer group: Consumers that share a group ID split the partitions of a topic: each partition goes to one consumer of the group.
Topic: A topic is a named stream of records, such as orders.
Broker: A broker is one Kafka server.
Partition: A partition is an ordered, append-only log on disk.
Replica: Each partition is copied to several brokers, the replication factor, often 3.
Offset: An offset is the position of a record in its partition: 0, 1, 2 and on, never reused.
Controller: The controller manages cluster metadata: which brokers are alive and which replica leads each partition.
Takeaway: Producers append records to partitions of a topic. Brokers store the partitions, each copied to several of them, and the controller tracks who leads. Consumer groups read the partitions by offset.
Records appended to partitions, read by offset
Step by stepThe producer sends a record with key A. The key picks partition 0, where Kafka appends it at offset 0. A record is never changed once written.
View as text
A producer above two partitions of the topic orders, drawn as rows of records with their offsets underneath. Below partition 0, a marker for each consumer group points at the next offset it will read.
The producer sends a record with key A. The key picks partition 0, where Kafka appends it at offset 0. A record is never changed once written.
More records arrive. Each partition numbers its own records, so offset 1 exists in both. Kafka keeps order inside a partition, not across the topic.
The billing group reads partition 0 from offset 0 to 2, then commits offset 3: the next record it wants. Reading removes nothing from the log.
The analytics group reads the same partition at its own pace, and is at offset 2. Each group keeps its own offsets, so another reader costs the producer nothing.
Two more records arrive. Past the retention time, 7 days by default, Kafka deletes old records, read or not. Partition 0 now starts at offset 2; offsets are never reused.
A new group, audit, starts at the earliest offset still kept and reads everything again. A queue cannot do that replay: it deletes a message once a consumer acknowledges it.
A producer above two partitions of the topic orders, drawn as rows of records with their offsets underneath. Below partition 0, a marker for each consumer group points at the next offset it will read.
The producer sends a record with key A. The key picks partition 0, where Kafka appends it at offset 0. A record is never changed once written.
More records arrive. Each partition numbers its own records, so offset 1 exists in both. Kafka keeps order inside a partition, not across the topic.
The billing group reads partition 0 from offset 0 to 2, then commits offset 3: the next record it wants. Reading removes nothing from the log.
The analytics group reads the same partition at its own pace, and is at offset 2. Each group keeps its own offsets, so another reader costs the producer nothing.
Two more records arrive. Past the retention time, 7 days by default, Kafka deletes old records, read or not. Partition 0 now starts at offset 2; offsets are never reused.
A new group, audit, starts at the earliest offset still kept and reads everything again. A queue cannot do that replay: it deletes a message once a consumer acknowledges it.
Takeaway: A partition is a log: records are appended at the next offset and stay until retention deletes them. Each consumer group only moves its own offset, so many groups can read, and reread, the same records.
In code
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("key.serializer", StringSerializer.class.getName());
props.put("value.serializer", StringSerializer.class.getName());
try (var producer = new KafkaProducer<String, String>(props)) {
var record = new ProducerRecord<>("orders", "customer-42", "{\"orderId\":7,\"total\":30}");
RecordMetadata meta = producer.send(record).get(); // wait for the broker's acknowledgement
System.out.printf("partition %d, offset %d%n", meta.partition(), meta.offset());
}Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("group.id", "billing"); // consumers with this ID split the partitions
props.put("auto.offset.reset", "earliest"); // a new group starts at the oldest record kept
props.put("key.deserializer", StringDeserializer.class.getName());
props.put("value.deserializer", StringDeserializer.class.getName());
try (var consumer = new KafkaConsumer<String, String>(props)) {
consumer.subscribe(List.of("orders"));
while (running) {
for (ConsumerRecord<String, String> r : consumer.poll(Duration.ofMillis(500))) {
bill(r.key(), r.value()); // r.partition() and r.offset() say where it was
}
consumer.commitSync(); // stores the next offset to read, per partition
}
}kafka-topics.sh --bootstrap-server localhost:9092 --create --topic orders \
--partitions 3 --replication-factor 3 --config retention.ms=604800000 # 7 days
# Latest value per key only, such as a customer's current address
kafka-topics.sh --bootstrap-server localhost:9092 --create --topic customer-addresses \
--partitions 3 --replication-factor 3 --config cleanup.policy=compact
kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group billing
# TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG
# orders 0 1042 1050 8// Three instances of the billing service, each with its own group: every order is billed three times
Wrong props.put("group.id", "billing-" + UUID.randomUUID());
// One group ID for the service: the three instances split the partitions, each order is billed once
Right props.put("group.id", "billing");Solve it
A new consumer group reads nothing of last week’s orders
The topic orders holds a week of records. You deploy a new reporting service with the group ID reporting to build
totals from that week, but it only sees orders placed after it started. Nothing is broken in the topic. Explain what
happened and get the report built.
- Find where the group started. A group with no committed offsets falls back to
auto.offset.reset, which defaults tolatest: it starts at the end of each partition, after the existing records. - Check the data is still there. Reading never deletes, so last week’s records stay until retention removes them.
With the default 7 days, the oldest ones are about to go, so act now or raise
retention.ms. - Move the group back. Stop the service, then reset the group to the earliest offsets, or set the reset policy to
earliestbefore its first start. The setting only applies when the group has no committed offset. - Mind the side effects. A replay reprocesses every record, so the report must tolerate duplicates: compute totals from scratch, or upsert by order ID.
- Check.
kafka-consumer-groups.sh --describeshows the lag dropping from the full week to zero, and the totals match a count from the database.
kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group reporting \
--topic orders --reset-offsets --to-earliest --execute
# For a group that has never committed, this Spring Boot property is enough:
# spring.kafka.consumer.auto-offset-reset=earliestHow to answer
- A topic is split into partitions; a partition is an ordered log on disk.
- Records are kept for a retention time, not deleted when read, so many groups can read them.
- Partitions spread load across brokers and are replicated, usually three times, for fault tolerance.
- Consumers in one group split the partitions, which is how reading scales.
Common mistakes
- Calling Kafka a message queue that deletes a message once it is consumed.
- Claiming Kafka keeps order across a whole topic. It keeps order only within a partition.
Follow-up questions
What is the difference between a topic and a partition?
A topic is a named, logical stream, such as orders. A partition is one ordered log that holds part of the topic's records, stored on a broker and replicated to others. A record's key decides its partition, so one key always lands in the same one. Partitions are the unit of parallelism, ordering and replication: more partitions allow more consumers in a group, but order is only guaranteed within one partition.
- Partition count caps the consumers of one group that can work at once.
- Partitions can be added later but never removed, and adding them moves keys.
- Each partition has one leader replica serving its reads and writes.
What is an offset, and who keeps track of it?
An offset is the position of a record inside its partition: 0, 1, 2 and on, assigned on append and never reused. The broker assigns it; the consumer tracks how far it has read. A consumer group commits, per partition, the offset of the next record to read, and Kafka stores it in the internal topic __consumer_offsets. After a restart or a rebalance, consumption resumes from the committed offset.
- Offsets are per partition, so offset 5 exists in every partition.
- The committed offset is the next one to read, not the last one processed.
- With no committed offset, auto.offset.reset decides: latest by default, or earliest.
See it in the infographic: Records appended to partitions, read by offset
How long does Kafka keep records, and what is log compaction?
By default Kafka deletes records after a retention time, retention.ms, 7 days by default, or once a partition passes retention.bytes, unlimited by default, whether or not anyone read them. It deletes whole segment files, so records can outlive the limit a little. Log compaction, cleanup.policy=compact, keeps the latest record per key instead, which suits a changelog of current state. A record with a null value, a tombstone, deletes its key.
- Retention is per topic and independent of consumers.
- Compaction keeps at least the last value of every key, forever.
- Topics can combine both policies, compact and delete.
See it in the infographic: Records appended to partitions, read by offset
Why is Kafka called a log and not a queue?
A queue hands each message to one consumer and deletes it once acknowledged. Kafka appends records to a log and keeps them for the retention time; consumers only move their own offset. So many consumer groups read the same records independently, a group can rewind and replay, and order holds within a partition. The price: Kafka acknowledges a position, not a single message, so it cannot skip one failed record and redeliver it alone.
- Reading does not delete; retention does.
- Load sharing comes from partitions, not from handing out messages one by one.
- Replay rebuilds read models or feeds a new service from history.
See it in the infographic: Records appended to partitions, read by offset
What happens to a topic when a broker goes down?
Each partition has replicas on several brokers, typically three. One is the leader that serves reads and writes; the followers copy it. When the leader's broker dies, the controller elects a new leader from the followers that were in sync, and clients refresh their metadata and carry on against it. With a replication factor of 3, the topic survives one broker down with no data loss, given the producer waited for all in-sync replicas.
- Replication factor 1 means one broker down makes that partition unavailable.
- Leaders are spread across brokers so load stays balanced.
- Producers and consumers only talk to the leader, by default.
Why does Kafka scale so well on ordinary disks?
It only appends to files and reads them in order, which disks and the operating system's page cache handle very fast. It batches and compresses records, and it can send file data straight from the page cache to the network socket with zero-copy. Partitions spread that work across brokers, and consumers pull at their own pace, so a slow consumer never slows the producer.
Kafka 4.0 removed ZooKeeper. What replaced it?
KRaft: a few nodes act as controllers and agree on cluster metadata, such as topics, brokers and partition leaders, through Kafka's own Raft protocol. Metadata lives in an internal log, so there is one system to run and secure, and recovery after a controller fails is faster.
Check yourself
Show answers
- Each partition can hold its own record at offset 5: Correct. Offsets are numbered per partition, from 0.
- Offset 5 names one record across the whole topic: Incorrect. That would need one counter for the topic; each partition has its own.
- It is the sixth record the topic ever received: Incorrect. Only within one partition; records of other partitions are counted separately.
Show answers
- Every record still within retention: Correct. Reading never deletes; retention does, so the records are still there.
- Nothing, because billing consumed them: Incorrect. That is queue behavior. Groups track their own offsets.
- Only records billing has not committed yet: Incorrect. One group's offsets do not limit another group.
Show answers
- Each consumer reads one partition: Correct. A group assigns each partition to exactly one of its consumers.
- Each consumer receives every record: Incorrect. That happens with three different group IDs.
- Records are dealt to the consumers one by one: Incorrect. Kafka assigns whole partitions, not single records.