โ 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.
- Ana and Ben switch on their radios and tune in to the news channel.
- The host reads out the first piece of news. Both radios play it at the same moment.
- Cy switches on his radio just now. The first piece of news has already gone, and there's no recording to replay it.
- The next piece of news goes out, and now all three radios play it.
- 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 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.
- 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.
- Olga reads the whole rail for her tally and remembers the last ticket she saw. All three tickets stay pinned.
- Olga waits by the rail for anything newer.
- A new ticket goes up, and Olga reads it straight away. She didn't have to keep walking back to check.
- The rail has a limit. Once it's full, each new ticket means the oldest ones are taken down to make room.
- Four tickets are pinned. The sandwich team starts with a marker at the first ticket.
- Sam and Tia each take the next ticket nobody on the team has taken. The clipboard notes who is holding which.
- 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.
- The accounting team works from the same rail with its own marker. The sandwich team taking tickets doesn't hide them from Ali.
- Sam takes ticket 1 and Tia takes ticket 2. The clipboard shows both as taken but not finished.
- Tia finishes and stamps ticket 2 done. But Sam feels faint and goes home with ticket 1 still under his name.
- A minute later, Tia checks the clipboard for tickets that sat untouched too long. She claims ticket 1 as her own.
- 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.
- The cashier pins a new ticket on the rail.
- The shift manager keeps checking the rail for the team and picks up the new ticket.
- The manager hands the ticket to Sam, who makes the sandwich.
- 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.