โ Back to Amazon SQS and SNS
๐งบ Amazon SQS and SNS: cheat sheet
๐จ The analogyA laundrette: baskets wait on a rack until an attendant finishes one, and a copier slips every rack a copy.
๐ The regular explanationAWS's managed messaging pair. SQS is a queue that consumers poll, where each message is processed and then deleted; SNS is a pub/sub topic that pushes a copy of every message to each subscriber, and the two combine into fan-out.
- The laundrette's front desk puts a customer's laundry in basket 1, sets it on the rack and goes straight back to work. The basket waits there.
- Attendant Kim comes to the rack and asks for work. The desk lets Kim take basket 1, but keeps it on the books until Kim ticks it off.
- Kim finishes the wash and ticks basket 1 off. Only now does the laundrette drop it from the books.
- The rack is empty. Instead of walking off and coming back over and over, Kim stands by the rack for a while.
- A new basket arrives while Kim is waiting, and the desk lets Kim take it straight away.
- Kim takes basket 1. It goes into a machine with a half-minute timer.
- Lee asks for work too. Basket 1 is in the machine, so Lee isn't given it.
- Oh no! Kim trips and never comes back to tick the basket off. The timer keeps ticking.
- The half minute runs out, so the laundrette puts basket 1 back on the rack. Nothing is lost.
- Lee takes basket 1, checks it hasn't been washed already, washes it and ticks it off.
- The ticked-off basket is gone from the rack.
- Kim takes the jammed-zip basket but can't wash it. Kim gives up, and when its timer runs out it goes back on the rack.
- The same thing happens again, and again. The laundrette counts every try.
- Three tries is the limit, so the laundrette moves the jammed basket to the problem shelf, out of everyone's way.
- The good baskets keep flowing. Kim takes basket 10, washes it and ticks it off.
- Later the manager opens the problem shelf, finds the jammed basket and works out what went wrong.
- The desk sets three baskets on the strict rail, each with a name tag. Ana's two baskets must be handled in order.
- The desk isn't sure Ana's second slip went through, so it hands it in again. The desk remembers it from a minute ago and throws the copy away.
- Kim gets Ana's first basket and Lee gets Ben's, at the same time. Ana's second basket waits until her first is ticked off.
- Kim and Lee tick their baskets off. Now Ana's second basket is handed out, after her first, never before.
- The ticked-off baskets are gone from the rail. Only Ana's second basket is left, with Kim.
- The front desk feeds one order slip into the slip copier, which has three names on its list.
- The copier prints a slip for everyone on its list and delivers them straight away. Nobody had to come and ask.
- The washing attendant takes the washing copy and ticks it off. That copy is done.
- The ironing attendant is busy, but that's fine. The ironing copy waits safely on its own rack.
- When the ironing attendant is free, they take their copy and tick it off too.
- Spring's attendant stands by the rack and is handed basket 1.
- It passes the basket to your washing routine, which finishes it and says "done".
- Only then does Spring's attendant tick basket 1 off.
- With basket 2, your routine runs into a problem. Spring's attendant doesn't tick it off.
- When its timer runs out, basket 2 is back on the rack for another try, and after too many tries it goes to the problem shelf.
๐งฉ Key questions
- Attendant Kim takes a basket and then trips before ticking it off. What happens to the basket?
- When its timer runs out, it goes back on the rack for another attendant. The laundrette only drops a basket from its books once it's ticked off, so a basket can be washed twice. Attendants should check before starting.
- One basket can never be washed and keeps coming back. What stops it clogging the rack?
- After a set number of tries, it's moved to the problem shelf. The bad basket is set aside for the manager to look at, and the good baskets keep flowing.
- The washing and ironing teams both need every new order slip. How does the laundrette set that up?
- The slip copier prints each slip onto a washing rack and an ironing rack. From one shared rack, each basket goes to only one attendant. With a copy each, every team gets every slip at its own pace.
- The desk hands the same order slip to the strict rail twice, a minute apart. What happens?
- The desk remembers the first one and quietly throws the copy away. The desk remembers slips from the last five minutes, so it can safely hand one in again when unsure.
- A team wants last week's baskets washed again after fixing a mistake. Can the laundrette do that?
- No: ticked-off baskets are gone, so the team needed to keep its own copies. A rack is for jobs that get done once. Once a basket is ticked off, there's nothing left to hand out again.