Skip to content

AxonOps — AI-Native Control Plane for Open Source Data Platforms

Kafka Architecture

Apache Kafka is a distributed commit log designed for high-throughput, fault-tolerant, real-time data streaming.


Kafka's architecture consists of brokers forming a cluster, with data organized into topics and partitions. Producers write to topic partitions, and consumers read from them.

Kafka cluster topology with producers, brokers, and consumer groupsKafka cluster topology with producers, brokers, and consumer groupsKafka ClusterBroker 1(Controller)Broker 2Broker 3ProducersConsumersConsumer Group AP0 LeaderP1 FollowerP2 FollowerP0 FollowerP1 LeaderP2 FollowerP0 FollowerP1 FollowerP2 LeaderProducer 1Producer 2C1C2Controller coordinatescluster metadatawritewritewritereadreadreadreplicatereplicate
ComponentDescription
BrokerServer that stores data and serves client requests
ControllerBroker responsible for cluster coordination (leader election, partition assignment)
TopicNamed category of records; logical grouping of related events
PartitionOrdered, immutable sequence of records within a topic
ReplicaCopy of a partition for fault tolerance
ProducerClient that publishes records to topics
ConsumerClient that subscribes to topics and processes records
Consumer GroupSet of consumers that coordinate to consume a topic

Brokers are the servers that form a Kafka cluster. Each broker:

  • Stores partition data on disk
  • Handles produce and fetch requests from clients
  • Replicates data to other brokers
  • Participates in cluster coordination
Kafka broker internal components from network layer to storageKafka broker internal components from network layer to storageKafka BrokerNetwork LayerRequest ProcessingStorage LayerReplicationAcceptorThreadsNetworkThreadsRequestQueueI/O Threads(num.io.threads)ResponseQueueLog ManagerPartitionLogsIndexFilesReplicaFetcherReplicaManagerconnectionsrequestsprocessread/writeresponsesendsync
ConfigurationDefaultDescription
broker.id-1 (auto)Unique identifier for this broker
log.dirs/tmp/kafka-logsDirectories for partition data
num.network.threads3Threads for network I/O
num.io.threads8Threads for disk I/O
socket.send.buffer.bytes102400Socket send buffer
socket.receive.buffer.bytes102400Socket receive buffer
socket.request.max.bytes104857600Maximum request size (100MB)

Broker Deep Dive


The controller manages cluster-wide metadata operations. In KRaft mode, controllers are a dedicated process role; in ZooKeeper mode, a broker is elected as controller.

ResponsibilityDescription
Leader electionElect new partition leaders when leaders fail
Partition assignmentAssign partitions to brokers
Broker registrationTrack broker membership in cluster
Topic managementCreate and delete topics
Metadata propagationDistribute cluster metadata to all brokers

Kafka is transitioning from ZooKeeper to KRaft (Kafka Raft) for cluster coordination.

ZooKeeper mode and KRaft mode cluster coordination comparedZooKeeper mode and KRaft mode cluster coordination comparedZooKeeper ModeZooKeeper EnsembleKafka BrokersKRaft ModeController QuorumKafka BrokersZK 1ZK 2ZK 3ControllerBroker 2Broker 3Controller 1(Leader)Controller 2Controller 3Broker 1Broker 2Broker 3leader election,metadataregistrationregistrationRaftRaftRaftmetadatametadatametadata
AspectZooKeeper ModeKRaft Mode
External dependencyZooKeeper cluster requiredNone
Metadata storageSplit (ZK + broker logs)Unified (__cluster_metadata topic)
Failover timeSeconds to minutesMilliseconds to seconds
Partition scaleHigher metadata overhead; lower practical limitsHigher practical partition counts
Operational complexityTwo systems to manageSingle system
VersionRemoved in Kafka 4.0Kafka 3.3+ (production ready)

KRaft Deep Dive


A topic is divided into partitions—ordered, append-only logs. Partitions enable:

  • Parallelism: Multiple consumers can read different partitions concurrently
  • Ordering: Records within a partition maintain strict order
  • Scalability: Partitions can be distributed across brokers
Topic partitions with partition-local offsetsTopic partitions with partition-local offsetsTopic: orders (3 partitions, RF=3)Partition 0Partition 1Partition 2Offset 0Offset 1Offset 2...Offset 0Offset 1...Offset 0Offset 1Offset 2Offset 3...Each partition is independentOffsets are partition-localNo ordering across partitions

Each partition has multiple replicas distributed across brokers. One replica is the leader; others are followers.

Partition replication and acknowledgement with acks=allBroker 1ProducerBroker 1Broker 2Broker 3ProducerProducerBroker 1(Leader)Broker 1(Leader)Broker 2(Follower)Broker 2(Follower)Broker 3(Follower)Broker 3(Follower)Broker 1produce(key, value)append to logreplicatereplicateackackack (acks=all)ISR (In-Sync Replicas): {leader, f1, f2}High Watermark advances after all ISR ack

For complete ISR mechanics, leader election protocol, and acknowledgment configuration, see Replication.

ConceptDescription
LeaderReplica that handles all reads and writes for a partition
FollowerReplica that replicates from leader; can become leader if current leader fails
ISR (In-Sync Replicas)Set of replicas that are fully caught up with leader
High WatermarkOffset up to which all ISR have replicated; consumers can only read up to HW
LEO (Log End Offset)Latest offset in the leader's log

The replication factor determines how many copies of each partition exist:

RFBehaviorUse Case
1No redundancy; data loss on broker failureDevelopment only
2Tolerates 1 broker failure with min.insync.replicas=1Limited production use
3Tolerates 1 broker failure with min.insync.replicas=2Production standard
4+Higher durability; rarely neededCritical data

Replication Deep Dive


Kafka stores data in log segments on disk. The storage design prioritizes sequential I/O for maximum throughput. For complete storage internals including indexes, compaction, and retention policies, see Storage Engine.

Partition directory with active and closed log segmentsPartition directory with active and closed log segmentsPartition DirectoryActive SegmentClosed Segments00000000000000012345.log00000000000000012345.index00000000000000012345.timeindex00000000000000000000.log00000000000000000000.index00000000000000005000.log00000000000000005000.indexActive segment receivesnew writes (append-only)Closed segments are immutableSubject to retention/compaction
FilePurpose
.logMessage data (key, value, headers, metadata)
.indexOffset-to-position index for efficient seeking
.timeindexTimestamp-to-offset index for time-based seeking
.txnindexTransaction index (for transactional messages)
.snapshotProducer state snapshots
PolicyConfigurationBehavior
Time-basedretention.msDelete segments older than threshold
Size-basedretention.bytesDelete oldest segments when partition exceeds size
Compactioncleanup.policy=compactKeep only latest value per key

Storage Engine Deep Dive


Kafka achieves high throughput through several design choices. For detailed performance tuning, benchmarking, and optimization techniques, see Performance Internals.

Random I/O compared with Kafka sequential append I/ORandom I/O compared with Kafka sequential append I/ORandom I/O (Traditional DB)Sequential I/O (Kafka)SeekReadSeekWriteSeekAppendAppendAppendRead batch~100 IOPS on HDDRandom seeks dominate~100 MB/s on HDDNo seek overhead

Kafka uses sendfile() to transfer data directly from disk to network, bypassing user-space copies.

Traditional copy path compared with zero-copy sendfile transferTraditional copy path compared with zero-copy sendfile transferTraditional CopyZero-Copy (sendfile)DiskKernel BufferUser BufferKernel BufferNetworkDiskPage CacheNetwork4 copies2 context switches0 copies to user spaceDirect kernel transfer1. read2. copy3. copy4. send1. read2. sendfile()

TLS Disables Zero-Copy

When TLS encryption is enabled, zero-copy is not possible because data must be encrypted in user space. Throughput impact depends on CPU and workload.

Producers batch messages before sending, and consumers fetch in batches:

Batching PointConfigurationBenefit
Producerbatch.size, linger.msAmortize network overhead, enable compression
BrokerInternal batchingEfficient disk writes
Consumerfetch.min.bytes, fetch.max.wait.msReduce fetch requests

Kafka relies on the OS page cache rather than managing its own cache:

BenefitDescription
Automatic managementOS handles cache eviction
Warm restartsCache survives broker restarts
Memory efficiencyAvoids double-buffering in JVM
Read-aheadOS prefetches sequential reads

Performance Internals


Kafka survives failures at multiple levels. For complete failure scenarios, recovery procedures, and monitoring strategies, see Fault Tolerance.

FailureKafka Response
Single brokerLeader election; ISR continues serving
Multiple brokersService continues if enough replicas remain
Rack failureRack-aware placement ensures cross-rack replicas
Network partitionISR shrinks; acks=all writes fail if ISR < min.insync.replicas
Disk failureReplicas on failed log dirs go offline; leaders move to healthy replicas
SettingValueBehavior
acks=allProducer waits for all ISRStrongest durability
min.insync.replicas=2Require 2 replicas for writesPrevents single-replica writes
unclean.leader.election.enable=falseOnly ISR can become leaderPrevents data loss on failover

Fault Tolerance Guide