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 Message queues vs event streams

🧥 Message queues vs event streams: cheat sheet

🎨 The analogyA coat-check pile, where each ticket is handled once, versus a guestbook anyone can reread from any page.
🎓 The regular explanationTwo shapes of messaging. A queue hands each message to one of several competing consumers and deletes it once acknowledged; an event stream is a retained, append-only log that any number of consumer groups read at their own offsets and can replay.
A queue hands each job to one workerStep 5 of 5
🧥 Coat-check pile🛎️Coat-check clerk🎟️ Ticket 3torn up👩Attendant Mia👨Attendant Leo
  1. The clerk writes three jobs on tickets and pins them on the coat-check pile, then gets back to the desk.
  2. Mia takes ticket 1 and Leo takes ticket 2 at the same time. Each ticket goes to just one of them, so they don't chase the same coat.
  3. Leo's job is quick. He brings coat 2 back and tears ticket 2 up, while Mia is still looking for coat 1. Job 2 finished before job 1.
  4. Leo takes ticket 3 straight away. Mia finally finds coat 1 and tears ticket 1 up.
  5. Leo finishes job 3 and tears up the last ticket. Every job was done once, by one attendant, and the pile keeps no record of them.
A stream keeps every eventStep 5 of 5
GuestbookAnakey visitor Ana: Ana0Benkey visitor Ben: Ben1Cydkey visitor Cyd: Cyd2Deekey visitor Dee: Dee3Evekey visitor Eve: Eve45caféshopcurator🧑‍🤝‍🧑Visitors☕Café manager🧐New curatorcaught up🎁Gift shop
  1. Four visitors arrive, and each writes one line on the next empty line of the guestbook.
  2. The café manager reads three lines and moves a bookmark. The gift shop reads one line with its own bookmark. Reading takes nothing out of the book.
  3. Another visitor signs. The gift shop catches up at its own speed, and the café hasn't read the new line yet. Neither waits for the other.
  4. A new curator starts work. Instead of only seeing new visitors, the curator opens the guestbook at the very first line.
  5. The curator reads every line, right up to the newest one. Everything that happened is still there to read.
Redelivery and the dead-letter queueStep 6 of 6
🧥 Coat-check pile🗃️ Problem tray1 message🎟️ Smudged ticket👩Attendant Mia👨Attendant Leo🧑‍💼Manager
  1. Mia takes the next ticket. It's off the pile, so nobody else takes the same job, but it isn't torn up yet.
  2. Mia trips and is out of action. Nobody tore the ticket up, so after a few minutes it goes back on the pile. If Mia had already fetched the coat, it gets fetched twice.
  3. Leo takes it, but the number is smudged and the coat can't be found. He puts it back, and the same thing happens again. The desk counts every try.
  4. Three tries is the limit, so the smudged ticket goes into the problem tray, out of everyone's way.
  5. The other jobs keep moving. Leo does ticket 5 and tears it up, and Mia is back on her feet.
  6. Later the manager looks in the problem tray, works out what went wrong and can put the ticket back on the pile.
Order per partition, one reader per partitionStep 4 of 4
☕ Café teamBook A to HA1key Ana: A10A2key Ana: A21234caféBook I to PL1key Lee: L101234caféBook Q to ZS1key Sam: S101234🧑‍🤝‍🧑Visitors👩Reader Cara👧Reader Cleo👨Reader Cole🧑Reader Cruzidle
  1. Ana signs twice, so both her lines go in the A to H book, in the order she wrote them. Lee and Sam sign in their own books.
  2. The café team shares the books out. Cara reads two books and Cole reads one, and each book has just one reader, so its lines are read in order.
  3. Cleo joins the team and takes over the I to P book. Now three readers work at the same time.
  4. Cruz joins too, but there are only three books. With every book already taken, Cruz has nothing to read.
Fan-out with queues needs a copy per consumerStep 5 of 5
new🛎️Front desk📠Copy clerk🧥 Coat-check pile1 waiting☕ Café pile1 waiting🎁 Gift shop pile1 waiting👩Attendant Mia🧑‍🍳Café cook🧑Shop helper
  1. Ana arrives. The front desk hands one note about her to the copy clerk.
  2. The clerk pins a copy on every team's pile, one for the coat-check and one for the café.
  3. Each team takes its own copy, does its job and tears that copy up. Neither one touches the other's pile.
  4. The gift shop starts helping too, so the clerk adds a pile for it. But the pile starts empty. Ana's visit is gone for good.
  5. Ben arrives, and now the clerk pins three copies. Every new team means another pile and another copy of every ticket.
A job for the queue, a fact for the streamStep 5 of 5
🧥 Coat-check pileGuestbookBenkey earlier visitor Ben: Ben0Cydkey earlier visitor Cyd: Cyd1Anakey Ana came in: Ana234cafécurator🛎️Front desk👩Attendant Mia☕Café manager🧐Curatorfixed
  1. Ana arrives. The desk pins a ticket for the job (hang up her coat) and writes the fact (Ana came) in the guestbook, under two earlier visitors.
  2. Mia hangs up the coat once and tears up the ticket. That job is done and nobody needs it again.
  3. The café manager and the curator each read the guestbook to keep their own lists up to date, each with their own bookmark.
  4. The curator finds the tally was wrong, so moves the bookmark back to the very first line to count again.
  5. The curator reads every line again and the tally is right this time. Only the guestbook could do that; the torn-up tickets are gone.

🧩 Key questions

Mia and Leo both work the coat-check pile. Mia takes a ticket. Can Leo get the same ticket while she has it?
No, unless Mia never finishes it; then it goes back on the pile for someone else. Each ticket goes to one attendant, but it's only torn up once the job is done, so a dropped job comes back.
A new curator starts next month and wants to know about every visitor so far. Which helps?
Opening the guestbook at the first page. Torn-up tickets are gone, and a new pile only gets new tickets. The guestbook kept every line.
The café team has four readers and the museum has three guestbooks. What does the fourth reader do?
Nothing: every book already has its one reader from the team. Each book has one reader per team, so its lines are read in order. More readers than books don't help.
Hanging up Ana's coat is a job for one attendant, and the café wants to know who came today. Where does each go?
The coat job on the pile, the visit in the guestbook. A job is done once and forgotten; a thing that happened is kept for anyone to read.
A guestbook reader goes on holiday for a month. The museum removes pages older than a week. What happens when the reader returns?
The lines on the removed pages are gone unread, and the reader carries on from whatever pages are left. Old pages are taken out on a schedule, whether or not everyone has read them.