TwiceToldby Develoker
ConceptsSystem designLabsCheat sheetsAbout

Develoker makes free tools for learning to build software. TwiceTold explains each idea twice, as an everyday analogy and the real thing; CloudArch lets you design cloud systems and put load on them.

About ยท Labs ยท Cheat sheets ยท Learning paths

โ† Back to Apache Kafka

๐Ÿ“ฌ 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.
The big pictureStep 6 of 6
๐Ÿซ School hallwayThe orders notebooko1o10o2o21o3o3234567billing๐ŸชToy shop๐ŸงฎBilling reader
  1. A web shop sells a toy. It writes a note, "order 1", and drops it into Kafka's notebook.
  2. Kafka writes the note on the next empty line. Lines are numbered from 0, and nothing is ever rubbed out.
  3. More toys sell. Each new note goes on the next line, so the notebook keeps them in order.
  4. The billing app reads the notebook from the top. Its green bookmark shows which line it will read next.
  5. It keeps reading until it reaches the end, then waits for new notes.
  6. Reading a note doesn't remove it. The notes stay, so other apps can read them later too.
Partitions and keysStep 6 of 6
๐Ÿ“š Three notebooks for ordersNotebook 0B1key Ben: B10B2key Ben: B212345Notebook 1A1key Ana: A10A2key Ana: A212345Notebook 2C1key Cat: C1012345๐ŸชToy shop
  1. One notebook would get far too busy, so Kafka splits the topic into three notebooks called partitions.
  2. Every note carries a name tag, called the key. Ana's order is tagged "Ana", and Kafka picks a notebook for it.
  3. Ben's order has a different name tag, so it might land in a different notebook.
  4. Ana orders again. Same name tag, so it goes into the same notebook, right after her first one.
  5. Cat and Ben order too. Each notebook only gets the notes whose name tags belong there.
  6. There's no promise about order between notebooks. Ben's note might be read before Ana's. Inside one notebook, order is always kept.
Consumer groups share the workStep 5 of 5
๐Ÿ’ฐ Billing team๐Ÿ“ฆ Shipping teamNotebook 0B1B10B2B212345billingNotebook 1A1A10A2A21A3A32345shippingNotebook 2C1C1012345billing๐Ÿ‘งHelper Ava๐Ÿ‘ฆHelper Ben๐ŸššShipping helper
  1. Here are three notebooks that already have orders in them.
  2. The billing team has two helpers. Kafka shares the notebooks out, and no notebook is read by two billing helpers.
  3. Both helpers read at the same time, each in its own notebooks, with its own bookmarks.
  4. 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.
  5. More helpers make billing faster, but only up to three. With four helpers and three notebooks, one would sit with nothing to do.
Offsets and commitsStep 7 of 7
๐Ÿซ School hallwayOrders notebooko0o00o1o11o2o22o3o33o4o44o5o55committedreading๐Ÿง’Billing reader
  1. The notebook has six orders. The billing app starts at line 0.
  2. It reads two orders and charges both customers.
  3. Then it writes its bookmark into Kafka, "I'm done up to line 2", so Kafka remembers it too.
  4. It reads two more orders, but hasn't written a new bookmark yet.
  5. Oh no! The app crashes before it can write its bookmark.
  6. When it restarts, it goes back to the last bookmark it wrote, line 2. So it reads orders 2 and 3 again!
  7. That's why careful apps check "did I already charge for this order?" before charging again.
RebalancingStep 5 of 5
Notebook 0aa01234Notebook 1bb01234Notebook 2cc01234๐Ÿ‘งHelper Avaโœ• โœ•๐Ÿ’ซHelper Ben๐Ÿง‘New helper Cy
  1. Two helpers share three notebooks. Helper 1 has two, helper 2 has one.
  2. A new helper joins the team. Time to share the notebooks out again!
  3. Helper 1 hands one notebook to the new helper. Now everyone has one.
  4. Helper 2 stops answering. Kafka waits a little, then decides it's gone.
  5. Its notebook is given to another helper, who carries on from the last bookmark.
Replication keeps data safeStep 7 of 7
๐Ÿ  Building 1 ๐Ÿ’ฅ๐Ÿก Building 2leader๐Ÿ˜๏ธ Building 3followerMain notebooko1o101234Copy notebooko1o10o2o2โœฆโœฆโœฆโœฆโœฆ1234Copy notebooko1o10o2o2โœฆโœฆโœฆโœฆโœฆ1234๐ŸชToy shop
  1. Kafka keeps three copies of every notebook, in three different buildings, in case one has a problem.
  2. The shop always gives its note to the main copy.
  3. The other two copies quickly copy the new line.
  4. Only when every copy has it does Kafka tell the shop "got it!"
  5. Uh oh, building 1 loses power!
  6. Copy 2 becomes the main copy. Nothing the shop was told "got it" about is lost.
  7. The shop keeps going, now writing to the new main copy.
A crash before the commitStep 6 of 6
Orders notebooko0o00o1o11o2o22o3o33o4o445committedreadingโœ• โœ•๐Ÿ’ซReader Avacharged 3 orders๐Ÿ‘ฆReader Benskipped repeats
  1. Ava is the only reader of the orders notebook. She has charged orders 0 and 1 and written her bookmark at line 2.
  2. Ava reads three more orders and charges all three customers. She has not written a new bookmark yet.
  3. Ava trips and falls before she can write her bookmark. The charges happened, but Kafka still thinks she stopped at line 2.
  4. Kafka notices she is gone and hands the notebook to Ben, who has been waiting.
  5. Ben starts at Ava's last bookmark, line 2, so he charges orders 2, 3 and 4 a second time.
  6. Ben has learned to check his list of already charged orders first. Then he writes the bookmark at line 5.
@KafkaListener in a Spring appStep 5 of 5
โ˜• Your Java appOrders notebooko0o00o1o11o2o22345app๐Ÿ™‹Helper who askscommitted 3๐Ÿ””Your bell
  1. Your Java app is waiting for orders. There are three in the notebook.
  2. Spring gives your app a helper that keeps asking Kafka "anything new?" Kafka hands it a bundle of notes.
  3. For every note in the bundle, the helper rings your bell. Your method gets one note at a time.
  4. When your method has finished all of them, the helper writes the bookmark into Kafka.
  5. 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.