Cloud queues · Build it
Build an SQS worker that handles failures
Practise this out loud
Say your answer out loud to an AI interviewer that follows up, and get a score for it.
Practise by voiceThe 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
- Why long polling, and why 10 at a time?
- How do you retry after a growing delay without a separate queue?
- A job can take 20 minutes. What changes?
- What does your worker do on SIGTERM during a deploy?
DeleteMessageBatchreturns one failed entry. What then?- How do you test this on a laptop?
The answer
What a strong answer covers
- Loop:
ReceiveMessagewithWaitTimeSeconds=20andMaxNumberOfMessages=10, asking for theApproximateReceiveCountattribute. - 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
ChangeMessageVisibilityevery few minutes. - Success: delete with the receipt handle from this receive; batch deletes with
DeleteMessageBatchand check itsFailedlist (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 voicePractise 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.