DEV Community

Cover image for Understanding Kafka Architecture - The Complete Mental Model 🧠
Ajinkya Singh
Ajinkya Singh

Posted on • Edited on

Understanding Kafka Architecture - The Complete Mental Model 🧠

How all the pieces fit together to create a powerful streaming platform


The Goal

Understand the "Big Picture" - How events, topics, partitions, producers, consumers, brokers, and consumer groups all work together as one cohesive system.

Think of this as getting a bird's eye view of the entire Kafka ecosystem! πŸ¦…


Building Block #1: The Event (Foundation)

What It Is

The fundamental unit - an immutable fact representing something that happened.

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚           EVENT/RECORD              β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Key: user_456                       β”‚
β”‚ Value: {"action": "purchase"}       β”‚
β”‚ Timestamp: 2025-11-18 14:30:00     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Everything in Kafka revolves around these!
Enter fullscreen mode Exit fullscreen mode

Building Block #2: The Kafka Cluster (Infrastructure)

What It Is

A collection of servers working together - NOT just one server!

        KAFKA CLUSTER
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                                 β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”        β”‚
β”‚  β”‚Broker 1β”‚  β”‚Broker 2β”‚  ...   β”‚
β”‚  β”‚Server 1β”‚  β”‚Server 2β”‚        β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜        β”‚
β”‚                                 β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”        β”‚
β”‚  β”‚Broker 3β”‚  β”‚Broker 4β”‚  ...   β”‚
β”‚  β”‚Server 3β”‚  β”‚Server 4β”‚        β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜        β”‚
β”‚                                 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Network of powerful servers!
Enter fullscreen mode Exit fullscreen mode

What Brokers Do

  • Store your events
  • Handle requests from applications
  • Ensure the system stays available even if one fails

Why Multiple Brokers?

  1. Scalability β†’ Handle massive amounts of data
  2. Fault Tolerance β†’ Keep running even if servers fail

Modern Kafka (4.0+)

  • Brokers are self-managing using KRaft protocol
  • They coordinate with each other internally
  • No external ZooKeeper needed! πŸŽ‰

Visualize: A resilient network of powerful servers ready to handle your data streams.


Building Block #3: Topics (Organization)

What It Is

A logical name/category for a stream of related events.

KAFKA CLUSTER
β”œβ”€β”€ Topic: "user-signups" πŸ‘€
β”œβ”€β”€ Topic: "payment-transactions" πŸ’°
β”œβ”€β”€ Topic: "sensor-readings" 🌑️
└── Topic: "order-events" πŸ“¦
Enter fullscreen mode Exit fullscreen mode

Key Characteristics

1. Distributed Across Brokers

Single topic doesn't live on just ONE broker:

Topic: "orders"
β”œβ”€β”€ Partition 0 β†’ Broker 1
β”œβ”€β”€ Partition 1 β†’ Broker 2
└── Partition 2 β†’ Broker 3

This distribution = SCALE! πŸš€
Enter fullscreen mode Exit fullscreen mode

2. Durable Storage

  • Events stored for configurable retention period
  • Can be re-read multiple times
  • Not deleted after consumption

Building Block #4: Partitions (Parallelism)

What It Is

Each topic is divided into ordered lanes called partitions.

The Multi-Lane Highway Analogy πŸ›£οΈ

Topic: "orders" (3 partitions)

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚               MULTI-LANE HIGHWAY                   β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                    β”‚
β”‚  Lane 0 (Partition 0): Order1 β†’ Order2 β†’ Order3  β”‚
β”‚  ═════════════════════════════════════════════►   β”‚
β”‚                                                    β”‚
β”‚  Lane 1 (Partition 1): Order4 β†’ Order5 β†’ Order6  β”‚
β”‚  ═════════════════════════════════════════════►   β”‚
β”‚                                                    β”‚
β”‚  Lane 2 (Partition 2): Order7 β†’ Order8 β†’ Order9  β”‚
β”‚  ═════════════════════════════════════════════►   β”‚
β”‚                                                    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Each lane (partition) processes traffic (events) 
independently but IN ORDER within that lane!
Enter fullscreen mode Exit fullscreen mode

Key Properties

1. Ordered Within Partition βœ…

Partition 0:
Event A (offset 0) β†’ Event B (offset 1) β†’ Event C (offset 2)

Consumer always sees: A, then B, then C
ORDER GUARANTEED within the partition!
Enter fullscreen mode Exit fullscreen mode

2. NO Order Across Partitions ❌

Partition 0: Event A (time: 10:00)
Partition 1: Event B (time: 09:59)

Consumer might see B before A
NO ORDER GUARANTEE across different partitions!
Enter fullscreen mode Exit fullscreen mode

3. Each Partition Lives on a Broker

Topic: "payments" (3 partitions)

Partition 0 β†’ Broker 1 (Server 1)
Partition 1 β†’ Broker 2 (Server 2)
Partition 2 β†’ Broker 3 (Server 3)

Load is DISTRIBUTED across servers! βš–οΈ
Enter fullscreen mode Exit fullscreen mode

Why Partitions?

  • Enable parallelism β†’ Multiple producers/consumers work simultaneously
  • Distribute load β†’ Spread data across multiple servers
  • Scale horizontally β†’ Add more partitions = more throughput

Building Block #5: Producers (Data Writers)

What It Is

Your application code that sends/publishes events to Kafka topics.

         PRODUCERS (Entry Ramps)

Mobile App πŸ“± ──┐
                β”‚
Web Server 🌐 ──┼──► Kafka Topic: "events"
                β”‚      β”œβ”€β–Ί Partition 0
IoT Device 🌑️ β”€β”€β”˜      β”œβ”€β–Ί Partition 1
                       └─► Partition 2
Enter fullscreen mode Exit fullscreen mode

How Producers Work

Option 1: Automatic Partition Selection (No Key)

Producer sends events WITHOUT key:

Event 1 β†’ Partition 0 (round-robin)
Event 2 β†’ Partition 1 (round-robin)
Event 3 β†’ Partition 2 (round-robin)
Event 4 β†’ Partition 0 (round-robin)
...

Result: EVEN DISTRIBUTION across partitions
Enter fullscreen mode Exit fullscreen mode

Option 2: Key-Based Routing (With Key)

Producer sends events WITH key:

Event (key: user_123) β†’ Partition 1
Event (key: user_123) β†’ Partition 1 (SAME!)
Event (key: user_456) β†’ Partition 2
Event (key: user_456) β†’ Partition 2 (SAME!)
Event (key: user_123) β†’ Partition 1 (SAME!)

Result: ALL events with SAME KEY go to SAME PARTITION
        This maintains ORDER for related events! 🎯
Enter fullscreen mode Exit fullscreen mode

Visual Example: Key-Based Routing

Producer: E-commerce Website

Order from user_123:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Key: user_123        β”‚
β”‚ Value: Order details β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         ↓
    Kafka hashes key
         ↓
    Always β†’ Partition 1

Another order from user_123:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Key: user_123        β”‚
β”‚ Value: Order details β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         ↓
    Kafka hashes key
         ↓
    Always β†’ Partition 1 (SAME!)

βœ… All user_123 orders processed IN ORDER!
Enter fullscreen mode Exit fullscreen mode

Producer Behavior

  • Asynchronous β†’ Send and move on (don't wait for consumer)
  • High throughput β†’ Can send thousands of events per second
  • Fire and forget β†’ Ensures speed

Visualize: Entry ramps onto a highway, directing traffic into specific lanes.


Building Block #6: Consumers (Data Readers)

What It Is

Your application code that reads/subscribes to events from topics.

         Kafka Topic: "orders"
                 ↓
         β”Œβ”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”
         β”‚               β”‚
    Consumer A      Consumer B
         ↓               ↓
   Analytics App    Email Service

Each reads INDEPENDENTLY with its own position (offset)
Enter fullscreen mode Exit fullscreen mode

Key Properties

1. Pull-Based Model

Traditional Systems:        Kafka:
Server β†’ PUSHES β†’ Client   Client ← PULLS ← Server

Benefits of Pull:
βœ… Consumer controls pace
βœ… Can process at own speed
βœ… Can pause/resume
Enter fullscreen mode Exit fullscreen mode

2. Independent Reading

Multiple consumers can read SAME topic:

Topic: "transactions"
     ↓
     β”œβ”€β”€β–Ί Consumer A (reads everything)
     β”œβ”€β”€β–Ί Consumer B (reads everything)
     └──► Consumer C (reads everything)

Each maintains its OWN offset (reading position)
Nobody affects anyone else! 🎭
Enter fullscreen mode Exit fullscreen mode

3. Offset Tracking

Partition 0:
β”Œβ”€β”€β”€β”€β”¬β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”
β”‚ 0  β”‚ 1  β”‚ 2  β”‚ 3  β”‚ 4  β”‚ 5  β”‚ ...
β””β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”˜
              ↑
         Consumer's
         current offset
         (remembers position)

If consumer stops and restarts:
βœ… Resumes from last offset (position 2)
βœ… No messages skipped
βœ… No messages duplicated
Enter fullscreen mode Exit fullscreen mode

Building Block #7: Consumer Groups (Team Work)

What It Is

A collection of consumer instances working together as a team to process events.

The Team Analogy πŸ‘₯

Team A (Consumer Group "analytics"):
Worker 1, Worker 2, Worker 3

Team B (Consumer Group "email"):
Worker 4, Worker 5

Team C (Consumer Group "archiving"):
Worker 6, Worker 7, Worker 8

Each TEAM gets its own FULL COPY of the event stream!
Enter fullscreen mode Exit fullscreen mode

How Consumer Groups Work

Rule: One Partition = One Consumer (within group)

Topic: "orders" (3 partitions)

Consumer Group "order-processors" (3 consumers):

Partition 0 ──► Consumer A ┐
Partition 1 ──► Consumer B β”œβ”€ Group "order-processors"
Partition 2 ──► Consumer C β”˜

βœ… Each partition assigned to EXACTLY ONE consumer
βœ… Work is DIVIDED among team members
βœ… Parallel processing! ⚑
Enter fullscreen mode Exit fullscreen mode

Example: Load Distribution

Scenario 1: More partitions than consumers

Topic: 4 partitions
Group: 2 consumers

Partition 0 ──┐
Partition 1 ──┼──► Consumer A
               β”‚
Partition 2 ───
Partition 3 ──┴──► Consumer B

Each consumer handles 2 partitions
Enter fullscreen mode Exit fullscreen mode
Scenario 2: More consumers than partitions

Topic: 2 partitions
Group: 3 consumers

Partition 0 ──► Consumer A
Partition 1 ──► Consumer B
                Consumer C (IDLE - no partition assigned)

Extra consumers sit idle (but ready for failover!)
Enter fullscreen mode Exit fullscreen mode
Scenario 3: Perfect match

Topic: 3 partitions
Group: 3 consumers

Partition 0 ──► Consumer A
Partition 1 ──► Consumer B
Partition 2 ──► Consumer C

Perfectly balanced! βš–οΈ
Enter fullscreen mode Exit fullscreen mode

Multiple Consumer Groups (Independent Processing)

Topic: "news-feed"
     β”‚
     β”œβ”€β”€β–Ί Group A "website-updates"
     β”‚    β”œβ”€ Consumer 1 β†’ Partition 0
     β”‚    β”œβ”€ Consumer 2 β†’ Partition 1
     β”‚    └─ Consumer 3 β†’ Partition 2
     β”‚
     β”œβ”€β”€β–Ί Group B "archiving"
     β”‚    β”œβ”€ Consumer 1 β†’ Partition 0
     β”‚    β”œβ”€ Consumer 2 β†’ Partition 1
     β”‚    └─ Consumer 3 β†’ Partition 2
     β”‚
     └──► Group C "sentiment-analysis"
          └─ Consumer 1 β†’ All partitions

βœ… Each group processes SAME data INDEPENDENTLY
βœ… Each group maintains its OWN offsets
βœ… Groups don't affect each other
Enter fullscreen mode Exit fullscreen mode

Automatic Failover (Self-Healing)

Before failure:
Partition 0 ──► Consumer A βœ…
Partition 1 ──► Consumer B βœ…
Partition 2 ──► Consumer C βœ…

Consumer B fails! πŸ’₯

After automatic rebalancing (seconds):
Partition 0 ──► Consumer A βœ…
Partition 1 ──► Consumer A βœ… (took over!)
Partition 2 ──► Consumer C βœ…

Or:
Partition 0 ──► Consumer A βœ…
Partition 1 ──► Consumer C βœ… (took over!)
Partition 2 ──► Consumer C βœ…

βœ… No data loss!
βœ… Processing continues!
Enter fullscreen mode Exit fullscreen mode

Visualize: Teams of workers where each team processes the full stream, but within each team, workers divide up the lanes (partitions) to work in parallel.


THE GRAND PICTURE: How Everything Works Together 🎯

Complete Data Flow

STEP 1: PRODUCERS CREATE EVENTS
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Mobile App, Website, IoT Devices, etc. β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                 ↓
          Generate Events

STEP 2: EVENTS SENT TO TOPICS
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Event with key "user_123"              β”‚
β”‚  β†’ Kafka hashes key                     β”‚
β”‚  β†’ Routes to specific partition         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                 ↓
           Topic: "orders"

STEP 3: PARTITIONS STORE EVENTS
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Partition 0 (Broker 1): [E1, E2, E3]   β”‚
β”‚ Partition 1 (Broker 2): [E4, E5, E6]   β”‚
β”‚ Partition 2 (Broker 3): [E7, E8, E9]   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                 ↓
        Ordered, Immutable Log

STEP 4: CONSUMER GROUPS PULL EVENTS
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Group "analytics":                      β”‚
β”‚   Consumer A reads Partition 0          β”‚
β”‚   Consumer B reads Partition 1          β”‚
β”‚   Consumer C reads Partition 2          β”‚
β”‚                                         β”‚
β”‚ Group "email":                          β”‚
β”‚   Consumer D reads Partition 0          β”‚
β”‚   Consumer E reads Partition 1, 2       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                 ↓
         Process in parallel
         at their own pace
Enter fullscreen mode Exit fullscreen mode

Visual: Complete System Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    KAFKA CLUSTER                            β”‚
β”‚                                                             β”‚
β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”‚
β”‚   β”‚Broker 1 β”‚  β”‚Broker 2 β”‚  β”‚Broker 3 β”‚  β”‚Broker 4 β”‚     β”‚
β”‚   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€  β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€  β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€  β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€     β”‚
β”‚   β”‚ P0 (L)  β”‚  β”‚ P1 (L)  β”‚  β”‚ P2 (L)  β”‚  β”‚ P3 (L)  β”‚     β”‚
β”‚   β”‚ P1 (F)  β”‚  β”‚ P2 (F)  β”‚  β”‚ P3 (F)  β”‚  β”‚ P0 (F)  β”‚     β”‚
β”‚   β”‚ P2 (F)  β”‚  β”‚ P3 (F)  β”‚  β”‚ P0 (F)  β”‚  β”‚ P1 (F)  β”‚     β”‚
β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β”‚
β”‚        ↑                           ↓                       β”‚
β”‚    WRITE                         READ                      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚                           β”‚
    β”Œβ”€β”€β”€β”€β”΄β”€β”€β”€β”€β”               β”Œβ”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”
    β”‚PRODUCERSβ”‚               β”‚CONSUMER    β”‚
    β”‚         β”‚               β”‚GROUPS      β”‚
    β”‚πŸ“± App   β”‚               β”‚            β”‚
    β”‚πŸŒ Web   β”‚               β”‚Group A:    β”‚
    β”‚πŸŒ‘οΈ IoT   β”‚               β”‚ C1, C2, C3 β”‚
    β”‚         β”‚               β”‚            β”‚
    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜               β”‚Group B:    β”‚
                              β”‚ C4, C5     β”‚
                              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Legend:
P0 = Partition 0
(L) = Leader
(F) = Follower (replica)
Enter fullscreen mode Exit fullscreen mode

Real-World Example: E-Commerce Order System

The Complete Flow

SCENARIO: Customer places an order on website

1️⃣ PRODUCER (Website) creates event:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Key: customer_789                β”‚
β”‚ Value: {                         β”‚
β”‚   order_id: "ORD-456",          β”‚
β”‚   items: ["laptop", "mouse"],   β”‚
β”‚   total: 1200                    β”‚
β”‚ }                                β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         ↓

2️⃣ Kafka routes to TOPIC and PARTITION:
Topic: "orders"
Key "customer_789" β†’ Partition 1 (always same partition!)
         ↓

3️⃣ BROKERS store in partition:
Broker 2 (Leader for Partition 1):
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Partition 1:                   β”‚
β”‚ Offset 100: ORD-453           β”‚
β”‚ Offset 101: ORD-454           β”‚
β”‚ Offset 102: ORD-456 ← NEW!    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Broker 3 (Follower):           Broker 4 (Follower):
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Partition 1 (copy):  β”‚       β”‚ Partition 1 (copy):  β”‚
β”‚ Offset 102: ORD-456  β”‚       β”‚ Offset 102: ORD-456  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         ↓                              ↓
    REPLICATED for durability!

4️⃣ MULTIPLE CONSUMER GROUPS process independently:

Group "payment-processing":
  Consumer A reads Partition 1 β†’ Charges credit card

Group "inventory":
  Consumer B reads Partition 1 β†’ Updates stock

Group "email":
  Consumer C reads Partition 1 β†’ Sends confirmation

Group "analytics":
  Consumer D reads Partition 1 β†’ Updates dashboard

βœ… All process SAME order
βœ… All work INDEPENDENTLY
βœ… Each at their own pace
Enter fullscreen mode Exit fullscreen mode

Key Principles That Make It All Work

1. Distribution

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Work spread across many servers  β”‚
β”‚ βœ… Scalability                   β”‚
β”‚ βœ… Parallel processing           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Enter fullscreen mode Exit fullscreen mode

2. Immutability

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Events never change or deleted   β”‚
β”‚ βœ… Can be replayed               β”‚
β”‚ βœ… Multiple consumers can read   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Enter fullscreen mode Exit fullscreen mode

3. Parallelism

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Multiple partitions processed    β”‚
β”‚ simultaneously                    β”‚
β”‚ βœ… High throughput               β”‚
β”‚ βœ… Efficient resource use        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Enter fullscreen mode Exit fullscreen mode

Fault Tolerance in Action

When Broker Fails

Before:
Broker 1 (P0-Leader) βœ…
Broker 2 (P0-Follower) βœ…
Broker 3 (P0-Follower) βœ…

Broker 1 fails! πŸ’₯

After (2-3 seconds):
Broker 1 (P0-Leader) πŸ’€
Broker 2 (P0-Leader) ⭐ Promoted!
Broker 3 (P0-Follower) βœ…

βœ… System keeps running
βœ… No data loss
Enter fullscreen mode Exit fullscreen mode

When Consumer Fails

Before:
Partition 0 β†’ Consumer A βœ…
Partition 1 β†’ Consumer B βœ…
Partition 2 β†’ Consumer C βœ…

Consumer B fails! πŸ’₯

After (seconds):
Partition 0 β†’ Consumer A βœ…
Partition 1 β†’ Consumer A βœ… Took over!
Partition 2 β†’ Consumer C βœ…

βœ… Processing continues
βœ… No events missed
Enter fullscreen mode Exit fullscreen mode

Summary: The Mental Model Checklist

The 7 Components

βœ… Events - The data (immutable facts)
βœ… Cluster - Network of servers
βœ… Brokers - Individual servers in cluster
βœ… Topics - Categories for events
βœ… Partitions - Ordered lanes within topics
βœ… Producers - Apps that write events
βœ… Consumers - Apps that read events
βœ… Consumer Groups - Teams that work together

The Flow

Producers β†’ Topics β†’ Partitions β†’ Brokers
                                    ↓
                            Consumer Groups
Enter fullscreen mode Exit fullscreen mode

The Guarantees

  • βœ… Order within a partition
  • βœ… Scalability through distribution
  • βœ… Durability through replication
  • βœ… Fault tolerance through automatic failover
  • βœ… Parallel processing through partitions and consumer groups

Your Mental Model

Think of Kafka as:

🏭 A highly organized factory where:
   β€’ Multiple assembly lines (partitions) run in parallel
   β€’ Workers (producers) add items to lines
   β€’ Quality checkers (consumers) inspect items
   β€’ Teams (consumer groups) divide the work
   β€’ Multiple facilities (brokers) ensure continuity
   β€’ Everything is tracked and never lost
Enter fullscreen mode Exit fullscreen mode

You now have a complete bird's eye view of Apache Kafka! πŸ¦…

This mental model will be invaluable as you build applications and dive deeper into Kafka's capabilities. Every detail you learn will fit into this bigger picture! 🎯

Top comments (1)

Collapse
 
mateorod_ profile image
Mateo Rodriguez

This post really solidified my understanding of Kafka as a system of cooperating pieces rather than just β€œa message queue.” The highway and factory analogies clarified how partitions, consumer groups, and brokers interact. It reinforced my view of Kafka as log-centric, but shifted my thinking toward treating consumer groups as independent, parallel β€œteams” over the same stream.