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.
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?
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.
Notice that the payment can't finish until everything after it does. So how do we break the chain?
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.
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?
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.
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.
The offset is just that number, a position in the line. It works like a bookmark that shows where you stopped reading.
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?
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.
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.
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.
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.
The consumer reads messages and finds a bug
Did you see it? The log never changed. Only one number moved.
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.