Apache Kafka Architecture

How Kafka works inside: topics, partitions and offsets, brokers and replication (leaders, ISR, min.insync.replicas), KRaft controllers, consumer groups and share groups (queues), retention, compaction and tiered storage, and how to size and operate a cluster.

On this page

Simple definition#

Apache Kafka is a distributed event streaming platform. At its core it is a replicated, partitioned, append-only log: producers append records, the cluster stores them durably for a configured time, and any number of consumers read them independently. Each consumer can re-read old records whenever it needs to.

The problem it solves#

Point-to-point integration between N systems creates up to N×(N−1) connections, each with its own format, retry logic and failure modes. Traditional message queues delete a message once it is consumed, so a new consumer cannot replay history. Kafka gives every producer and consumer one durable, ordered, replayable stream per topic. It scales to millions of events per second.

Core concepts#

Concept What it is
Record (event) Key, value, headers and timestamp. The key decides the partition and therefore the ordering.
Topic A named stream of records, for example payments.authorised.v1.
Partition An ordered, append-only log. A topic is split into partitions for parallelism. Ordering is guaranteed only within a partition.
Offset The position of a record in a partition. Consumers track how far they have read with offsets.
Broker A server that stores partitions and serves reads and writes.
Replica / leader / follower Each partition has N replicas on different brokers. One is the leader, which handles reads and writes, and the others follow.
ISR The in-sync replicas: followers that are caught up with the leader.
Controller (KRaft) Manages cluster metadata (topics, partitions, leaders) using a Raft quorum.
Consumer group Consumers that share the work. Each partition is assigned to exactly one member.
Share group Queue-style consumption. Many consumers can read from the same partition, and each record is acknowledged on its own.
Apache Kafka 4.x cluster architecture · drawn with Archify · zoom, guided views, search and SVG/PNG export. © 2026 Samir Kumar Sahoo, all rights reserved. Open full screen ↗

Partitions, keys and ordering#

The key decides the partition; ordering holds per partition
flowchart LR
    P["Producer"] -->|"key = account-42"| H{"hash(key) mod 6"}
    H --> P0["partition 0"]
    H --> P3["partition 3<br/>all account-42 events, in order"]
    H --> P5["partition 5"]
  • Records with the same key always go to the same partition, so they stay in order. For example, every event for account-42 is processed in sequence.
  • Hot keys create hot partitions. A single huge merchant or tenant can overload one partition, so choose keys with high cardinality and even load.
  • The number of partitions caps consumer parallelism within a group. You can add partitions later, but that changes the key-to-partition mapping for new records and breaks per-key ordering across the change. Plan for it up front.

Replication and durability#

Each partition is replicated (replication.factor=3 is the production norm) across brokers placed in different racks or availability zones.

Write path with acks=all and min.insync.replicas=2
sequenceDiagram
    participant Pr as Producer
    participant L as Leader (broker 1)
    participant F1 as Follower (broker 2)
    participant F2 as Follower (broker 3)
    Pr->>L: produce(batch), acks=all
    L->>L: append to local log
    F1->>L: fetch
    F2->>L: fetch
    L-->>Pr: ack once ISR has the batch (ISR size at least 2)
Setting Meaning Typical production value
replication.factor Copies of each partition 3
min.insync.replicas Minimum ISR size for a write with acks=all to succeed 2
acks (producer) 0 none, 1 leader only, all full ISR all (the default since Kafka 3.0)
unclean.leader.election.enable Allow an out-of-sync replica to become leader, which can lose data false

With RF=3 and min.insync.replicas=2, the cluster survives one broker failure without losing acknowledged writes and without stopping writes. If two replicas are lost, writes fail rather than risk data loss. That is the right trade-off for financial data.

KRaft: Kafka without ZooKeeper#

Older Kafka versions stored metadata in Apache ZooKeeper. KRaft (Kafka Raft) moves metadata into Kafka itself: a small quorum of controller nodes keeps a replicated metadata log.

  • Kafka 4.0 (March 2025) removed ZooKeeper entirely. Clusters must run in KRaft mode, and you migrate from ZooKeeper while on 3.x.
  • It is simpler to operate (one system), fails over faster and supports far more partitions per cluster.
  • Run 3 controllers (or 5 for very large clusters). Use dedicated controller nodes in production; combined broker-and-controller mode is for small or dev clusters.
  • Dynamic controller quorums (KIP-853) let you add or remove controllers without downtime.

Reading: consumer groups and share groups#

Consumer groups are the classic model. Partitions are split among the group members, each member reads its partitions in order, and the group commits offsets to the internal __consumer_offsets topic. That gives ordering and scale. But one slow record blocks its partition, and parallelism is capped at the partition count.

  • The next-generation rebalance protocol (KIP-848), GA in Kafka 4.0, moves partition assignment to the broker-side group coordinator. Members join and leave incrementally, without stop-the-world rebalances. Enable it with group.protocol=consumer.

Share groups (Queues for Kafka, KIP-932) reached GA in Kafka 4.2 (2026). They provide queue semantics on top of topics:

  • Many consumers can process records from the same partition concurrently, so parallelism is not limited by the partition count.
  • Each record is acquired with a time-limited lock and individually acknowledged (ACCEPT, RELEASE to retry or REJECT). The broker counts delivery attempts, which enables poison-message handling.
  • Kafka 4.2 adds a RENEW acknowledgement to extend a lock for slow processing, plus share-partition lag metrics.
  • Use them for work queues (jobs, notifications, tasks) where per-key ordering does not matter. Keep consumer groups for ordered stream processing.
Consumer group Share group
Unit of assignment Partition Record (acquired batch)
Ordering Per partition Not guaranteed
Max parallelism Number of partitions Number of consumers
Ack model Commit offset (cumulative) Per-record accept / release / reject
Best for Event streams, CDC, stateful processing Job queues, fan-out work

Retention, compaction and tiered storage#

  • Time or size retention (retention.ms, retention.bytes) deletes old segments, not individual records.
  • Log compaction (cleanup.policy=compact) keeps at least the latest record per key. It suits changelog and “current state” topics such as customer profiles or Kafka Streams state. A record with a null value (a tombstone) deletes the key.
  • Tiered storage (KIP-405, production-ready since Kafka 3.9) moves older segments to object storage (S3, GCS, Azure Blob) while keeping recent data on local disk. That makes long retention (months or years) affordable and speeds up broker recovery and rebalancing.

Kafka Connect and the ecosystem#

  • Kafka Connect runs source and sink connectors: Debezium for CDC from databases, JDBC, S3 and Iceberg sinks, Elasticsearch and more. Integration then becomes configuration rather than code.
  • Schema Registry (Confluent, Apicurio, AWS Glue) stores Avro, Protobuf and JSON Schema versions and enforces compatibility. See Schemas and event contracts.
  • Stream processing: Kafka Streams (a library) and Apache Flink (a cluster). See Stream processing.
  • Diskless and object-storage-native Kafka is a growing trend. Some services (for example WarpStream, Confluent Freight and AutoMQ) write directly to object storage to cut cross-AZ replication cost, in exchange for higher latency. KIP-1150 proposes “diskless topics” for Apache Kafka itself.

Sizing and operations#

Question Rule of thumb
How many partitions? Max of (target throughput ÷ per-partition producer throughput, target throughput ÷ per-partition consumer throughput). Leave headroom; partitions are cheap in KRaft, but not free.
How many brokers? Enough for throughput, storage and failure tolerance. Spread across 3 AZs.
Retention Driven by replay needs and compliance. Use tiered storage for long retention.
Monitoring Under-replicated partitions, offline partitions, ISR shrinks, request latency, consumer lag, disk usage, controller health.
Security TLS everywhere, SASL (SCRAM or OAUTHBEARER) or mTLS authentication, ACLs per principal and topic, encryption at rest, audit logs.
Multi-region MirrorMaker 2, Cluster Linking or stretched clusters. Each trades RPO, latency and complexity differently.

Common mistakes#

Quick check#

What does Kafka guarantee about ordering? Order is guaranteed only within a partition. Records with the same key go to the same partition, so they are ordered per key.

What do RF=3, min.insync.replicas=2 and acks=all give you? Acknowledged writes survive the loss of one broker, and writes keep working with one broker down. With two replicas down, writes fail rather than lose data.

What changed in Kafka 4.0? ZooKeeper was removed, so KRaft is the only mode. The new consumer rebalance protocol (KIP-848) also became GA.

When would you use a share group instead of a consumer group? For queue-style work where per-key ordering does not matter and you want more consumers than partitions, with per-record acknowledgement and redelivery.

What is log compaction for? It keeps the latest record per key, so a topic can represent current state, such as a changelog. Tombstones delete keys.

Why use tiered storage? It keeps long retention cheap by moving old segments to object storage, and makes broker recovery faster.

Summary#

  • Kafka is a partitioned, replicated, append-only log. The key determines the partition and the ordering.
  • Durability comes from RF=3, min.insync.replicas=2, acks=all and no unclean election.
  • Kafka 4.x is KRaft-only and has a broker-side rebalance protocol. Share groups (GA in 4.2) add queues.
  • Use compaction for state, tiered storage for long retention, and Connect and Schema Registry for integration.

References#