Message queue
Amazon SQS
Holds work so the sender doesn’t wait for the doer. Each message is handled by one consumer.
In a monolith
Jobs table / Redis list
Background jobs in the same codebase (ARQ / Celery / Sidekiq) using Redis or a jobs table as the queue.
In a distributed system
Broker between services
Decouples producer and consumer availability, turns spikes into backlog instead of errors, and enables retries.
On AWS
Amazon SQS
Managed queues: Standard (huge throughput, at-least-once, best-effort order) or FIFO (ordered per message group, deduplication). Visibility timeouts and redrive to a DLQ.
- SQS Standard$At-least-once, best-effort ordering, nearly unlimited throughput
- SQS FIFO$Ordered per group, deduplication window, lower throughput
- Amazon MQ (RabbitMQ / ActiveMQ)$$AMQP/MQTT compatibility for existing apps
- Reach for it when
- Work that’s slow, spiky, retryable, or doesn’t need to finish before you reply.
- What it costs you
- At-least-once delivery → duplicates; consumers must be idempotent
- Eventual consistency: results arrive later
- Backlog and poison messages need monitoring and a DLQ
- When it dies
- Managed queues are multi-AZ. If consumers die, messages wait safely; if producers can’t publish, the request must fail or fall back (outbox pattern).
Interview follow-ups
Queue vs pub/sub?
Queue: each message goes to one consumer (work distribution). Pub/sub: each message goes to every subscriber (broadcast). Combine them: topic → one queue per subscriber.
What is a visibility timeout?
After a consumer receives a message it’s hidden for N seconds; if it isn’t deleted in time (consumer crashed), it reappears for another consumer. Set it longer than processing time.
How do you guarantee both the DB write and the message happen?
Transactional outbox: write the event into an outbox table in the same DB transaction; a relay publishes it to the queue.
Connection rules
It talks to
Gets traffic from
- App server
- Service
- Worker
- Scheduler
- Realtime server
- Workflow
- Object storage
- Pub/sub topic
- API gatewayOnly when: AWS API Gateway service integrations (e.g. POST /events → SQS, Kinesis or EventBridge) for ingestion and webhooks — fine when there’s no business logic beyond request mapping.
- SQL databaseOnly when: A CDC connector or outbox relay tails the log and publishes — the transactional outbox pattern.
- NoSQL storeOnly when: A CDC connector or outbox relay tails the log and publishes — the transactional outbox pattern.
- DLQOnly when: Redrive: after the bug is fixed, failed messages go back to the source queue.
Never
- Queue → SQL databaseQueues don’t run code. A consumer reads the message and does the write or call.
- Queue → Read replicaQueues don’t run code. A consumer reads the message and does the write or call.
- Queue → NoSQL storeQueues don’t run code. A consumer reads the message and does the write or call.
- Queue → CacheQueues don’t run code. A consumer reads the message and does the write or call.
- Queue → Object storageQueues don’t run code. A consumer reads the message and does the write or call.
- Queue → SearchQueues don’t run code. A consumer reads the message and does the write or call.
- Queue → DB proxyQueues don’t run code. A consumer reads the message and does the write or call.
- Queue → Third-party APIQueues don’t run code. A consumer reads the message and does the write or call.
- Clients → QueueClients can’t be trusted to enqueue directly — there’s no validation or auth in between.