Architecture
Before learning Producers, Consumers, Replication, or Fault Tolerance, it is important to understand the core building blocks that make up a Kafka cluster.
At its heart, Kafka is a distributed append-only log system. Every message produced to Kafka is stored in logs, organized into topics and partitions, and managed by brokers.
The primary building blocks are:
- Kafka Cluster
- Brokers
- Topics
- Partitions
- Records (Messages)
- Offsets
Understanding these concepts is essential because nearly every advanced Kafka feature is built on top of them.
What is a Kafka Cluster?
A Kafka Cluster is a collection of one or more Kafka brokers working together to store and serve data.
A single broker can run Kafka, but production environments almost always use multiple brokers.
A cluster provides:
- Scalability
- High Availability
- Fault Tolerance
- Load Distribution
Why Not Use a Single Server?
Imagine an application generating millions of events every second.
A single machine would eventually face:
- CPU limitations
- Memory limitations
- Disk limitations
- Network limitations
Kafka solves this by distributing data across multiple brokers.
Data is spread across machines, allowing Kafka to scale horizontally.
High-Level Data Flow
A typical Kafka workflow looks like:
Step-by-step:
- Producer sends data.
- Data is written into a topic.
- Topic is divided into partitions.
- Partitions are stored on brokers.
- Consumers read data.
Kafka Brokers
A Broker is a Kafka server responsible for storing and serving data.
Every Kafka cluster consists of one or more brokers.
Think of a broker as a specialized database server that stores Kafka partitions.
One Kafka broker can store multiple partitions/topics.
Responsibilities of a Broker
A broker performs several tasks:
- Store Messages: Messages are persisted to disk.
- Serve Consumer Requests: Consumers fetch messages from brokers.
- Handle Producer Requests: Producers send messages to brokers.
- Replicate Data: Brokers replicate partitions to other brokers.
- Participate in Cluster Coordination: Brokers exchange metadata and coordinate cluster operations.
Broker Identification
Each broker has a unique identifier.
Example:
Broker 1
Broker 2
Broker 3
Kafka uses broker IDs to identify nodes in the cluster.
Broker Storage
Each broker stores partition data locally.
A broker may host partitions from many topics.
Topics
A Topic is a logical category used to organize messages.
Examples:
orders
payments
users
notifications
logs
A topic acts like a stream of events.
Real-World Example
Suppose an e-commerce application exists.
Different events may be stored in different topics:
orders
payments
inventory
shipments
This separation improves organization and scalability.
Topic as a Stream
Think of a topic as an endless stream.
New messages continue to arrive.
The stream never truly ends.
Topics are Logical
A common misconception is:
A topic physically stores data.
This is incorrect.
A topic is a logical abstraction.
Actual data is stored inside partitions.
Partitions
Without partitions, a topic would become a bottleneck.
Consider:
All traffic would hit one machine. This limits throughput.
Partitioning for Scalability
Kafka divides topics into partitions.
Each partition can live on a different broker.
This allows parallel processing.
Partition Structure
A partition is an append-only log.
Messages are always appended to the end.
Existing messages are never modified.
Ordering Guarantee
Kafka guarantees ordering only within a partition.
Example:
Partition 0
Order Created
Order Paid
Order Shipped
Order Delivered
Order is preserved.
Across multiple partitions:
Partition 0 -> Order A
Partition 1 -> Order B
Partition 2 -> Order C
Global ordering is not guaranteed.
Parallel Processing
Partitions enable parallelism.
Multiple consumers can process data simultaneously.
Partition Keying
Messages may include keys.
Example:
User ID = 101
User ID = 101
User ID = 101
Kafka hashes the key.
All messages with the same key go to the same partition.
This preserves ordering for that user.
Records (Messages)
The smallest unit stored in Kafka is called a Record.
A record contains:
- Key
- Value
- Timestamp
- Headers
- Offset
Record Structure
Record
├── Key
├── Value
├── Timestamp
├── Headers
└── Offset
Key
The key determines partition placement.
Example:
Key: User-101
Messages with identical keys usually land in the same partition.
Value
The value is the actual payload.
Example:
{
"orderId": 1001,
"amount": 500
}
Timestamp
Kafka stores timestamps for records.
Example:
2026-01-10T10:15:00Z
Used for:
- Retention
- Monitoring
- Event processing
Headers
Headers contain metadata.
Example:
source=payment-service
version=v1
Offsets
Every record inside a partition receives a unique sequential number.
This number is called an Offset.
Example
Partition 0
Offset 0 -> Order Created
Offset 1 -> Order Paid
Offset 2 -> Order Shipped
Offset 3 -> Order Delivered
Visual Representation
Offsets always increase.
Important Rule
Offsets are unique only within a partition.
Example:
Partition 0
Offset 0
Offset 1
Offset 2
Partition 1
Offset 0
Offset 1
Offset 2
The same offset value may exist in multiple partitions.
Why Offsets Matter
Offsets allow consumers to track progress.
Example:
Current Offset = 150
Consumer knows:
- Messages before 150 were processed.
- Messages after 150 remain unread.
Reading from Offsets
Consumers can start reading from:
Beginning
Offset 0
Latest
Most Recent Offset
Specific Offset
Offset 500
This makes replaying historical events possible.
Relationship Between All Components
The entire hierarchy looks like: