How Kafka Works End to End
-
You set up a Kafka cluster - it’s made up of multiple brokers (servers).
-
You create topics - like channels or folders where messages will go.
-
Producers are apps or services that send messages to Kafka topics.
-
Each message is just a piece of data (like a log, event, or JSON).
-
Kafka stores these messages inside partitions - each topic is split into multiple partitions for scalability.
-
Messages in a partition are ordered - always appended at the end.
-
Kafka stores the data durably on disk and keeps it for a configured time (e.g., 7 days), even if it’s already read.
-
Consumers are apps or services that read messages from topics.
-
Consumers track their own position (called offset) to know what they’ve already read.
-
Kafka doesn’t delete messages just because they’re read - different consumers can read at their own pace.
-
Multiple consumers can read the same topic in consumer groups - Kafka ensures each message goes to only one consumer in a group.
-
If a consumer crashes, Kafka reassigns its share of work to others in the group.
-
Kafka can handle millions of messages per second - it’s built for speed and scale.
-
It’s used for decoupling systems - producer doesn’t need to know who the consumer is.
-
Kafka is great for real-time pipelines, event sourcing, activity tracking, log aggregation, etc.
-
Optional: You can add Kafka Connect to plug in data sources/sinks like DBs, or Kafka Streams for processing on the fly.
-
Kafka ensures high durability, fault tolerance, and horizontal scalability out of the box.
-
Kafka automatically replicates data across multiple brokers - so even if one server dies, your messages are safe.
Kafka Architecture Deep Dive
Broker Internals
A Kafka broker is a server in the Kafka cluster that:
- Manages Storage: Maintains partition commit logs
- Handles Network Protocol: Serves producer/consumer requests
- Replication Management: Coordinates replicas between brokers
- Group Coordination: For consumer group management (leader brokers)
- Controller Functions: One broker acts as a controller for the cluster
Brokers are identified by unique IDs and store partition data in segments:
- Log Segments: The actual files containing message data
- Indexes: Offset and time-based indexes for fast message lookup
- Active Segment: The currently writable segment
Partitioning Mechanics
Partitions are the fundamental unit of parallelism in Kafka:
-
Partitioning Strategy:
- Default: Round-robin or hash of the message key
- Custom: Producers can implement custom partitioners
- Sticky: Batch messages to same partition for efficiency
-
Partition Leaders and Followers:
- Each partition has one leader and multiple followers (replicas)
- Leaders handle all reads and writes
- Followers passively replicate the leader
-
Partition Reassignment:
- Rebalancing partitions across brokers
- Automatically happens when brokers join/leave
- Can be manually triggered for load balancing
Replication Protocol
Kafka uses a leader-follower replication model:
-
In-Sync Replicas (ISR):
- Set of replicas that are fully caught up with the leader
- Maintained by the controller
- Min.insync.replicas setting controls durability guarantees
-
Replication Flow:
- Producer sends message to leader
- Leader appends to local log
- Followers fetch from leader (pull-based)
- Message is considered “committed” when all ISRs have replicated it
-
Leader Election:
- If a leader fails, a follower from ISR becomes the new leader
- Unclean leader election (allowing non-ISR followers) is configurable
Message Format
Kafka messages have a sophisticated binary format:
-
Message Structure:
- Timestamp: When message was created
- Key (optional): For partitioning and compaction
- Value: The actual payload (can be null)
- Headers: Optional metadata (key-value pairs)
-
Record Batches:
- Messages are grouped into batches for efficiency
- Compression happens at batch level (gzip, snappy, lz4, or zstd)
- Each batch shares metadata like producer ID and timestamp base
-
Message Versions:
- v0, v1: Legacy formats
- v2: Current format with better performance and features
Producer Internals
Kafka producers are highly optimized:
-
Buffering and Batching:
- Messages are queued in memory before sending
- Batched by partition for network efficiency
- Controlled by batch.size and linger.ms
-
Acknowledgment Modes (acks):
- acks=0: Fire and forget (no wait for acknowledgment)
- acks=1: Wait for leader acknowledgment
- acks=all: Wait for all in-sync replicas to acknowledge
-
Retries and Idempotence:
- Automatic retries on transient failures
- Idempotent producer uses sequence numbers to prevent duplicates
- Transactions allow atomic multi-partition writes
Consumer Internals
Kafka consumers have sophisticated features:
-
Offset Management:
- Stored in special topic __consumer_offsets
- auto.offset.reset controls behavior for new consumers
- Can be committed automatically or manually
-
Consumer Groups:
- Group Coordinator broker manages membership
- Rebalance Protocol:
- Eager: Stop all consumers during rebalance
- Cooperative: Incremental reassignment
-
Fetch Protocol:
- Consumers pull data from brokers (not push)
- Controlled by max.poll.records and fetch.max.bytes
- Backpressure handled naturally by consumer pace
Kafka’s Storage Engine
Kafka’s storage is optimized for sequential operations:
-
Log Structure:
- Append-only commit log
- Immutable segments (files)
- Sequential writes for high performance
-
Retention Policies:
- Time-based: Delete segments older than N days
- Size-based: Keep only N bytes per partition
- Compaction: Keep only latest value per key
-
Zero-Copy:
- Uses sendfile() system call
- Data goes directly from disk to network
- Bypasses application buffer for efficiency
Zookeeper’s Role (Pre-KRaft)
Traditionally, Kafka relied on Zookeeper for:
-
Cluster Coordination:
- Broker registration
- Topic configuration
- Access control lists
-
Leader Election:
- Managing controller elections
- Tracking partition leadership
-
KRaft Mode:
- Newer versions can run without Zookeeper
- Uses Raft consensus protocol built into Kafka
- Improves scalability and simplifies architecture
Advanced Features
-
Kafka Streams:
- Stream processing library built on Kafka
- Stateful and stateless transformations
- Exactly-once semantics with processing guarantees
- Windows, joins, aggregations on streaming data
-
Schema Registry:
- Central repository for message schemas
- Schema evolution with compatibility checks
- Common formats: Avro, Protobuf, JSON Schema
-
MirrorMaker 2:
- Cross-cluster replication
- Geo-replication for disaster recovery
- Active-active multi-datacenter setups
-
Kafka Connect:
- Data integration framework
- Source connectors: Import data to Kafka
- Sink connectors: Export data from Kafka
- Distributed or standalone mode
- Offset tracking and exactly-once delivery
Performance Tuning
-
Broker Tuning:
- num.network.threads and num.io.threads
- log.flush.interval.messages and log.flush.interval.ms
- replica.fetch.max.bytes for replication throughput
-
Producer Tuning:
- buffer.memory for producer memory allocation
- compression.type for message compression
- batch.size and linger.ms for batching control
-
Consumer Tuning:
- fetch.min.bytes and fetch.max.wait.ms
- max.partition.fetch.bytes for batch size
- heartbeat.interval.ms for group membership
-
Topic Tuning:
- Partition count for parallelism
- Replication factor for reliability
- min.insync.replicas for durability guarantees