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 Redis Streams and pub/sub

๐Ÿ“ป Redis Streams and pub/sub: cheat sheet

๐ŸŽจ The analogyA radio show that's gone once aired, next to a deli ticket rail where tickets stay until someone serves them.
๐ŸŽ“ The regular explanationTwo messaging tools inside Redis and Valkey. Pub/sub pushes each message to whoever is subscribed right now and keeps nothing; Streams are an append-only log where consumer groups share entries and acknowledge them, giving at-least-once delivery.
Pub/sub reaches whoever is listening nowStep 5 of 5
๐ŸŽ™๏ธRadio hostreached 3๐Ÿ“กRadio station๐Ÿ‘ฉListener Anaโœ• โœ•๐Ÿ’ซListener Bencut off๐Ÿง‘Listener Cy
  1. Ana and Ben switch on their radios and tune in to the news channel.
  2. The host reads out the first piece of news. Both radios play it at the same moment.
  3. Cy switches on his radio just now. The first piece of news has already gone, and there's no recording to replay it.
  4. The next piece of news goes out, and now all three radios play it.
  5. Ben's old radio is too slow, and unheard news piles up for it. The station cuts Ben off so the pile can't jam the station. Whatever he misses now is gone.
A stream keeps every entry, in orderStep 6 of 6
Ticket rail ยท cap about 10000o1key 1718000000000-0: o10o2key 1718000000000-1: o21o3key 1718000000005-0: o32o4key 1718000000020-0: o43o5key 1718000000031-0: o5โœฆโœฆโœฆโœฆโœฆ45olga๐Ÿง‘โ€๐Ÿ’ผCashier Dee๐Ÿ‘ตOwner Olga
  1. A customer orders a ham sandwich. Cashier Dee writes a ticket and pins it at the end of the rail. The clock stamps it with a number.
  2. Two more orders come in. One is pinned in the very same moment as the first, so it gets the same time plus an extra number.
  3. Olga reads the whole rail for her tally and remembers the last ticket she saw. All three tickets stay pinned.
  4. Olga waits by the rail for anything newer.
  5. A new ticket goes up, and Olga reads it straight away. She didn't have to keep walking back to check.
  6. The rail has a limit. Once it's full, each new ticket means the oldest ones are taken down to make room.
A consumer group shares the entriesStep 4 of 4
๐Ÿฅช Sandwich team๐Ÿ“’ Accounting teamTicket railo1o10o2o21o3o32o4o4345sandwichesaccounting๐Ÿง‘โ€๐ŸณServer Samholding o3๐Ÿ‘ฉโ€๐ŸณServer Tiaholding o2๐ŸงฎAccountant Aliholding o1
  1. Four tickets are pinned. The sandwich team starts with a marker at the first ticket.
  2. Sam and Tia each take the next ticket nobody on the team has taken. The clipboard notes who is holding which.
  3. Sam hands over his sandwich and stamps ticket 1 done, so it comes off the clipboard. The ticket stays on the rail. Then he takes ticket 3.
  4. The accounting team works from the same rail with its own marker. The sandwich team taking tickets doesn't hide them from Ali.
Claiming a stuck entryStep 4 of 4
๐Ÿฅช Sandwich teamTicket railo1o10o2o212345sandwichesโœ• โœ•๐Ÿ’ซServer Sam๐Ÿ‘ฉโ€๐ŸณServer Tia
  1. Sam takes ticket 1 and Tia takes ticket 2. The clipboard shows both as taken but not finished.
  2. Tia finishes and stamps ticket 2 done. But Sam feels faint and goes home with ticket 1 still under his name.
  3. A minute later, Tia checks the clipboard for tickets that sat untouched too long. She claims ticket 1 as her own.
  4. Tia checks the counter first, in case Sam already made order 1. It isn't there, so she makes it and stamps the ticket done.
A Spring stream listenerStep 4 of 4
๐Ÿฅช Sandwich teamTicket railo1o1012345sandwiches๐Ÿง‘โ€๐Ÿ’ผCashier Dee๐Ÿ“‹Shift manager๐Ÿง‘โ€๐ŸณServer Sam
  1. The cashier pins a new ticket on the rail.
  2. The shift manager keeps checking the rail for the team and picks up the new ticket.
  3. The manager hands the ticket to Sam, who makes the sandwich.
  4. When the sandwich is made, Sam stamps the ticket done, and it comes off the clipboard.

๐Ÿงฉ Key questions

Cy's radio was off when the host read out the news. He switches on a minute later. What does he hear?
Only news read out from now on. The radio keeps no recording. If you weren't tuned in, it's gone.
Sam takes ticket 3 and goes home sick without stamping it done. What happens to ticket 3?
It stays on the clipboard under Sam's name until someone claims it. Nobody takes over a ticket on their own. Another server has to check the clipboard and claim it.
Why should a server check the counter before making a claimed order?
The first server might have made it but not stamped it, so it could be made twice. A ticket may be handled more than once, but it is never just forgotten.
The sandwich team and the accounting team both use the rail. Sam takes ticket 1 and stamps it done. Does accounting still see it?
Yes: each team has its own marker and its own clipboard. Taking or stamping a ticket never removes it from the rail.
The rail is capped, and the oldest tickets are taken down. The sandwich team is far behind and hasn't reached ticket 5, which is now taken down. What happens to that order?
The ticket is gone and the team never makes the order. The rail only cares about its length, not about who has read what. A rail that is long enough for the slowest team avoids this.