Skip to content
derpx06.dev
WritingExplainersNotesAbout
works on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisationworks on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisation

Learning in public.

A student's notebook on system design, distributed systems and databases. Shared as I learn.

ReadWritingExplainersNotesAbout
ElsewhereGitHubLinkedInHugging FaceLeetCode
Say hellomanaskhairnar1511@gmail.com
derpx06
NORMALmain© 2026 Derpx06Pune, Indiatop ↑
Blog/System Design

What is Kafka? Why it keeps messages after you read them

Queues let services hand off work and walk away. Kafka goes further with an append-only log. Here is how it scales, step by step.

Derpx0627 Sept · 5 min read
Contents
What goes wrong with no queue?How does a queue fix it?Why does a normal queue forget?What's a log, then?Isn't writing to disk slow?How does one log become many?What if the readers are too slow?Can you undo a bad run? ReplayWhat if a machine dies?Design it yourselfSources
0% read · 5 min leftBack to top ↑
On this page0%
Contents
What goes wrong with no queue?How does a queue fix it?Why does a normal queue forget?What's a log, then?Isn't writing to disk slow?How does one log become many?What if the readers are too slow?Can you undo a bad run? ReplayWhat if a machine dies?Design it yourselfSources

When you order food in an app, it doesn't make you wait until the meal is cooked. It shows "order placed" and lets you go. The kitchen picks up the order when it is ready.

That small gap between "placed" and "cooked" is a message queue. It is how big systems keep working when everyone shows up at once. Today's questions: how does it work under the hood, and why do people store messages in a plain file that only grows at the end?

The one idea to keep

A queue lets one part of a system hand off work and walk away. A log goes further. It keeps every message in order, so readers can come back whenever they want.

What goes wrong with no queue?

You build a tiny shop site. When someone pays, your code saves the order, sends an email and updates stock. All in one go.

It works until the email service gets slow. Now every payment has to wait for it. One slow step holds up every step after it.

Hit play and watch the packets. Every third one gets lost at the email link, and the whole chain feels it.

Payment
Email service
×
Stock
Billing
A toy chain: each step waits for the next, so one bad link hurts everyone. Not real measurements.

Notice that the payment can't finish until everything after it does. So how do we break the chain?

How does a queue fix it?

The payment code writes a message ("order 42 paid") and finishes. Email, stock and billing each read it when they are ready. The sender doesn't even know who is listening.

TangledPayment waits for email, stock and billing, one by one.
→
Queue in the middlePayment drops one message. The others pick it up when ready.

If email is down for an hour, the messages just wait. They are like letters in a mailbox, waiting to be picked up. Sounds perfect, so what's the catch?

Why does a normal queue forget?

In a classic queue, a message is removed once someone reads it. That sounds tidy. But what if the reader had a bug and handled 1,000 messages wrongly? The messages are gone.

Kafka does something different. Its docs say events are not removed after being read. A setting for each topic decides how long they are kept.

What's a log, then?

Just keep writing at the end. A log is a list of records that only grows at the end, in time order. Jay Kreps (who helped build Kafka at LinkedIn) calls it "perhaps the simplest possible storage abstraction". New things go on the end. Nothing gets edited.

1Message 0
2Message 1
3Message 2
4Message 3
5(next goes here)
Each message gets a number: its offset.

The offset is just that number, a position in the line. It works like a bookmark that shows where you stopped reading.

Isn't writing to disk slow?

I assumed "disk" meant slow. Kafka's design docs say it's more nuanced. On one setup they describe, writing in a straight line hit about 600MB/sec. Writing to random places got only about 100kB/sec. That's a gap of over 6000X.

Reading and writing in a straight line is the fast way. A log that only grows at the end does only that. So the plain design is the fast one.

Jumping around Like looking for one book by walking between ten different rooms.

Straight line Like reading one book from front to back.

Fast, but one log still lives on one machine. What then?

How does one log become many?

Chop it into partitions. These are separate logs spread across machines.

Per the Kafka docs, events with the same key (say, a customer ID) go to the same partition. A consumer reads a partition's events in exactly the order they were written. Kreps adds a catch: there's no global ordering between partitions.

So the rule of thumb: if two events must stay in order, give them the same key.

What if the readers are too slow?

Say email is slow because 1,000,000 messages piled up. You add more email workers. A consumer group is that team of workers sharing one job.

The docs say each partition is consumed by exactly one consumer within each subscribing group at a time. So the workers split the partitions between them. A different group (say, billing) gets its own full copy of the stream.

Consumer position

For each partition, a consumer's position is just one number: the offset of the next message to read. Kafka's docs point out this makes tracking "what's been consumed" very cheap.

Can you undo a bad run? Replay

Messages stay, and your position is just a number. So you can set it back. The docs say a consumer can "rewind back to an old offset and re-consume data". They give an example: find a bug after processing, fix it, then run the old messages again.

This breaks the usual rule of a queue. The docs say it turns out to be essential for many consumers. It is like rewinding a recording to hear a part again.

Click Next and follow a replay from the bug to the fix.

Log
Consumer
Offset

The consumer reads messages and finds a bug

Step 1 of 4
Replay works because reading doesn't delete anything.

Did you see it? The log never changed. Only one number moved.

What if a machine dies?

One machine can die. So topics are replicated: copies live on several machines. The docs say a common production setting is a replication factor of 3.

Design it yourself

  1. Which of your side projects has one slow step that blocks everything? What message would you drop in a queue?
  2. Two events must stay in order (a "created" and a "deleted"). What do you use as the key?
  3. A bug corrupted yesterday's results. What would you move, and to where?
Key takeaways
  • A queue decouples services: the sender drops a message and walks away.
  • Kafka keeps messages in a log that only grows at the end, so reading doesn't delete anything.
  • Partitions spread the log across machines; same key means same partition and same order.
  • A consumer's place is just an offset, so replay means moving one number back.

Sources

  • Apache Kafka: Introduction
  • Apache Kafka: Design
  • Jay Kreps, The Log: What every software engineer should know about real-time data's unifying abstraction, LinkedIn Engineering, 2013 (read via a web archive copy)
ShareXLinkedInEmail
Written by Derpx06A developer who builds AI search tools and the code around them. I explain what I got wrong first, so you can skip it. Spotted a mistake? Tell me — corrections get credited.
System Design · 4 minHow does video streaming work? (Why movies play on slow Wi-Fi)System Design · 5 minHow does a search engine find one page out of billions so fast?System Design · 5 minHow to scale a website from 1 user to 1 million users
Start here
01Why is 0.1 + 0.2 not 0.3 in code?02How does Git work inside? (Snapshots, not changes)03How does your computer know the time? (And why it can be wrong)