Core Concepts in Depth: Partitions, Offsets, Segments, Retention and Replication

Apache Kafka 16 min min read Updated: Nov 17, 2026 Intermediate
Core Concepts in Depth: Partitions, Offsets, Segments, Retention and Replication
Intermediate Topic 4 of 4

Partitions: The Unit of Parallelism

  • A topic is split into partitions, and partitions are spread over brokers
  • Each partition is an ordered log. Order is guaranteed inside a partition only
  • More partitions allow more consumers in a group to work in parallel (one partition is read by at most one consumer of the same group)
  • More partitions also mean more open files, more replication work and longer recovery. Do not create thousands without a reason
text Topic "orders" (3 partitions, replication factor 3, brokers b1 b2 b3) Leader Follower Follower orders-0: b1 b2 b3 orders-1: b2 b3 b1 orders-2: b3 b1 b2 (leaders are spread to balance the load)

Offsets

Every record in a partition gets a sequential number, its offset. Offsets are not reused and are unique only within one partition. A consumer remembers which offset it has processed. The group's progress is stored in an internal Kafka topic called __consumer_offsets.

How Data Is Stored on Disk

text /var/lib/kafka/data/orders-0/ 00000000000000000000.log <- segment: the actual records, from offset 0 00000000000000000000.index <- offset -> byte position (sparse index) 00000000000000000000.timeindex <- timestamp -> offset 00000000000005242880.log <- the next segment starts at offset 5242880 ... leader-epoch-checkpoint

A partition is a folder of segment files. Kafka writes to the newest (active) segment and starts a new one when it reaches log.segment.bytes (default 1 GB) or log.roll.hours. Old segments are deleted as a whole file, which is very cheap.

Retention: How Long Data Is Kept

SettingMeaningDefault
retention.ms (topic) / log.retention.hours (broker)Delete segments older than this7 days
retention.bytesDelete oldest segments when a partition exceeds this sizeunlimited
segment.bytesSize of one segment file1 GB
cleanup.policydelete (time or size based) or compactdelete
Retention does not depend on reading: Data is deleted by age or size, not because a consumer read it. That is why a new consumer can read old data and why you can replay.

Log Compaction

With cleanup.policy=compact, Kafka keeps at least the latest value for every key and removes older values of the same key. It behaves like a table that holds the current state.

text Before compaction: (k1,v1) (k2,v1) (k1,v2) (k3,v1) (k1,v3) (k2,null) After compaction: (k3,v1) (k1,v3) (k2,null -> a "tombstone", removed after delete.retention.ms)

Uses: current customer profiles, product catalogs, the changelog of a stream processing state store, __consumer_offsets.

Replication

  • Each partition has one leader and zero or more followers. Producers and consumers talk to the leader
  • Followers continuously fetch new data from the leader
  • The ISR (in-sync replica set) is the leader plus followers that are caught up
  • If the leader fails, one member of the ISR becomes the new leader (no data loss for acknowledged writes when configured correctly)
SettingMeaningTypical production value
replication.factorCopies of each partition3
min.insync.replicasMinimum ISR size needed to accept writes with acks=all2
unclean.leader.election.enableAllow an out-of-sync replica to become leader (can lose data)false
text replication.factor = 3, min.insync.replicas = 2, acks = all - One broker down: still 2 in sync -> writes continue - Two brokers down: only 1 in sync -> writes are REJECTED (safe) rather than risk losing data

Why Is Kafka So Fast?

  • Sequential disk writes: appending to a log is much faster than random writes
  • OS page cache: Kafka relies on the operating system cache instead of its own big cache, so restarts stay warm
  • Zero-copy: data goes from disk or cache to the network without copying into application memory (sendfile)
  • Batching and compression: producers and brokers handle batches of records
  • Partitioning: work is spread over many brokers and disks
Rule of thumb for partitions: Start with the number of consumers you expect at peak, multiplied by a growth factor (for example 2). Common values for busy topics are 6, 12 or 30. Measure throughput per partition for your workload.

Quick Quiz

  1. Can two consumers of the same group read the same partition at the same time? (No)
  2. Can two consumers of different groups read the same partition? (Yes)
  3. Does reading a record delete it? (No, retention does)
  4. What happens to ordering if you add partitions? (Keys may map to new partitions, so order for a key can change over time)

Conclusion

Kafka stores each partition as an append-only log of segment files, keeps data by time or size (or latest per key with compaction), and replicates partitions to followers. Sequential IO, page cache and zero-copy make it fast.

Quick Summary: partition = ordered log; segments; retention by age/size; compaction = latest per key; leader+ISR.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators