Skip to the page

Cloud queues · Build it

Build an SQS worker that handles failures

The interviewer asks

Write a worker in Python with boto3, or in Node with the AWS SDK v3, that reads 'generate invoice' jobs from SQS, runs them and handles failures properly. It runs in a container, not Lambda.

Then

Follow-ups the interviewer asks next

  1. Why long polling, and why 10 at a time?
  2. How do you retry after a growing delay without a separate queue?
  3. A job can take 20 minutes. What changes?
  4. What does your worker do on SIGTERM during a deploy?
  5. DeleteMessageBatch returns one failed entry. What then?
  6. How do you test this on a laptop?

The answer

What a strong answer covers

  • Loop: ReceiveMessage with WaitTimeSeconds=20 and MaxNumberOfMessages=10, asking for the ApproximateReceiveCount attribute.
  • Run messages with bounded concurrency (a thread pool or a promise pool), each with a timeout shorter than the visibility timeout; for long jobs, a heartbeat that calls ChangeMessageVisibility every few minutes.
  • Success: delete with the receipt handle from this receive; batch deletes with DeleteMessageBatch and check its Failed list (retry those; if one still fails, the message may come back, which idempotency covers).
  • Temporary failure: don't delete; set the visibility to a growing delay (for example 2 to the power of the attempt, in seconds, plus jitter, using the receive count) so the retry isn't instant. After maxReceiveCount, the redrive policy moves it to the dead-letter queue.
  • Permanent failure (a bad payload): don't burn five retries; log it with the message id, send it to the dead-letter queue yourself (or a parking store), then delete it.
  • Idempotency: invoice creation keyed by order id (a unique constraint) and a fixed S3 key, so a repeat does nothing.
  • SIGTERM: stop receiving, finish in-flight work within the grace period, and release anything unfinished with visibility 0 so another worker takes it at once.
  • Metrics and logs: processed, failed and retried counts, job time, the message id in every log line; scale on queue depth and the age of the oldest message.
  • Tests: the handler alone in unit tests; the loop against LocalStack or a real test queue.

The red pen

Common traps

Avoid:deleting before processing; unbounded concurrency; ignoring batch delete failures; endless retries for a bad payload.

Practise this out loud

Say your answer out loud to an AI interviewer that follows up, and get a score for it.

Practise by voice

Practise questions like this out loud

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

Start your free interview