The Core Anatomy of Apache Kafka
Understand how topics, partitions, offsets, brokers, and consumer groups form the foundational building blocks of Kafka's distributed streaming architecture.
To design reliable systems with Apache Kafka, you must understand how its core primitives fit together. Kafka does not treat messages as ephemeral items in a queue; it organizes data as distributed, partitioned, append-only commit logs.
In this lesson, you will explore the foundational components of Apache Kafka: topics, partitions, offsets, records, brokers, and consumer groups.
01The Physical Hierarchy: Topics and Partitions
At the highest level, Kafka organizes streams of records into Topics. A topic is a logical category or feed name to which records are published, such as orders, payments, or user-signups.
However, a single topic cannot scale on a single machine if millions of events arrive per second. To achieve horizontal scalability, Kafka subdivides each topic into one or more Partitions.
What is a Partition?
A partition is an ordered, immutable sequence of records continuously appended to a structured commit log file on disk.
- Append-Only: New records are strictly written to the end of the partition log. Existing records cannot be updated or deleted in place.
- Strict Ordering per Partition: Kafka guarantees that records within a single partition are stored and consumed in the exact order they were received.
- No Global Ordering: Kafka does not guarantee global ordering across multiple partitions of the same topic. If total ordering across all events is required, a topic must have exactly one partition, which limits concurrency to a single consumer thread.
02The Offset: Unique Message Identity
Every record appended to a partition receives a sequential integer identifier called an Offset.
Partition 0: [Offset 0] -> [Offset 1] -> [Offset 2] -> [Offset 3] -> [Offset 4 (Latest)]Key offset rules to remember:
- Immutable: The offset of a record never changes once written.
- Partition-Scoped: Offsets are unique only within their specific partition. A record at offset 5 in Partition 0 is completely distinct from a record at offset 5 in Partition 1.
- Log End Offset (LEO): The offset of the next record to be written to the partition. In the diagram above, if offsets 0 through 6 exist, the LEO is 7.
- High Watermark (HW): The offset up to which all in-sync replicas have acknowledged the write. Consumers can only read messages up to the high watermark to prevent reading uncommitted data that might be lost during a leader failure.
03The Anatomical Structure of a Kafka Record
When a producer sends data to Kafka, it constructs a ProducerRecord. On disk and across the network, a record consists of five elements:
// Conceptual layout of an Apache Kafka record
public record KafkaRecord<K, V>(
String topic,
Integer partition, // Optional: target partition index
K key, // Optional: determines partition routing
V value, // The payload (JSON, Avro, Protobuf, String)
Long timestamp, // Creation time or LogAppendTime
RecordHeaders headers // Key-value metadata pairs (e.g. traceId, correlationId)
) {}The Role of the Message Key
The record key serves two critical purposes:
- Partition Assignment: If a key is provided (for example,
customerIdororderId), Kafka hashes the key using the murmur2 algorithm to route all records with the same key to the exact same partition:partition = hash(key) % partitionCount. This ensures that all events for a specific customer are processed in strict chronological order. - Log Compaction: For compacted topics, Kafka uses the key to retain only the latest state for that key, discarding older records.
If no key is provided (key = null), Kafka uses its default sticky partitioner to batch records efficiently across available partitions in a round-robin manner.
04Consumer Groups and Independent Read Pointers
In traditional message queues, when multiple workers listen to the same queue, each message is consumed by only one worker, and the broker deletes the message.
Kafka uses the concept of Consumer Groups to combine the benefits of queueing and publish-subscribe models:
- Point-to-Point Queueing: Multiple instances of a single consumer group (such as
order-fulfillment-group) share the partitions of a topic. Each partition is assigned to exactly one consumer instance within the group, allowing parallel processing without race conditions. - Publish-Subscribe Fan-Out: Multiple independent consumer groups can read from the exact same topic simultaneously. The
order-fulfillment-groupand thefraud-analytics-groupeach maintain their own independent offset pointers.
Because reading a record does not delete it, a consumer group can reset its offset to 0 and re-read six months of historical events without affecting any other service.
05Kafka Brokers and the Cluster
A Kafka Broker is a server process running the Kafka software. A collection of one or more brokers forms a Kafka Cluster.
- Stateless Clients, Stateful Storage: Brokers are responsible for receiving records from producers, appending them to physical disk logs, replicating them to other brokers, and serving fetch requests to consumers.
- Bootstrap Servers: Kafka clients only need the network address of one or two active brokers (called
bootstrap-servers) to connect. Upon initial handshake, the client queries the cluster metadata to discover the leader broker for every partition and connects directly to the relevant broker.
06In the interview
- Can you have more consumer instances in a group than there are partitions in a topic? No. If a topic has 6 partitions and a consumer group has 8 instances, 2 consumer instances will sit completely idle. The maximum parallel processing capacity of a consumer group is bounded by the number of partitions.
- What happens if a record has no key? When the key is null, the modern Kafka sticky partitioner fills a batch of records for one partition before switching to the next, optimizing network throughput and broker compression ratios.
07Check your understanding
Quiz · 4 questions
0 of 4 answered
1. What happens if a consumer group with 6 instances subscribes to a topic that has 4 partitions?
2. What is the primary architectural purpose of a message key in a Kafka producer record?
3. Where does Kafka store the tracking state of which records a consumer group has successfully processed?
4. What is the relationship between the High Watermark (HW) and the Log End Offset (LEO) of a partition?