Skip to the page

Cloud queues · mid

SQS visibility timeout, dead-letter queues and workers

After a receive, a message is hidden for the visibility timeout and comes back if you do not delete it. Slow jobs, retries and dead-letter queues all depend on this one setting.

The notes

What you must know

  • The visibility timeout starts at receive. The default is 30 seconds. If the message is not deleted in time, it becomes visible again and another worker can take it. The clock starts at receive for all messages in a batch. If you process them one after another, the later ones can time out even when each is quick.
  • Each receive raises the receive count. Past maxReceiveCount the redrive policy moves the message to the dead-letter queue, even while it is being processed fine.
  • Raise the visibility timeout above the worst-case job time, or extend it with ChangeMessageVisibility while the job runs (a heartbeat). The maximum is 12 hours from the first receive, and extending does not reset it. Use ChangeMessageVisibility also for back-off: after a temporary failure, set a growing delay (for example based on the receive count) so the retry is not instant.
  • Permanent failures (a bad payload): send to the dead-letter queue yourself and delete, instead of burning retries.
  • Set the dead-letter queue retention longer than the source queue. For standard queues the enqueue time does not reset when the message moves, so messages can expire in the dead-letter queue earlier than you expect.
  • Alarm on any message in the dead-letter queue and on its age. Redrive with the console or StartMessageMoveTask at a limited rate, only after the fix and with idempotent handlers.
  • A worker loop: receive with long polling, bounded concurrency, a timeout shorter than the visibility timeout, delete on success, handle SIGTERM by finishing work and releasing the rest with visibility 0. With Lambda, set the queue visibility timeout well above the function timeout, and use partial batch responses so one failure does not retry the whole batch.

The red pen

Common traps: mistake → fix

  • Mistake: Delete the message as soon as it is received

    Fix: a crash then loses the job.

  • Mistake: A 12-hour timeout for everything

    Fix: a real crash then delays the retry for hours.

  • Mistake: A dead-letter queue keeps messages forever

    Fix: it has its own retention.

  • Mistake: SQS made the duplicates by mistake

    Fix: it is at-least-once; make jobs idempotent.

Easy to hard

Questions interviewers ask, easy to hard

  1. Our SQS job starts again after 30 seconds. Why?
  2. Refund messages disappeared from the dead-letter queue. Where did they go?
  3. Write an SQS worker that handles failures well.
  4. How do you extend a message's time while a job runs?

Practise

Practise with these questions

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview