DEV Community

Cover image for Understanding Kafka Complete Architecture Flow - Advanced🎯
Ajinkya Singh
Ajinkya Singh

Posted on • Edited on

Understanding Kafka Complete Architecture Flow - Advanced🎯

Please read the last two articles in my Kafka series before this one β€” this part gets serious.


🌐 The Big Picture: Two Brains Working Together

Think of Kafka as a well-organized company with two main components:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚           KAFKA CLUSTER                     β”‚
β”‚                                             β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”‚
β”‚  β”‚  CONTROLLERS (The Brain 🧠)        β”‚    β”‚
β”‚  β”‚  β€’ Manage who does what            β”‚    β”‚
β”‚  β”‚  β€’ Track what's happening          β”‚    β”‚
β”‚  β”‚  β€’ Make decisions                  β”‚    β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚
β”‚                 ↓ Commands & Updates       β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”‚
β”‚  β”‚  BROKERS (The Workers πŸ’ͺ)          β”‚    β”‚
β”‚  β”‚  β€’ Store the actual data           β”‚    β”‚
β”‚  β”‚  β€’ Serve producers and consumers   β”‚    β”‚
β”‚  β”‚  β€’ Follow controller's instructionsβ”‚    β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Simple Analogy: Controllers are like managers who plan and coordinate, while Brokers are employees who do the actual work.


πŸš€ Part 1: SETUP - Controllers Organize Everything

Step 1: Controllers Start and Elect Leader

When Kafka starts, multiple controllers use the Raft Election algorithm to choose one leader:

Controller-1    Controller-2    Controller-3
     β”‚               β”‚               β”‚
     └───────Raft Electionβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                     ↓
          One becomes LEADER ⭐
                     β”‚
            β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
            β”‚ Controller-1   β”‚
            β”‚   (LEADER) ⭐   β”‚ ← Makes all decisions!
            β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Simple: Like choosing a class monitor who manages everything.


Step 2: Controllers Create Metadata Registry

The Controller Leader creates a comprehensive "notebook" πŸ“’ of everything happening in the cluster:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚        METADATA REGISTRY                 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                          β”‚
β”‚ TOPICS:                                  β”‚
β”‚  β€’ "orders" β†’ 3 partitions               β”‚
β”‚  β€’ "payments" β†’ 2 partitions             β”‚
β”‚                                          β”‚
β”‚ BROKERS:                                 β”‚
β”‚  β€’ Broker-1 β†’ Alive βœ… (IP: 192.168.1.10)β”‚
β”‚  β€’ Broker-2 β†’ Alive βœ… (IP: 192.168.1.11)β”‚
β”‚  β€’ Broker-3 β†’ Alive βœ… (IP: 192.168.1.12)β”‚
β”‚                                          β”‚
β”‚ WHO'S IN CHARGE (Leaders):               β”‚
β”‚  β€’ orders-partition-0 β†’ Broker-1         β”‚
β”‚  β€’ orders-partition-1 β†’ Broker-2         β”‚
β”‚  β€’ orders-partition-2 β†’ Broker-3         β”‚
β”‚                                          β”‚
β”‚ BACKUPS (Followers):                     β”‚
β”‚  β€’ orders-partition-0 β†’ Broker-2, Broker-3β”‚
β”‚  β€’ orders-partition-1 β†’ Broker-3, Broker-1β”‚
β”‚  β€’ orders-partition-2 β†’ Broker-1, Broker-2β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Simple: Like a school register showing which classes exist, which teachers are present, who teaches which subject, and who are the substitute teachers.


Step 3: Controller Tells Brokers Their Jobs

The controller assigns specific roles to each broker:

Controller ⭐ β†’ Broker-1: "You are the LEADER for orders-partition-0"
                         "You are a BACKUP for orders-partition-2"

Controller ⭐ β†’ Broker-2: "You are the LEADER for orders-partition-1"
                         "You are a BACKUP for orders-partition-0"

Controller ⭐ β†’ Broker-3: "You are the LEADER for orders-partition-2"
                         "You are a BACKUP for orders-partition-1"
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Simple: Like a manager assigning tasks to employees.


πŸ“€ Part 2: PRODUCER SENDS DATA

Step 4: Producer Wants to Send Message

Your application (Producer) has data to send:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  I have a message:       β”‚
β”‚  β€’ Topic: "orders"       β”‚
β”‚  β€’ Key: "user_123"       β”‚
β”‚  β€’ Value: {order data}   β”‚
β”‚                          β”‚
β”‚  "Where do I send this?" β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Simple: Like having a letter but not knowing which post office to use.


Step 5: Producer Asks ANY Broker for Information

The producer can connect to any broker to get routing information:

Producer ──────────► Broker-1
"Where do I send     "Let me check
messages for         my copy of
'orders' topic?"     the registry..."
Enter fullscreen mode Exit fullscreen mode

Key Point: Every broker has a copy of the controller's metadata!

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Broker-1's Copy of Metadata:     β”‚
β”‚  β€’ orders-partition-0 β†’ Broker-1  β”‚
β”‚  β€’ orders-partition-1 β†’ Broker-2  β”‚
β”‚  β€’ orders-partition-2 β†’ Broker-3  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Simple: Like asking a postman for directions - he has a map (copy of registry).


Step 6: Producer Calculates Which Partition

The producer's client library automatically determines the target partition:

1. Hash the key: hash("user_123") = 456789
2. Divide by partitions: 456789 % 3 = 0
3. Result: Goes to Partition 0
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Simple: Like a sorting machine that knows exactly which box to put each item in.


Step 7: Producer Sends to Correct Broker

Now the producer sends directly to the partition leader:

Producer ───────────► Broker-1 (Leader for P0)

Broker-1 receives:
1. βœ… Validates it's the leader for P0
2. πŸ’Ύ Writes to disk: /kafka/data/orders-0/
3. πŸ“ Assigns offset: 1251
4. βœ… Sends "OK!" back to producer
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Simple: Like mailing a letter to the correct post office that handles your area.


Step 8: Broker Replicates to Followers (Background)

After writing, the leader broker automatically replicates to followers:

Broker-1 (Leader P0) ───COPY MESSAGE───► Broker-2 (Follower P0)
                                          βœ… Copied!
         │◄──────────ACK (Copied!)────────

Broker-1 (Leader P0) ───COPY MESSAGE───► Broker-3 (Follower P0)
                                          βœ… Copied!
Enter fullscreen mode Exit fullscreen mode

Result: Data now exists on 3 brokers! πŸ’ͺ

πŸ’‘ Simple: Like making photocopies of important documents and storing them in different safes.


πŸ“₯ Part 3: CONSUMER READS DATA

Step 9: Consumer Wants to Read Messages

Your application (Consumer) wants to read data:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  I want to read from:    β”‚
β”‚  β€’ Topic: "orders"       β”‚
β”‚  β€’ Group: "my-group"     β”‚
β”‚                          β”‚
β”‚  "How do I start?"       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Simple: Like wanting to read a book but not knowing which library has it.


Step 10: Consumer Connects and Gets Metadata

The consumer connects to any broker to get cluster information:

Consumer ────────────► Broker-2
"Tell me everything    "Here's the full
about 'orders'"        cluster map!"
Enter fullscreen mode Exit fullscreen mode

Broker returns:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Topic "orders" info:             β”‚
β”‚  β€’ Partition 0 β†’ Leader: Broker-1 β”‚
β”‚  β€’ Partition 1 β†’ Leader: Broker-2 β”‚
β”‚  β€’ Partition 2 β†’ Leader: Broker-3 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Simple: Like getting a mall directory showing which stores are on which floor.


Step 11: Consumer Joins Group (Controller Coordinates)

The consumer joins a consumer group for coordinated reading:

Consumer β†’ Broker β†’ Controller ⭐
"I want to join      "Let me assign
group 'my-group'"    partitions..."

Controller decides:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Group "my-group" has 1 consumer  β”‚
β”‚  Topic "orders" has 3 partitions  β”‚
β”‚                                   β”‚
β”‚  Assignment:                      β”‚
β”‚  Consumer-A β†’ [P0, P1, P2]        β”‚
β”‚  (gets all 3 partitions)          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Simple: Like a teacher assigning homework to students.


Step 12: Consumer Reads and Tracks Progress

How Consumer Manages Reading Position:

STARTUP (Once):
Consumer β†’ Broker: "Where did I leave off?"
Broker β†’ Consumer: "P0: offset 1250, P1: offset 890, P2: offset 2100"
Consumer stores in memory βœ…

CONTINUOUS READING:
Consumer fetches using local memory (no broker queries!)
β€’ Fetch from P0: offset 1250 β†’ 1300 β†’ 1350 (tracked in memory)
β€’ Fetch from P1: offset 890 β†’ 940 β†’ 990 (tracked in memory)
β€’ Fetch from P2: offset 2100 β†’ 2150 β†’ 2200 (tracked in memory)

PERIODIC COMMIT (Every 5 seconds or after batch):
Consumer β†’ Broker: "Save progress: P0=1350, P1=990, P2=2200"
Enter fullscreen mode Exit fullscreen mode

πŸ”₯ KEY POINTS:

  • Consumer reads position from broker ONCE at startup
  • Tracks current position IN MEMORY while reading
  • Saves progress back to broker PERIODICALLY (every 5 seconds by default)

πŸ’‘ Simple: Like checking your bookmark when opening a book, remembering your page while reading, and updating the bookmark occasionally.


Step 13: Consumer Pulls Data from Brokers

Consumer fetches data in parallel from all partition leaders:

Consumer ───────► Broker-1 (P0) β†’ Returns 50 events
        β”œβ”€β”€β”€β”€β”€β”€β”€β”€β–Ί Broker-2 (P1) β†’ Returns 50 events
        └────────► Broker-3 (P2) β†’ Returns 50 events

Total: 150 events fetched!
         ↓
   Process all events
   (Your business logic)
         ↓
   All done! βœ…
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Simple: Like reading from multiple books at the same time, keeping track of your progress in each.


πŸ”§ Part 4: FAILURE HANDLING

SCENARIO A: Broker Fails (Controller Handles)

Broker-1 crashes! πŸ’₯

STEP 1: Controller Detects Failure
Controller ⭐: "Broker-1 stopped responding!"

STEP 2: Controller Checks Metadata
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Partition 0 (orders):            β”‚
β”‚  β€’ Leader: Broker-1 πŸ’€            β”‚
β”‚  β€’ Followers: Broker-2 βœ…, Broker-3 βœ…β”‚
β”‚                                   β”‚
β”‚  Need new leader for P0!          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

STEP 3: Controller Elects New Leader
Controller ⭐ β†’ Broker-2: "You are now LEADER for P0!"
Broker-2: "OK! I'm the new leader!"

STEP 4: Controller Updates Metadata
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Partition 0 (orders):            β”‚
β”‚  β€’ Leader: Broker-2 ⭐ (NEW!)     β”‚
β”‚  β€’ Followers: Broker-3 βœ…         β”‚
β”‚  β€’ Broker-1: πŸ’€ (removed)         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

STEP 5: Controller Notifies Everyone
Controller β†’ All: "Metadata changed! P0 leader is now Broker-2!"

Total time: 2-3 seconds! ⚑
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Simple: Like when a teacher is absent, the principal quickly assigns a substitute.


SCENARIO B: Consumer Fails (Controller Rebalances)

Consumer-A crashes! πŸ’₯

STEP 1: Group Coordinator Detects
Coordinator: "Consumer-A missed 3 heartbeats!"

STEP 2: Coordinator Triggers Rebalance
Coordinator β†’ Controller: "Group 'my-group' needs rebalancing!"
Controller β†’ Other Consumers: "Stop! Rebalancing..."

STEP 3: Controller Reassigns Partitions
Before:
Consumer-A: [P0, P1] πŸ’€ (dead)
Consumer-B: [P2] βœ…

After:
Consumer-B: [P0, P1, P2] βœ… (takes over all!)

STEP 4: Controller Notifies Consumer-B
Controller β†’ Consumer-B: "You now read P0, P1, P2"

Consumer-B resumes reading:
βœ… Loads last saved positions
βœ… Continues from where Consumer-A left off
βœ… No messages lost!
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Simple: Like when one waiter is sick, another takes over their tables.


🎨 The Complete Picture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                   KAFKA CLUSTER                        β”‚
β”‚                                                        β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”‚
β”‚  β”‚     CONTROLLERS (The Managers 🧠)             β”‚    β”‚
β”‚  β”‚                                               β”‚    β”‚
β”‚  β”‚  Controller-1 ⭐  Controller-2  Controller-3  β”‚    β”‚
β”‚  β”‚    (Leader)      (Follower)    (Follower)    β”‚    β”‚
β”‚  β”‚                                               β”‚    β”‚
β”‚  β”‚  Maintains METADATA REGISTRY                  β”‚    β”‚
β”‚  β”‚  β€’ Who's alive?                               β”‚    β”‚
β”‚  β”‚  β€’ Who's the leader?                          β”‚    β”‚
β”‚  β”‚  β€’ Who reads what?                            β”‚    β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚
β”‚                     ↓ Commands & Notifications        β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”‚
β”‚  β”‚     BROKERS (The Workers πŸ’ͺ)                  β”‚    β”‚
β”‚  β”‚                                               β”‚    β”‚
β”‚  β”‚  Broker-1      Broker-2      Broker-3        β”‚    β”‚
β”‚  β”‚  P0 (L)⭐      P1 (L)⭐      P2 (L)⭐         β”‚    β”‚
β”‚  β”‚  P1 (F)        P2 (F)        P0 (F)          β”‚    β”‚
β”‚  β”‚  P2 (F)        P0 (F)        P1 (F)          β”‚    β”‚
β”‚  β”‚                                               β”‚    β”‚
β”‚  β”‚  Stores & Serves Data                         β”‚    β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚
β”‚         ↑              ↑              ↑               β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
          β”‚              β”‚              β”‚
          ↓              ↓              ↓
    Producer-1     Producer-2     Producer-3
          β”‚              β”‚              β”‚
          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                         β”‚
         All query metadata from any broker
                         β”‚
          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
          ↓                             ↓
    Consumer-A                    Consumer-B
    Group: g1                     Group: g1
    Reads: P0,P1                  Reads: P2
Enter fullscreen mode Exit fullscreen mode

πŸ“‹ Key Roles Summary

Controller (The Boss πŸ‘”)

MANAGES:

  • βœ… Who's alive? (broker health)
  • βœ… Who's in charge? (partition leaders)
  • βœ… Who reads what? (consumer assignments)
  • βœ… What exists? (topics, partitions)

DECIDES:

  • βœ… New leader when broker fails
  • βœ… Partition assignments for consumers
  • βœ… Where new partitions go

NOTIFIES:

  • βœ… Tells brokers their jobs
  • βœ… Updates everyone on changes
  • βœ… Coordinates rebalancing

Broker (The Worker πŸ‘·)

STORES:

  • βœ… Actual data on disk
  • βœ… Log files for partitions
  • βœ… Copy of metadata (from controller)

SERVES:

  • βœ… Producer write requests
  • βœ… Consumer read requests
  • βœ… Metadata queries

REPLICATES:

  • βœ… Copies data to followers
  • βœ… Syncs with leader
  • βœ… Reports status to controller

Producer (The Sender πŸ“€)

DOES:

  • βœ… Creates messages
  • βœ… Queries metadata (from any broker)
  • βœ… Calculates partition (key hash)
  • βœ… Sends to correct broker

DOESN'T CARE ABOUT:

  • ❌ Controllers (transparent)
  • ❌ Followers (writes only to leader)
  • ❌ Other producers

Consumer (The Receiver πŸ“₯)

DOES:

  • βœ… Joins consumer group
  • βœ… Gets partition assignment
  • βœ… Tracks reading position in memory
  • βœ… Pulls from leader brokers
  • βœ… Saves progress periodically

DOESN'T CARE ABOUT:

  • ❌ How controller assigns partitions
  • ❌ Follower replicas
  • ❌ Other consumer groups

🎯 The Magic: Why This Works So Well

1. Separation of Concerns

  • CONTROLLERS think 🧠
  • BROKERS work πŸ’ͺ
  • Controllers don't handle data
  • Brokers don't make decisions
  • Like: Managers plan, workers execute

2. Everything Has a Backup

  • Controllers: 3 copies (1 leader + 2 followers)
  • Partitions: 3 copies (1 leader + 2 followers)
  • Metadata: All brokers have a copy
  • Result: If anything fails, backups take over!

3. Distributed = Fast + Reliable

  • Multiple brokers = Parallel processing
  • Multiple partitions = Load distribution
  • Multiple replicas = No data loss
  • Like: Many checkout lanes at a store

4. Automatic Recovery

  • Broker fails β†’ Controller elects new leader (seconds)
  • Consumer fails β†’ Controller reassigns partitions (seconds)
  • Controller fails β†’ Another controller becomes leader (seconds)
  • All automatic! No human intervention needed!

I also took some help from Claude to understand and make a few concepts more visual.

Top comments (1)

Collapse
 
jjbb profile image
Jason Burkes

This breakdown of Kafka's roles oddly reminds me of how a film set runsβ€”directors, assistant directors, crew, and backup plans for every shot so the production never really stops even if something goes wrong.