Backend Developer interview question
When do you use a message queue?
Use a queue when work can be asynchronous, retried, or smoothed under load. Consumers must be idempotent because delivery can be duplicated or delayed.
Direct answer
What a strong answer covers
How it works
A queue decouples the producer from the consumer in time and in capacity: the producer returns as soon as the message is durably stored, consumers process at their own pace, and bursts are absorbed rather than dropped. The price is at-least-once delivery, reordering, and eventual consistency, so every consumer must handle duplicates and the system must expose a dead-letter path for messages that keep failing. Use one when the work can be delayed and retried; keep a synchronous call when the caller needs the result now.
Example
Send an email job after committing an order; the worker records the order-event id so a redelivery does not send a second email. Publishing from inside the database transaction through an outbox table guarantees the message exists if and only if the order does, which a direct publish after commit cannot promise.
Common mistakes
- Publishing the message before the transaction commits and emailing about an order that was rolled back.
- Assuming exactly-once delivery from the broker instead of making the consumer idempotent.
- Having no dead-letter queue, so one poison message blocks the partition forever.
A useful answer structure
Answer from your own work.
Do not memorise a script: use only examples you can explain honestly in follow-up questions.
Open the desktop workspace and rehearse a clear answer in your own words.