On this page
Apache Kafka Internals
Storage internals, replication protocol, and the operational playbook for running Kafka for real — segments and indexes, the producer/consumer wire paths, ISR replication, compaction, exactly-once semantics, and the lag/rebalance debugging patterns you reach for during an incident. See kafka-field-guide.md for the concept-first tour this guide sits underneath.
Track how many of the knowledge checks below you've cleared as you go:
Storage: Log Segments
Kafka stores each partition as an append-only log on disk, divided into segment files.
graph TD
subgraph "Partition 0 on disk"
SEG0["00000000000000000000.log<br/>messages offset 0-999"]
IDX0["00000000000000000000.index<br/>offset --> file position"]
SEG1["00000000000000001000.log<br/>messages offset 1000-1999"]
IDX1["00000000000000001000.index<br/>sparse index"]
ACTIVE["00000000000000002000.log<br/>ACTIVE segment<br/>new writes go here"]
end
PROD["Producer<br/>append to active"] --> ACTIVE
CONS["Consumer<br/>seek to offset, read sequentially"] --> SEG0 & SEG1 & ACTIVE
Segment rolling: When active segment reaches log.segment.bytes (default 1GB) or log.roll.ms (default 7 days), it's closed and a new one starts.
The index file: Sparse index mapping offsets to byte positions. Consumer seeks to an offset → binary search in index → seek to file position → read forward. O(log n) seek, then O(1) sequential read.
Step through what a seek to an arbitrary offset actually does:
.index file doesn't map every offset, just a sample. Binary search finds the closest indexed entry at or before 1450.
.log file.
The index only maps a sample of offsets, not every one. Why is a seek still O(log n) instead of O(n)?
Producer Write Path
sequenceDiagram
participant PROD as Producer
participant LEADER as Partition Leader (broker-1)
participant ISR1 as ISR Replica (broker-2)
participant ISR2 as ISR Replica (broker-3)
PROD->>LEADER: ProduceRequest (acks=all, messages)
LEADER->>LEADER: Append to local log segment
LEADER->>ISR1: Replicate (async)
LEADER->>ISR2: Replicate (async)
ISR1-->>LEADER: Fetch offset acknowledged
ISR2-->>LEADER: Fetch offset acknowledged
Note over LEADER: All ISR replicas caught up
LEADER-->>PROD: ProduceResponse (offset=1234)
Note over PROD: Write committed (acks=all)
acks settings:
In the sequence diagram above, the leader replicates to ISR1 and ISR2 before sending ProduceResponse. With acks=all, could the producer still get its ack before both followers confirm?
Consumer Groups and Offset Management
graph TD
TOPIC["Topic: orders<br/>6 partitions"] --> CG["Consumer Group: payments"]
subgraph CG["Consumer Group: payments (3 consumers)"]
C1["Consumer-1<br/>assigned: P0, P1"]
C2["Consumer-2<br/>assigned: P2, P3"]
C3["Consumer-3<br/>assigned: P4, P5"]
end
C1 & C2 & C3 -->|"commit offsets"| OFFSET_TOPIC["__consumer_offsets topic<br/>stores: group+topic+partition --> offset"]
Offset commit strategies:
// Auto commit (default, at-least-once risk)
props.put("enable.auto.commit", "true");
props.put("auto.commit.interval.ms", "5000");
// Manual commit after processing (at-least-once, safer)
consumer.poll(Duration.ofMillis(100));
// ... process records ...
consumer.commitSync(); // block until broker confirms
// Exactly-once: commit offset in same DB transaction as business logic
// (transactional outbox pattern)
auto.commit.interval.ms, default 5000ms) regardless of whether the records returned by the last poll() have actually finished processing. Default behavior, at-least-once risk.
commitSync() is called explicitly after the processing loop finishes, tying the offset move to completed work instead of a timer. Still at-least-once, but safer than auto commit.
Looking at the code above: what's the difference between "auto commit" and "manual commit after processing," in terms of when the commit actually happens relative to processing the records?
auto.commit.interval.ms) independent of whether the record has actually been processed yet. Manual commit calls commitSync() explicitly, after the processing loop — so the offset only moves once the work it represents is actually done.Offset reset policy:
auto.offset.reset=earliest — if there's no committed offset yet for this group/partition, start reading from the very beginning of the log.
auto.offset.reset=latest (default) — if there's no committed offset yet, start from the newest message onward. Everything already in the log is skipped.
A consumer group has been running for months with a healthy committed offset. Does changing auto.offset.reset from latest to earliest change where it resumes on its next restart?
Replication — ISR Deep Dive
graph TD
LEADER2["Partition Leader<br/>HW = 1005 (High Watermark)"] --> ISR_A["ISR: broker-2<br/>LEO = 1007 (Log End Offset)"]
LEADER2 --> ISR_B["ISR: broker-3<br/>LEO = 1005"]
LEADER2 --> OUT_ISR["OUT of ISR: broker-4<br/>LEO = 950 (too far behind)<br/>replica.lag.time.max.ms exceeded"]
CONS2["Consumer<br/>can only read up to HW=1005<br/>not uncommitted messages 1006-1007"]
High Watermark (HW): The offset up to which ALL ISR replicas have the data. Consumers can only read up to HW. Messages above HW are uncommitted — might be lost if leader crashes.
Leo (Log End Offset): Latest offset written to the log, may be ahead of HW.
ISR shrink/expand:
replica.lag.time.max.ms (default 30s) — too far behind, like broker-4 above (LEO 950 vs the leader's HW of 1005). It rejoins automatically once fully caught up. Alert if ISR size drops below replication.factor — that's lost redundancy.
In the diagram above, why can consumers read up to offset 1005 (the HW) but not the messages the leader already wrote at 1006-1007?
Try It Yourself: Live ISR Simulator
Same idea as the diagram above, but live: one partition, 1 leader + 2 followers. Produce records, stall a follower to simulate it falling behind, and watch exactly when it gets dropped from the ISR — and what acks=all is actually blocked on at each step, versus acks=1. Non-stalled followers catch up to the leader instantly here (a simplification of real async replication lag) — the part worth watching closely is what happens to a stalled one.
Cluster Metadata and the Controller: KRaft (KIP-500)
Everything above this point — ISR membership, which broker leads which partition, who's allowed to do what — is a decision someone has to track cluster-wide, not per-partition. That someone is the controller: exactly one broker (pre-KRaft) or a small elected leader (KRaft) responsible for topic/partition metadata, leader assignment, broker liveness, and ACLs. If it goes away, the remaining nodes elect a new one and the cluster keeps serving data in the meantime. The interesting question this section answers: where does that metadata actually live, and what does "elect a new one" actually mean under the hood — because as of Kafka 3.x (production-ready) and 4.0 (default, ZooKeeper support removed entirely), the answer changed completely.
Why ZooKeeper got removed. For most of Kafka's life, none of that metadata lived inside Kafka at all — it lived in Apache ZooKeeper, a general-purpose coordination service Kafka delegated to. That meant every Kafka deployment was actually two distributed systems stacked on top of each other: the Kafka cluster you wanted, and a ZooKeeper ensemble underneath it whose sole job was being Kafka's filing cabinet for topics, partitions, ACLs, and controller election. ZooKeeper brought its own cluster to provision and patch, its own quorum-sizing rules, its own session-timeout tuning, and its own failure modes — a lost ZK quorum could stall Kafka's entire control plane (no new leader elections, no topic changes) even while every broker was up and healthy serving reads and writes. Running Kafka well meant a team had to also run ZooKeeper well, as a second, unrelated skill set. KIP-500 removed that second system.
KRaft's approach. Instead of delegating metadata to an outside system, Kafka now stores it the same way it stores everything else it's good at storing: as a log. Every metadata change — a topic created, a partition's leader changing, an ACL granted — is appended as a record to an internal topic, __cluster_metadata, and that topic is replicated using an actual Raft implementation among a small set of nodes running the controller role. Which physical nodes play that role is a config choice, not a fixed topology — the process.roles setting on each node is broker, controller, or both:
graph TD
subgraph Small["Small cluster -- roles combined"]
S1["Node 1<br/>process.roles=broker,controller"]
S2["Node 2<br/>process.roles=broker,controller"]
S3["Node 3<br/>process.roles=broker,controller"]
S1 --> S2
S2 --> S3
S3 --> S1
end
subgraph Large["Larger cluster -- roles split"]
C1["Controller 1 -- current Raft leader<br/>process.roles=controller"]
C2["Controller 2<br/>process.roles=controller"]
C3["Controller 3<br/>process.roles=controller"]
B1["Broker 1<br/>process.roles=broker"]
B2["Broker N<br/>process.roles=broker"]
C1 --> C2
C1 --> C3
B1 -->|"fetch __cluster_metadata"| C1
B2 -->|"fetch __cluster_metadata"| C1
end
Small clusters typically combine both roles on the same handful of nodes (cheaper, fewer processes to run). Larger clusters split them — a dedicated 3- or 5-node controller quorum handling only metadata, separate from the brokers serving produce/consume traffic, so metadata load never competes with data load on the same process.
Leader election for that metadata quorum is no longer "ZooKeeper handles it as a black box" — it's genuine Raft: terms, log offsets, majority votes, the same mechanics already covered in replication.md § 9, Consensus Algorithms (including a live leader-election demo). That's the identical algorithm, not a loose analogy — it's just electing a leader for the __cluster_metadata log instead of a general-purpose replicated log, so there's no need to re-derive term numbers or vote-counting here.
What's operationally different for a cluster admin:
- One system to run and monitor, not two. No separate ZooKeeper ensemble to size, patch, upgrade, and page on — controller state is just another Kafka log, observed with the same tooling as everything else in the cluster.
- Faster controller failover. ZooKeeper-based failover was bottlenecked by ZK session timeouts before a dead controller's session even expired and a new election could start. Raft's own election timeout drives failover directly instead — no second system's timeout sitting in the critical path.
- Metadata durability rides on Kafka's own replication.
__cluster_metadatais replicated and made durable the same way any other Kafka log is, instead of depending on a separate consensus implementation (ZooKeeper's) with its own semantics and its own bugs to reason about.
Why is running a KRaft-based Kafka cluster operationally simpler than the old ZooKeeper-based one?
__cluster_metadata) replicated among controller nodes that are part of the Kafka cluster itself, so there's one system to run and monitor instead of two.Under the old architecture, ZooKeeper handled controller election as a black box. Under KRaft, did the election mechanism itself change, or did metadata just move to a new storage location with the same election logic underneath?
__cluster_metadata log -- the same algorithm covered in replication.md's Consensus Algorithms section, not a Kafka-specific black box ZooKeeper ran internally. Metadata moving into a Kafka-style log is one change; electing that log's leader with real Raft semantics is a separate, more fundamental one.Log Compaction
graph LR
subgraph Before["Before compaction (key:value log)"]
M1["offset=0: user:1 --> {name:Alice}"]
M2["offset=1: user:2 --> {name:Bob}"]
M3["offset=2: user:1 --> {name:ALICE}"]
M4["offset=3: user:3 --> {name:Charlie}"]
M5["offset=4: user:2 --> null (tombstone = delete)"]
end
subgraph After["After compaction"]
K1["offset=2: user:1 --> {name:ALICE} (latest)"]
K2["offset=3: user:3 --> {name:Charlie}"]
Note["user:2 deleted (tombstone + old value removed)"]
end
Log compaction retains the latest value per key — turns Kafka into a changelog/event store for materialized views. Used by Kafka Streams and ksqlDB.
log.cleanup.policy=compact # enable compaction
log.cleanup.policy=compact,delete # compact AND delete old segments
min.cleanable.dirty.ratio=0.5 # compact when 50% of log is dirty
Step through a compaction pass on the log shown above:
min.cleanable.dirty.ratio (0.5 above), the cleaner picks this log for a pass.
Try It Yourself: Live Log Compaction
The stepper above narrates one fixed, scripted pass. This one is live: append your own keyed records (the same key can land at many offsets — that's the whole point), tombstone a key to mark it for deletion, then run Compact and watch only the highest offset per key survive.
A topic uses cleanup.policy=compact. Does a record get removed because it's old, or for some other reason?
Exactly-Once Semantics
graph LR
PROD2["Producer<br/>enable.idempotence=true<br/>transactional.id=tx-1"] -->|"ProducerID + sequence number<br/>broker deduplicates"| BROKER["Kafka Broker<br/>dedup by ProducerID+Seq"]
BROKER -->|"transaction: atomic multi-partition write"| P1["Partition A"]
BROKER --> P2["Partition B"]
P1 & P2 -->|"consumer reads isolation.level=read_committed"| CONS3["Consumer<br/>only sees committed transactions"]
Idempotent producer: Each message tagged with ProducerID + sequence number. Broker rejects duplicates (retry after network failure = same message, not duplicate).
Transactions: Write to multiple partitions atomically. Either all committed or none visible.
A producer has enable.idempotence=true but isn't using transactions. It writes to Partition A, then Partition B, then crashes right after A's write lands but before B's does. Is that an atomic failure?
Key Metrics
# Consumer lag (most important — alert > 10000)
kafka_consumer_group_lag > 10000
# Under-replicated partitions (alert > 0)
kafka_server_replication_under_replicated_partitions > 0
# ISR shrink rate (alert if frequent)
rate(kafka_server_replication_isr_shrinks_total[5m]) > 0
# Producer request latency p99
histogram_quantile(0.99, kafka_network_request_total_time_ms_bucket{request="Produce"}) > 100
Schema Registry
Avro/Protobuf schemas are stored in the Schema Registry. Producers serialize with schema ID; consumers look up the schema to deserialize. Prevents incompatible schema changes breaking consumers.
graph LR
PROD2["Producer"] -->|"register/lookup schema"| SR["Schema Registry"]
PROD2 -->|"[magic:1B][schema_id:4B][avro_bytes]"| BROKER2["Kafka Broker"]
CONS2["Consumer"] -->|"lookup schema by ID"| SR
BROKER2 -->|"raw bytes"| CONS2
CONS2 -->|"deserialize with schema"| DATA2["Typed object"]
# Producer with schema registry
from confluent_kafka.avro import AvroProducer
producer = AvroProducer(
{'bootstrap.servers': 'kafka:9092', 'schema.registry.url': 'http://registry:8081'},
default_value_schema=avro.loads(value_schema_str)
)
producer.produce(topic='orders', value={'id': '123', 'amount': 99.99})
Schema compatibility modes:
Compatibility is set to FORWARD. A schema change adds a new required field (no default). Does it pass compatibility checking?
Kafka Transactions (Exactly-Once)
sequenceDiagram
participant APP as Application
participant BROKER as Kafka Broker
participant OFFSET_TOPIC as __consumer_offsets
APP->>BROKER: initTransactions()
APP->>BROKER: beginTransaction()
APP->>BROKER: produce(orders, message1)
APP->>BROKER: produce(analytics, message2)
APP->>OFFSET_TOPIC: sendOffsetsToTransaction(group, offsets)
APP->>BROKER: commitTransaction()
Note over BROKER: All messages + offset commit atomic
Note over BROKER: Consumers with isolation.level=read_committed<br/>only see committed messages
producer.initTransactions();
producer.beginTransaction();
try {
producer.send(new ProducerRecord<>("orders", key, value));
producer.sendOffsetsToTransaction(offsets, groupMetadata);
producer.commitTransaction();
} catch (Exception e) {
producer.abortTransaction();
}
Walk through the happy path plus the abort branch:
transactional.id — a one-time setup call before any transaction begins.
orders and analytics — all belonging to this one transaction.
isolation.level=read_committed now see all of it, or none of it.
catch block fires instead, nothing produced in this transaction ever becomes visible under read_committed — not partially, not at all.
Why does sendOffsetsToTransaction() need to exist — why not just call commitSync() on the consumer offset normally, right after commitTransaction()?
Kafka Streams
Kafka Streams is a Java library for stream processing — stateless transformations, aggregations, joins — all backed by Kafka topics.
StreamsBuilder builder = new StreamsBuilder();
// Read from topic
KStream<String, Order> orders = builder.stream("orders");
// Stateless: filter + transform
KStream<String, Order> paidOrders = orders
.filter((key, order) -> order.getStatus().equals("paid"))
.mapValues(order -> enrichOrder(order));
// Stateful: count per user (stored in RocksDB state store)
KTable<String, Long> orderCounts = orders
.groupByKey()
.count(Materialized.as("order-counts-store"));
// Write to output topic
paidOrders.to("paid-orders");
orderCounts.toStream().to("order-counts");
State stores (RocksDB) are backed by changelog topics — on restart, the state is rebuilt from the changelog without reprocessing all input.
filter, mapValues, and similar — each record is transformed independently. Nothing needs to be remembered between records, so no state store is involved.
groupByKey().count() and similar — the operation needs to remember something across records (a running count per key). That memory lives in a local RocksDB state store, itself backed by a changelog topic.
A Kafka Streams instance crashes and restarts, losing its local RocksDB files. Does it have to reprocess the original input topics from scratch to rebuild its state?
Consumer Lag Alerting
# Alert: consumer group is falling behind (lag > 10K messages)
kafka_consumer_group_lag{group="payments", topic="orders"} > 10000
# Calculate processing rate needed to catch up
# current_lag / (consume_rate - produce_rate) = time to catch up
# Alert: no consumer is running for a group (lag growing without consumption)
increase(kafka_consumer_group_lag[5m]) > 0
AND
kafka_consumer_group_members{group="payments"} == 0
# Real-time lag monitoring
kafka-consumer-groups.sh \
--bootstrap-server kafka:9092 \
--describe --group payments
# Watch lag column — should trend toward 0 for healthy consumer
# Kafka UI tools: Kafdrop, Redpanda Console, Conduktor
The alert combo `increase(lag[5m]) > 0 AND members == 0` fires. What specific failure mode does the members==0 half rule in that a plain rising-lag alert alone wouldn't distinguish?
Topic Sizing and Retention
# View topic configuration
kafka-configs.sh --bootstrap-server kafka:9092 \
--describe --entity-type topics --entity-name orders
# Override retention for a specific topic
kafka-configs.sh --bootstrap-server kafka:9092 \
--alter --entity-type topics --entity-name orders \
--add-config retention.ms=604800000 # 7 days
# retention.bytes=10737418240 # 10GB
# Estimate disk usage
# disk_per_partition = (produce_rate_bytes/s × retention_seconds) / num_partitions
# Total disk = disk_per_partition × total_partitions × replication_factor
Debugging
# Describe a topic (partitions, replicas, ISR)
kafka-topics.sh --bootstrap-server kafka:9092 --describe --topic orders
# Partition: 0 Leader: 1 Replicas: 1,2,3 Isr: 1,2,3
# If Isr != Replicas: a replica is behind → investigate
# Read messages from beginning
kafka-console-consumer.sh \
--bootstrap-server kafka:9092 \
--topic orders --from-beginning --max-messages 10
# Check broker log dirs (find large partitions)
kafka-log-dirs.sh --bootstrap-server kafka:9092 \
--broker-list 1,2,3 --topic-list orders
# Preferred replica election (rebalance leaders back after failure)
kafka-leader-election.sh --bootstrap-server kafka:9092 \
--election-type PREFERRED --all-topic-partitions
Consumer Lag Deep-Dive
Consumer lag = log-end-offset - committed-offset. It tells you how far behind a consumer group is from the head of the partition.
Reading lag correctly
# View lag per partition for a consumer group
kafka-consumer-groups.sh \
--bootstrap-server kafka:9092 \
--describe \
--group payments-processor
# Output:
# GROUP TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID
# payments-processor payments 0 45000 45100 100 consumer-1
# payments-processor payments 1 44900 45050 150 consumer-2
# payments-processor payments 2 44800 46000 1200 consumer-3 ← spike
# Total lag = sum of all partition lags = 1450
# Partition 2 has 10x the lag of others → partition imbalance
Why per-partition lag matters: a consumer group may show low average lag while one partition is 10,000 messages behind. Average lag hides the worst case. Always look at max lag per partition.
A consumer group's total/average lag looks healthy. Does that guarantee no single partition is badly behind?
Lag alert with Prometheus (Kafka Exporter)
# kafka-exporter exposes: kafka_consumergroup_lag{consumergroup, topic, partition}
- alert: KafkaConsumerLagHigh
expr: |
sum(kafka_consumergroup_lag{consumergroup="payments-processor"}) by (consumergroup, topic) > 10000
for: 5m
labels:
severity: warning
- alert: KafkaConsumerLagCritical
expr: |
max(kafka_consumergroup_lag{consumergroup="payments-processor"}) by (partition) > 50000
for: 2m
labels:
severity: critical
annotations:
summary: "Single partition lag > 50k — consumer likely dead or partition hot"
Root causes and fixes
| Cause | Lag pattern | Fix |
|---|---|---|
| Consumer too slow | Steadily growing across all partitions | Scale consumers (add instances up to partition count) |
| Hot partition | One partition 10x lag of others | Key redesign; add partitions; spot the hot key |
| Consumer died | One partition at 0 throughput | Check consumer logs; rebalance trigger |
| Rebalance storm | Lag spikes every few minutes | Increase session.timeout.ms, tune max.poll.interval.ms |
| GC pause in consumer | Sporadic lag spikes | Tune JVM GC; reduce max.poll.records |
| Message processing error | Lag at specific offset | Consumer stuck in retry loop; add DLQ |
Producer tuning for throughput vs durability
# High throughput (analytics, logs) — batch more, weaker guarantees
acks=1 # leader ACK only (not all replicas)
batch.size=65536 # 64KB batch (default 16KB)
linger.ms=10 # wait 10ms to fill batch before sending
compression.type=lz4 # compress batches (lz4 best CPU/ratio tradeoff)
buffer.memory=67108864 # 64MB producer buffer
max.in.flight.requests.per.connection=5
# High durability (payments, orders) — ensure no data loss
acks=all # all ISR replicas must ACK
retries=2147483647 # retry forever (Java MAX_INT)
max.in.flight.requests.per.connection=1 # prevent message reordering on retry
enable.idempotence=true # exactly-once on producer side
delivery.timeout.ms=120000 # 2 minutes total retry window
acks=1, bigger batches, a few ms of linger.ms, lz4 compression. For analytics/logs workloads where an occasional lost message on leader failure is an acceptable tradeoff for throughput.
acks=all, retries effectively forever, max.in.flight.requests.per.connection=1 to prevent reordering on retry, idempotence on. For payments/orders — the goal is zero data loss even if it costs latency.
Partition strategy — choosing partition count
Partition count determines max consumer parallelism.
More partitions → more parallelism + more overhead (open file handles, leader elections).
Rule of thumb:
target_throughput_MB/s ÷ throughput_per_partition_MB/s = partitions needed
Single partition throughput (approximate):
Producer: ~50-100 MB/s (disk sequential write speed)
Consumer: ~50-100 MB/s (network + processing bound in practice)
Example:
Need 500 MB/s total throughput
Each partition handles ~50 MB/s
→ 10 partitions minimum
For consumer parallelism:
max_consumers_in_group = partition_count
If you have 20 consumer instances, you need ≥20 partitions
Extra consumers beyond partition count sit idle
# Add partitions to an existing topic (can only increase, never decrease)
kafka-topics.sh \
--bootstrap-server kafka:9092 \
--alter \
--topic payments \
--partitions 20
# WARNING: adding partitions changes key→partition mapping for new messages.
# Old messages stay on old partitions. Consumers must handle reordering.
# For strict ordering by key: pre-plan partition count at topic creation.
Rebalance debugging
Consumer rebalances (triggered by member join/leave/timeout) pause ALL consumers in the group while a new partition assignment is computed.
# Check rebalance frequency
kafka-consumer-groups.sh \
--bootstrap-server kafka:9092 \
--describe --group payments-processor
# CONSUMER-ID changes on rebalance
# Common causes of excessive rebalancing:
# 1. max.poll.interval.ms too low — consumer takes longer to process than allowed
# Fix: increase max.poll.interval.ms or reduce max.poll.records
# 2. session.timeout.ms too low — consumer heartbeat misses under GC pause
# Fix: increase session.timeout.ms (but lag detection slower)
# 3. Rolling restart — each pod restart triggers two rebalances (leave + rejoin)
# Fix: use static group membership
poll() calls than max.poll.interval.ms allows — the coordinator assumes it's dead and rebalances. Fix: increase max.poll.interval.ms, or reduce max.poll.records so each batch processes faster.
session.timeout.ms. Fix: increase session.timeout.ms — the tradeoff is slower detection of a genuinely dead consumer.
group.instance.id) so a restart within session.timeout.ms skips the rebalance entirely.
# Static group membership — survive restarts without rebalance
group.instance.id=payments-consumer-0 # unique, stable ID per consumer instance
session.timeout.ms=60000 # how long before a static member is considered dead
# With static membership, restarts within session.timeout.ms skip rebalance
Try It Yourself: Live Partition Assignment (Range Assignor)
The mermaid diagram earlier in this file already showed the shape of it — 6 partitions split into contiguous ranges across consumers. This is that assignment live: 6 fixed partitions (P0–P5), starting with a 2-consumer group. Add or remove a consumer by name to trigger a rebalance and watch the Range assignor — Kafka's default — recompute the whole assignment from scratch every time.
Why Kafka Is Fast: OS-Level I/O
Kafka's throughput — hundreds of MB/s on commodity hardware — comes from four compounding OS-level properties, not from clever JVM code.
Sequential Writes
Kafka producers always append to the end of the active log segment for a partition. The OS page cache buffers these writes in memory and flushes to disk asynchronously in large sequential batches. Kafka does not call fsync() per message — it relies on replication (ISR quorum) for durability, and lets the OS flush at its own pace.
Sequential I/O benchmark on spinning disk: ~500MB/s write. Random I/O on the same disk: ~200 IOPS (~1MB/s effective). The difference is three orders of magnitude.
Zero-Copy with sendfile()
When a consumer reads a batch of messages, the naive path copies data four times:
sequenceDiagram
participant Disk
participant PageCache as Kernel Page Cache
participant UserSpace as Broker User Space
participant SocketBuf as Socket Buffer (kernel)
participant NIC
Note over Disk,NIC: Normal read() + write() — 4 copies, 2 context switches
Disk->>PageCache: 1. DMA: disk → page cache
PageCache->>UserSpace: 2. read() syscall: kernel → user space (CPU copy)
UserSpace->>SocketBuf: 3. send() syscall: user space → socket buffer (CPU copy)
SocketBuf->>NIC: 4. DMA: socket buffer → NIC
Note over Disk,NIC: sendfile() — 2 copies, 0 user-space involvement
Disk->>PageCache: 1. DMA: disk → page cache
PageCache->>NIC: 2. DMA: page cache → NIC (kernel-to-kernel, no user space)
With sendfile(), the broker never copies message bytes into user space for a passthrough consumer read. The kernel transfers directly from the page cache to the NIC buffer. This is how a single Kafka broker can saturate a 10Gbps NIC without the JVM heap doing any of the work.
Kafka enables zero-copy by design: messages are stored on disk in the exact same binary format they're sent to consumers over the network. No deserialization, no transformation — the broker is a dumb pipe.
Page Cache as Read Cache
Consumers that follow closely behind producers (real-time consumers) read messages that are still in the OS page cache — data that was never evicted to disk from the producer's write. This is a free read from RAM.
The page cache advantage over a JVM heap cache:
- OS-managed: survives broker restarts (the JVM heap is gone, the page cache survives if the data is still warm)
- Shared: multiple consumers reading the same partition share the same cached pages
- No GC pressure: page cache memory doesn't participate in JVM garbage collection
mmap for Index Files
Kafka's .index (offset → physical position) and .timeindex (timestamp → offset) files are memory-mapped with mmap(). A consumer offset lookup is a binary search over a virtual memory region — no read() syscall, no kernel/userspace copy. The OS maps the index file into the broker's virtual address space and handles page faults lazily.
Batching
Producer side: linger.ms and batch.size accumulate multiple records before a single TCP write(). Fewer syscalls, larger payloads, better compression ratio (more repetition within a batch).
Consumer side: fetch.min.bytes and fetch.max.wait.ms prevent the consumer from firing a fetch per message. The broker waits until there's enough data, then returns one large response.
Combined, batching means fewer syscalls, fewer TCP round trips, and better sendfile() utilization — one sendfile() call can transfer thousands of messages at once.
A consumer reads 10MB from a Kafka broker. How many times does the 10MB of message data cross the kernel/userspace boundary with zero-copy (sendfile) vs without?
sendfile() (zero-copy): zero times. The data moves from disk → page cache (DMA, hardware-driven), then page cache → NIC (DMA, hardware-driven). The broker's user-space process is never involved in copying the payload bytes — it only issues the syscall. Without zero-copy (normal read + write): twice. First copy: kernel page cache → broker user-space buffer (CPU-driven, on the read() syscall). Second copy: broker user-space buffer → kernel socket buffer (CPU-driven, on the write() syscall). Then a final DMA from socket buffer to NIC — that one's hardware-driven in both paths.Redpanda
Redpanda is a Kafka-API-compatible message broker rewritten in C++ using the Seastar framework. It's a drop-in replacement for Kafka clients and producers — no code changes required. No JVM, no ZooKeeper, no KRaft migration.
Thread-per-Core Architecture
Kafka's JVM broker runs a thread pool where threads share state (partition maps, request queues, memory). Threads compete for locks under load; GC pauses all threads simultaneously.
Redpanda's model: each CPU core owns a fixed subset of partitions and its own memory shard. Cores communicate via message passing (Seastar futures/fibers), never shared memory. This eliminates lock contention entirely — there's nothing to lock.
graph LR
subgraph Kafka["Kafka (JVM)"]
TP["Thread Pool<br/>(shared state,<br/>locks, GC)"]
TP --> P1a["Partition A"]
TP --> P1b["Partition B"]
TP --> P1c["Partition C"]
end
subgraph Redpanda["Redpanda (C++ / Seastar)"]
C0["Core 0<br/>(owns P-A)"]
C1["Core 1<br/>(owns P-B)"]
C2["Core 2<br/>(owns P-C)"]
end
No GC Pauses
Kafka's JVM GC (G1 or ZGC) can pause all broker threads simultaneously — even ZGC with its concurrent collection introduces occasional multi-millisecond stop-the-world pauses. These pauses show up as p99/p999 latency spikes in consumers.
Redpanda is C++ with deterministic memory management (RAII, custom allocators). There are no GC pauses. p99 latency stays in the sub-millisecond range even under load.
Built-In Raft
Kafka's Raft (KRaft, KIP-500) replaced ZooKeeper in 3.0 — a necessary migration that required running a separate controller quorum alongside the broker quorum, with careful upgrade procedures.
Redpanda uses Raft natively from its first release. Every partition group uses Raft for leader election and log replication, coordinated within the same broker processes. There's no separate metadata cluster to operate, no ZooKeeper to manage, no KRaft migration path.
Kafka vs Redpanda
| Kafka | Redpanda | |
|---|---|---|
| Language | Scala/Java (JVM) | C++ (Seastar) |
| Scheduling | Preemptive OS threads | Cooperative fibers (no context switches) |
| Memory model | JVM heap + GC | Manual/RAII, per-core shards |
| Shared state | Yes (locks) | No (message passing between cores) |
| Kafka | Redpanda | |
|---|---|---|
| p50 produce latency | 1–5ms | <1ms |
| p99 produce latency | 10–50ms (GC spikes) | 1–5ms |
| GC pause risk | Yes (even ZGC) | None |
| Tail latency predictability | Variable | Consistent |
| Kafka | Redpanda | |
|---|---|---|
| Metadata store | ZooKeeper → KRaft (3.0+) | Built-in Raft, day one |
| Upgrade complexity | ZK→KRaft migration required | Single binary, rolling upgrade |
| Ecosystem maturity | Very mature (10+ years) | Growing (2020+) |
| Kafka Streams | Native | Not available (use Flink/ksqlDB) |
| Schema Registry | Confluent / community | Built-in |
- Tail latency matters: trading, gaming, real-time bidding — p99 spikes are unacceptable
- Smaller ops footprint: no ZooKeeper, no KRaft migration, single binary deployment
- You want Kafka API compatibility without JVM tuning overhead (
-Xmx, GC flags, heap dumps) - Edge or resource-constrained environments where JVM startup and footprint are costs
Stay on Kafka when:
- You use Kafka Streams (no direct Redpanda equivalent)
- Your team has deep Kafka operational expertise — don't pay the relearning cost for marginal latency gains
- You rely on the Confluent ecosystem (ksqlDB, Kafka Connect sources/sinks, Schema Registry with Confluent features)
- You're at a scale where Confluent Cloud or MSK operational support is worth more than the latency savings
Disaster Recovery & Cross-Cluster Replication
Why Kafka Has No Traditional Backup
Kafka's log segments are already replicated across brokers by the ISR mechanism. A replication factor of 3 means each partition is stored on 3 brokers — losing one broker is handled automatically by leader re-election.
Traditional "backup" (tar the data dir) doesn't work well:
- Log segments contain offsets, which are cluster-specific
- Consumer group offsets live in
__consumer_offsets— not in the log segments - A node restored from raw files won't know the current partition leader or cluster membership
The answer to DR in Kafka is not backup — it's cross-cluster replication.
In-Cluster HA vs Cross-Cluster DR
- Single broker failure (leader re-election from ISR)
- Rolling broker restarts (upgrades)
- Disk failure (partition replicated to other brokers)
- Full cluster failure (data center outage)
- Accidental topic deletion
- Corrupted data replicated to all replicas
- Active-passive DC failover (traffic switches to DR cluster)
- Active-active geo-distribution (writes in both clusters, replicated bidirectionally)
- Accidental deletion recovery (DR cluster has data independently)
- Sub-second failover (replication lag means some messages may not be in DR cluster yet)
- Exactly-once across clusters
MirrorMaker2 Architecture
graph LR
subgraph Source["Source Cluster (DC1)"]
B1[Broker 1]
B2[Broker 2]
B3[Broker 3]
end
subgraph MM2["MirrorMaker2"]
MSC["MirrorSourceConnector<br/>mirrors topic data"]
MCC["MirrorCheckpointConnector<br/>syncs consumer offsets"]
MHC["MirrorHeartbeatConnector<br/>measures replication lag"]
end
subgraph Target["Target Cluster (DC2)"]
T1[Broker 1]
T2[Broker 2]
T3[Broker 3]
end
Source -->|topic records| MSC --> Target
Source -->|__consumer_offsets| MCC --> Target
MHC -->|heartbeat.ms latency| Target
Three connectors, each with a specific job:
MirrorSourceConnector: replicates topic records. Topics appear in target prefixed with the source alias (e.g.,source.my-topic).MirrorCheckpointConnector: translates and syncs consumer group offsets from source to target so consumers can resume from the right position after failover.MirrorHeartbeatConnector: writes periodic records to a heartbeat topic; used to measure actual replication lag.
MirrorMaker2 Configuration
# mm2.properties
clusters = source, target
source.bootstrap.servers = dc1-broker1:9092,dc1-broker2:9092
target.bootstrap.servers = dc2-broker1:9092,dc2-broker2:9092
# Enable all three connectors
source->target.enabled = true
source->target.topics = .* # all topics
source->target.groups = .* # all consumer groups
# Replication factors for internal MM2 topics
source->target.replication.factor = 3
checkpoints.topic.replication.factor = 3
heartbeats.topic.replication.factor = 3
# Start MirrorMaker2
./bin/connect-mirror-maker.sh mm2.properties
Consumer Group Offset Sync & Failover
Consumer group offsets are cluster-local — offset 1000 in source ≠ offset 1000 in target if compaction ran differently. MirrorCheckpointConnector writes translated offsets to mm2-offsets.<source>.internal in the target cluster.
On failover:
# Reset consumer group to translated offset in target cluster
./bin/kafka-consumer-groups.sh \
--bootstrap-server dc2-broker1:9092 \
--group my-consumer-group \
--reset-offsets \
--from-file translated-offsets.csv \
--execute
auto.offset.reset=earliest as a safe fallback: if no translated offset is found, the consumer starts from the earliest available record — may reprocess some messages but won't miss any.
Tiered Storage (KIP-405) as Long-Term Retention
Apache Kafka 3.6+ (and Confluent Platform) supports Tiered Storage — older log segments are offloaded to object storage (S3/GCS) while recent segments stay on broker disk. This enables:
- Months/years of retention without scaling broker disk
- Cost-effective DR: historical data in S3 survives a full cluster loss
- Re-bootstrapping a new cluster from tiered storage
Not a backup per se — but achieves similar recoverability for historical data without a separate backup pipeline.
Your source Kafka cluster goes down. MirrorMaker2 was replicating with ~30 second lag. Consumer group payments-consumer was at offset 50000 in source. What happens to it in the target cluster after failover?