๐ฌ Apache Kafka: cheat sheet
๐จ The analogyA giant shared notebook. Some apps write notes in it, and others read them, each at their own speed.
๐ The regular explanationA distributed, append-only log. Producers write streams of events to it, and any number of consumers read them at their own pace.
- A web shop sells a toy. It writes a note, "order 1", and drops it into Kafka's notebook.
- Kafka writes the note on the next empty line. Lines are numbered from 0, and nothing is ever rubbed out.
- More toys sell. Each new note goes on the next line, so the notebook keeps them in order.
- The billing app reads the notebook from the top. Its green bookmark shows which line it will read next.
- It keeps reading until it reaches the end, then waits for new notes.
- Reading a note doesn't remove it. The notes stay, so other apps can read them later too.
- One notebook would get far too busy, so Kafka splits the topic into three notebooks called partitions.
- Every note carries a name tag, called the key. Ana's order is tagged "Ana", and Kafka picks a notebook for it.
- Ben's order has a different name tag, so it might land in a different notebook.
- Ana orders again. Same name tag, so it goes into the same notebook, right after her first one.
- Cat and Ben order too. Each notebook only gets the notes whose name tags belong there.
- There's no promise about order between notebooks. Ben's note might be read before Ana's. Inside one notebook, order is always kept.
- Here are three notebooks that already have orders in them.
- The billing team has two helpers. Kafka shares the notebooks out, and no notebook is read by two billing helpers.
- Both helpers read at the same time, each in its own notebooks, with its own bookmarks.
- Now the shipping team arrives. It reads the very same notes with its own bookmarks. Billing reading a note doesn't hide it from shipping.
- More helpers make billing faster, but only up to three. With four helpers and three notebooks, one would sit with nothing to do.
- The notebook has six orders. The billing app starts at line 0.
- It reads two orders and charges both customers.
- Then it writes its bookmark into Kafka, "I'm done up to line 2", so Kafka remembers it too.
- It reads two more orders, but hasn't written a new bookmark yet.
- Oh no! The app crashes before it can write its bookmark.
- When it restarts, it goes back to the last bookmark it wrote, line 2. So it reads orders 2 and 3 again!
- That's why careful apps check "did I already charge for this order?" before charging again.
- Two helpers share three notebooks. Helper 1 has two, helper 2 has one.
- A new helper joins the team. Time to share the notebooks out again!
- Helper 1 hands one notebook to the new helper. Now everyone has one.
- Helper 2 stops answering. Kafka waits a little, then decides it's gone.
- Its notebook is given to another helper, who carries on from the last bookmark.
- Kafka keeps three copies of every notebook, in three different buildings, in case one has a problem.
- The shop always gives its note to the main copy.
- The other two copies quickly copy the new line.
- Only when every copy has it does Kafka tell the shop "got it!"
- Uh oh, building 1 loses power!
- Copy 2 becomes the main copy. Nothing the shop was told "got it" about is lost.
- The shop keeps going, now writing to the new main copy.
- Ava is the only reader of the orders notebook. She has charged orders 0 and 1 and written her bookmark at line 2.
- Ava reads three more orders and charges all three customers. She has not written a new bookmark yet.
- Ava trips and falls before she can write her bookmark. The charges happened, but Kafka still thinks she stopped at line 2.
- Kafka notices she is gone and hands the notebook to Ben, who has been waiting.
- Ben starts at Ava's last bookmark, line 2, so he charges orders 2, 3 and 4 a second time.
- Ben has learned to check his list of already charged orders first. Then he writes the bookmark at line 5.
- Your Java app is waiting for orders. There are three in the notebook.
- Spring gives your app a helper that keeps asking Kafka "anything new?" Kafka hands it a bundle of notes.
- For every note in the bundle, the helper rings your bell. Your method gets one note at a time.
- When your method has finished all of them, the helper writes the bookmark into Kafka.
- If your method trips over a bad note, Spring tries again a few times, then can put the note aside in a special "problem" notebook.
๐งฉ Key questions
- Two readers read the same notebook. Do they get in each other's way?
- No: each has its own bookmark and reads at its own speed. Nobody ever rubs anything out.
- Why do all notes with Ana's name tag go into the same notebook?
- So all of Ana's notes stay in the right order. Notes are read in order within one notebook.
- A reader trips before writing its bookmark into Kafka. What happens when it gets back up?
- It goes back to its last bookmark and reads a few notes again. A few notes get read twice, so readers should cope with that.
- A note that nobody can read keeps stopping a reader at the same line. What should the reader do?
- Set it aside in a problem-notes notebook and carry on. Setting the note aside keeps the reader moving, and the problem note can be looked at later.
- Every spare copy of the notebook has fallen behind, so only the main one is up to date, and the rule says two copies must have each note. A writer tries to add a note. What happens?
- The note is refused, so nothing is promised that could be lost. Refusing is the price of the promise that an accepted note survives a breakdown.