AlgoViz

Amazon SQS

easy 25 min

Amazon SQS is a managed queue: producers send messages over HTTPS, consumers poll for them, and a message is hidden, not removed, while somebody works on it.

See it run

Send, hide, delete. Then crash a worker, keep a long job alive, block a FIFO group and empty the dead letters.

Operations

One message

The normal path: nothing is removed until the worker says so.

Step 1 of 4

Send

AWS and your workersProducerweb appSQS queueordersWorker ApollsWorker BpollsDead lettersempty

Visible

order 42

In flight

The producer sends order 42. SQS stores copies and answers with a message id.

Step 1: Send

send(body)msg, handle = receive(wait=20)work(msg)delete(handle)
Delay
One poll plus the work
Promise
At least once
Delete before the work
a crash loses it

Delete is the last line of the worker, after the result is saved.

In the real world

The same amazon sqs doing real jobs. Pick one and step through it.

Work takes 45 seconds, the timeout is 30.

Step 1 of 4

Second 0

AWS and your workersProducerweb appSQS queueordersWorker AstartedWorker BpollsDead lettersempty

Visible

In flight

order 42

Worker A starts order 42.

Step 1: Second 0

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • Deleting before the work

    critical

    If the worker dies in between, nobody has the message any more.

    Wrong
    delete(handle)work(msg)
    Right
    work(msg)delete(handle)
  • Not safe to repeat

    critical

    At-least-once delivery means that a second run will happen some day.

    Wrong
    charge(card, amount)
    Right
    charge(card, amount, key=order_id)
  • Short polling in a loop

    major

    Empty answers cost requests, and messages may be missed for a moment.

    Wrong
    receive(WaitTimeSeconds=0)
    Right
    receive(WaitTimeSeconds=20, MaxNumberOfMessages=10)
  • One message group for all

    major

    A constant group makes the FIFO queue a single lane that one failure blocks.

    Wrong
    MessageGroupId="orders"
    Right
    MessageGroupId=cart_id

Interview tips

  • How to explain it

    • Receive hides, delete removes.
    • The timeout replaces crash detection.
    • So the same work may run twice.
  • Memory trick

    • A cup over the token.
    • Done box: deleted.
    • Ask drawer: dead letters.
  • Reach for it when

    • Work can wait but must not be lost.
    • Load is spiky.
    • You want no broker to run.
  • Be careful when

    • Somebody needs a replay.
    • Many teams read the same event.
    • Work lasts longer than the timeout.