skip to content

Topics, Partitions and Log Storage

Kafka's data model: topics split into partitioned append-only logs, how keys route records, and how retention or compaction reclaims space. The most-asked Kafka area, because every design question lands on partitioning and ordering.

part ofApache Kafkaoverview, primer and where to startread it →
on this pageshow

explore

questions

page 1 of 2

When you send a Kafka record with a non-null key, how does the producer decide which partition it goes to, and why does that matter?

level: juniorimportance: must knowfreq 78%

answer

  1. murmur2(key) % numPartitions
  2. deterministic same-key-same-partition
  3. ordering only within a partition
  4. key skew = partition skew
  5. not Object.hashCode

basics

~10 s

The producer hashes the key and takes hash % numberOfPartitions. The same key always lands in the same partition, so all records for that key stay ordered together on one partition.

solid answer

~40 s

With a non-null key the default partitioner computes a hash of the serialized key bytes and maps it to a partition with hash % numPartitions. This is deterministic: the same key always routes to the same partition (as long as the partition count is unchanged), which is the mechanism that guarantees per-key ordering, because Kafka only orders records within a single partition. Kafka uses the murmur2 hash over the key bytes (not Java's Object.hashCode), so routing is consistent across producer instances and languages. This matters when you need related events — say all events for one user or order id — to be processed in order; you key by that id so they share a partition. The trade-off is that key skew (a few hot keys) creates partition skew and uneven consumer load.

go deeper

for a junior

Know: non-null key => hash(key) % partitions => same key always same partition => keeps that key's records ordered.

for a middle

Add the murmur2 detail, the modulo-on-partition-count caveat, and that ordering is only within a partition.

for a senior

Discuss key skew, repartitioning consequences, and the link between keying, ordering and compaction.

for a principal

Frame keying as the lever for ordering vs. parallelism trade-offs and capacity planning (partition count chosen up front).

**The problem.** A Kafka topic is split into N **partitions** — append-only logs. Kafka guarantees ordering only *within* a partition, never across partitions. So if you need related records processed in order, they must land on the same partition. The **partition key** is how you control that. **The mechanism.** When you call `producer.send(record)` and the record has a non-null key, the producer serializes the key to bytes, then the **partitioner** maps those bytes to a partition number. The default algorithm is `Utils.murmur2(serializedKey)` then `toPositive(hash) % numPartitions`. murmur2 is a fast non-cryptographic hash; Kafka uses it (not `Object.hashCode()`) precisely so that the same key bytes hash identically regardless of which producer, JVM, or client language emits them. **Determinism.** Given the same key bytes and the same partition count, the result is always the same partition. That is what delivers **per-key ordering**: every record with key `user-42` goes to, say, partition 3, and within partition 3 they are appended in send order. **Why it matters.** Ordering, co-location (compaction keeps the latest value per key in one place), and even consumer parallelism all hinge on keying. You key by the entity whose events must stay ordered (order id, account id, device id). **Edge cases.** - **Changing partition count breaks the mapping.** `hash % N` changes if N changes, so old keys may route to new partitions — historical ordering for a key is not preserved across a repartition. This is why teams over-provision partitions up front. - **Key skew → partition skew.** If 90% of traffic has the same key, one partition (and one consumer) gets hammered. - **null key** is a different path entirely (sticky partitioning), covered separately.

  • What happens to existing keys' routing if you add partitions to a topic?
    The modulo divisor changes, so keys re-map: a key that was on partition 3 may now hash to a different partition. Past records stay where they were, so per-key ordering is not preserved across the partition-count change.
  • Why does Kafka use murmur2 instead of the key's Java hashCode?
    hashCode is JVM/implementation-specific and not portable across languages or even JVM versions; murmur2 over the raw serialized bytes gives a stable, language-agnostic mapping so producers in any client compute the same partition.

saying these in an interview costs you the question

  • Saying Kafka guarantees global ordering across all partitions
  • Claiming the same key can land on different partitions run-to-run (it can't, given fixed partition count)
  • Thinking it uses Java Object.hashCode()
  • Believing adding partitions preserves existing key routing

context

open as a page

What does cleanup.policy=compact do to a Kafka topic, and how is it different from the default delete policy?

level: juniorimportance: must knowfreq 70%

basics

~10 s

Compaction keeps at least the latest value for each message key, deleting older values for that key. The default delete policy instead drops whole segments once they exceed retention.ms or retention.bytes, regardless of key.

open as a page

What is an offset in Kafka, and why are offsets meaningful only within a single partition?

level: juniorimportance: must knowfreq 78%

basics

~20 s

An offset is a number that marks a record's position inside one partition. It starts at 0 and increases by 1 for each new record. Offsets are per-partition, so offset 5 in partition 0 and offset 5 in partition 1 are unrelated records.

open as a page

What ordering guarantee does Kafka provide, and what does it NOT guarantee?

level: juniorimportance: must knowfreq 80%

basics

~10 s

Kafka guarantees order only within a single partition: messages are read in the same order they were written. It does NOT guarantee any order across different partitions of a topic.

open as a page

What does `kafka-topics.sh --alter --partitions` allow you to do, and what is the key directionality constraint?

level: juniorimportance: must knowfreq 70%

basics

~10 s

It can only INCREASE the partition count of an existing topic, never decrease it. To shrink, you must create a new topic with fewer partitions and republish the data; Kafka has no in-place shrink.

open as a page

How does the partition count of a Kafka topic relate to consumer parallelism, and what is the maximum number of consumers in a single consumer group that can actively consume from it?

level: juniorimportance: must knowfreq 80%

basics

~20 s

Partition count is the parallelism ceiling for one consumer group. Each partition is assigned to at most one consumer in the group, so if a topic has 6 partitions, at most 6 consumers do useful work; extra consumers sit idle.

open as a page

What is a Kafka record batch, and why does Kafka store messages in batches rather than one record at a time?

level: juniorimportance: must knowfreq 55%

basics

~20 s

A record batch is a group of messages stored together as one unit on disk. Kafka batches records to reduce per-message overhead, improve compression, and write/read more efficiently — fewer, larger I/O operations instead of many tiny ones.

open as a page

What does cleanup.policy=delete mean for a Kafka topic, and how does Kafka decide which data to remove?

level: juniorimportance: must knowfreq 72%

basics

~10 s

cleanup.policy=delete means old log data is discarded once it exceeds a time or size limit. Kafka deletes whole log segments older than the retention time, or to keep the partition under the size cap.

open as a page

How do you create a Kafka topic, and what do the partitions and replication.factor settings mean?

level: juniorimportance: must knowfreq 80%

basics

~10 s

Use the kafka-topics.sh CLI or AdminClient. partitions sets how many parallel log slices the topic has; replication.factor sets how many brokers keep a copy of each partition for fault tolerance.

open as a page

What is a Kafka topic, and what is a partition?

level: juniorimportance: must knowfreq 90%

basics

~20 s

A topic is a named stream of records (like a category or feed name). Each topic is split into one or more partitions. A partition is an ordered, append-only log of records that Kafka stores and serves.

open as a page

What happens to partition selection when a record's key is null, and how has that behavior changed across Kafka versions (sticky partitioning)?

level: middleimportance: must knowfreq 62%

basics

~20 s

With a null key there's no hash to route on, so the producer spreads records across partitions. Modern Kafka uses sticky partitioning: it fills one partition's batch, then switches to another, instead of strict round-robin per record.

open as a page

What is a tombstone in a compacted Kafka topic, and how does delete.retention.ms govern its lifecycle?

level: middleimportance: must knowfreq 60%

basics

~20 s

A tombstone is a record with a null value that marks a key for deletion. After compaction, the tombstone removes prior values for that key, and the tombstone itself is retained for at least delete.retention.ms before being purged, so lagging consumers can still observe the delete.

open as a page

Distinguish a consumer's committed offset from its current position. How do poll, position, and commit interact?

level: middleimportance: must knowfreq 70%

basics

~20 s

The current position is the offset of the next record the consumer will fetch — it lives in memory and moves forward as you poll. The committed offset is the position durably saved to Kafka, used to resume after a restart or rebalance. They can differ.

open as a page

How does message keying preserve per-key ordering, and what is the exact mechanism?

level: middleimportance: must knowfreq 75%

basics

~20 s

Records with the same key are hashed to the same partition by the producer's default partitioner. Since one partition is strictly ordered, all records for a given key are processed in the order they were produced.

open as a page

Explain CreateTime vs LogAppendTime in Kafka. How does message.timestamp.type affect what is stored in a record batch?

level: middleimportance: must knowfreq 45%

basics

~10 s

CreateTime is the timestamp the producer set when the message was created. LogAppendTime is when the broker appended it. The topic config message.timestamp.type (CreateTime or LogAppendTime) decides which one Kafka keeps.

open as a page

Compare time-based and size-based retention in Kafka. Which configs control each, and what happens when both are set?

level: middleimportance: must knowfreq 64%

basics

~20 s

Time retention (log.retention.ms/.minutes/.hours, default 7 days) removes data older than a duration. Size retention (retention.bytes, default -1 = unlimited) caps bytes per partition. When both are set, a segment is removed if either limit is exceeded.

open as a page

Explain per-topic config overrides versus broker defaults: how does precedence work for something like retention.ms?

level: middleimportance: must knowfreq 65%

basics

~10 s

Brokers have cluster-wide defaults (e.g. log.retention.ms). A topic can override the equivalent topic-level config (retention.ms). If a topic-level override exists it wins; otherwise the broker default applies.

open as a page

Why is the partition — not the topic — the fundamental unit of parallelism and replication in Kafka?

level: middleimportance: must knowfreq 78%

basics

~20 s

Because Kafka stores, replicates, and serves data per partition. Each partition lives on a broker, has its own replicas, and can be consumed by exactly one consumer in a group at a time — so partitions, not topics, drive parallelism.

open as a page

What is the high watermark, how does it relate to LEO, and why can a consumer not read up to the LEO?

level: seniorimportance: must knowfreq 60%

basics

~20 s

The high watermark is the offset up to which records are replicated to all in-sync replicas, so they are 'committed' and safe. Consumers can only read records below the high watermark, never up to the LEO, because un-replicated records could be lost on leader failure.

open as a page

How can max.in.flight.requests.per.connection cause records to be reordered within a partition, and how does idempotence fix it?

level: seniorimportance: must knowfreq 65%

basics

~10 s

With multiple in-flight batches and retries, a retried earlier batch can land after a later one, reordering within a partition. Enabling the idempotent producer preserves order even with up to 5 in-flight requests.

open as a page

Why does increasing the partition count of a keyed topic break ordering and key-locality guarantees, and how do you avoid that disruption?

level: seniorimportance: must knowfreq 65%

basics

~20 s

The default partitioner routes a key via hash(key) % numPartitions. Changing numPartitions changes the modulo result, so a key that always landed in partition 2 may now land elsewhere — splitting that key's records across partitions and breaking per-key ordering.

open as a page

How do the producerId, producerEpoch, and baseSequence fields in a record batch enable idempotent (exactly-once-into-the-log) production, and how does the broker use them to detect duplicates?

level: seniorimportance: must knowfreq 40%

basics

~20 s

Each batch header carries a producerId (PID), producerEpoch, and baseSequence number. The broker tracks the last sequence it accepted per (PID, partition). If a retried batch repeats sequences, the broker drops it as a duplicate, so retries don't create duplicate records.

open as a page

How does a producer decide which partition a record goes to, and how does that interact with ordering guarantees?

level: seniorimportance: must knowfreq 72%

basics

~20 s

If a record has a key, the default partitioner hashes the key (murmur2 mod partition count) so the same key always maps to the same partition, preserving per-key order. Keyless records are spread across partitions (sticky/round-robin), so their relative order across partitions isn't guaranteed.

open as a page

How can a producer bypass the partitioner and write to a specific partition explicitly, and what are the consequences of doing so?

level: middleimportance: should knowfreq 38%

basics

~20 s

Use a ProducerRecord constructor that takes a partition number, e.g. new ProducerRecord(topic, partition, key, value). Then the partitioner is skipped entirely and the record goes to exactly that partition. You become responsible for balancing and for valid partition numbers.

open as a page

Define base offset, log-start-offset, and log-end-offset (LEO) for a Kafka partition. How do they move over time?

level: middleimportance: should knowfreq 55%

basics

~20 s

Log-start-offset is the offset of the earliest record still retained. LEO (log-end-offset) is the offset that will be assigned to the next record — one past the last written record. Base offset is the first offset of a particular log segment file.

open as a page

In the RecordBatch v2 format, how are record offsets stored, and how does the broker derive each record's absolute offset?

level: middleimportance: should knowfreq 35%

basics

~10 s

The batch header stores one absolute baseOffset. Each record inside stores only a small offsetDelta. A record's absolute offset = baseOffset + offsetDelta. This avoids repeating the full 8-byte offset on every record.

open as a page

How do you set and inspect retention on a single topic without changing the whole broker, and how do broker-level vs topic-level configs interact?

level: middleimportance: should knowfreq 48%

basics

~10 s

Use kafka-configs.sh (or kafka-topics.sh --config) to set per-topic overrides like retention.ms and retention.bytes. A topic-level config always overrides the broker default (log.retention.*). Inspect with --describe.

open as a page

What do auto.create.topics.enable and num.partitions control, and why is auto-creation often disabled in production?

level: middleimportance: should knowfreq 60%

basics

~20 s

auto.create.topics.enable lets the broker auto-create a missing topic when a client produces to or fetches from it. num.partitions and default.replication.factor decide the new topic's shape. It's disabled in prod to prevent typo'd or unplanned topics.

open as a page

What is a TopicPartition, and how is a single record uniquely addressed in Kafka?

level: middleimportance: should knowfreq 60%

basics

~10 s

A TopicPartition is the identity of one specific partition: the pair (topic name, partition number). A single record is uniquely addressed by adding its offset, giving the triple (topic, partition, offset).

open as a page

When would you implement a custom Kafka Partitioner, and what does the interface require you to do?

level: seniorimportance: should knowfreq 45%

basics

~20 s

Implement org.apache.kafka.clients.producer.Partitioner when the default key-hash routing isn't what you need — e.g. routing hot keys specially or grouping by a field of the value. You override partition(...) to return the partition number and wire it with partitioner.class.

open as a page

showing 1–30 of 47