Message Queue

Updated Nov 16, 2026 · 5 min read
On this page

    A customer clicks "Place order". Before the page can answer, the Order Service saves the order, sends a confirmation email, updates inventory, notifies the warehouse and records an analytics event. The email provider is having a slow day and takes 4 seconds. Checkout now takes 4 seconds too. When the provider goes down completely, orders fail, even though the order itself was saved fine.

    The order did not need the email to finish before replying. It only needed someone to send it, soon and reliably. Putting that work on a list that another process works through is the idea behind a message queue.

    How it works

    Asynchronous order flow: the Order Service saves the order, publishes an order-placed message and replies 202 in about 30 ms; email, inventory and analytics workers each consume from the queue at thei
    Asynchronous order flow: the Order Service saves the order, publishes an order-placed message and replies 202 in about 30 ms; email, inventory and analytics workers each consume from the queue at their own pace.
    1. The request comes in through the API Gateway and load balancer to the Order Service.
    2. The service writes the order to its database (the part the user must not lose).
    3. It publishes a message, {order_id: 7781, user_id: 42, total: 79.00}, to the order-placed queue or topic and replies to the user right away.
    4. Consumers (workers) read messages and do the slow work: send the email, reserve stock, update analytics. Each one runs at its own pace and can be scaled separately.

    If the email worker is slow, messages wait in the queue. If it crashes, they wait until it comes back. The user never notices.

    Queue vs log (topic)

    Work queue (SQS, RabbitMQ)Log / topic (Kafka, Kinesis, Pulsar)
    Who gets a messageOne consumer in the poolEvery consumer group gets every message
    After processingMessage is deletedMessage stays for the retention period (days)
    OrderingBest effort (FIFO queues exist, with lower throughput)Ordered within a partition
    ReplayNoYes: reset the offset and reprocess
    Typical useBackground jobs, tasksEvent streams, fan-out to many services, analytics

    For "send this email" a work queue is enough. For "an order was placed, and five services care" a topic is the better fit: each service has its own consumer group and its own position.

    Delivery: ack, retry, dead letters

    At-least-once delivery: the worker acks a message after success; with no ack before the timeout the message is delivered again; after five failures it moves to a dead-letter queue.
    At-least-once delivery: the worker acks a message after success; with no ack before the timeout the message is delivered again; after five failures it moves to a dead-letter queue.
    • A consumer acks a message only after it has finished the work. If it crashes first, the queue delivers the message again after a visibility timeout.
    • This gives at-least-once delivery: nothing is lost, but a message can arrive twice. So handlers must be idempotent: processing message 17 twice has the same effect as once (see Idempotency & Deduplication).
    • Failed messages are retried with exponential backoff. After a limit (say 5 attempts) they move to a dead-letter queue (DLQ), where someone can inspect and replay them. One bad message never blocks the rest.
    • "Exactly-once" in practice means at-least-once delivery plus idempotent processing.

    Ordering

    Most queues do not guarantee global order. Logs keep order within a partition, and you choose the partition by key. Use order_id as the key and all events for one order (placed, paid, shipped) arrive in order to the same consumer, while different orders spread across partitions for throughput.

    Trade-offs and costs

    • Eventual consistency: the email goes out a few seconds later, not before the reply. Fine for emails, wrong for "is the payment authorized?"
    • Duplicates and reordering: you design for them with idempotent handlers and per-key partitions.
    • Harder debugging: a request's work is spread across services and time. You need correlation IDs and tracing.
    • Another system to run: brokers need capacity planning, retention settings and monitoring.
    • Dual-write problem: saving to the database and publishing to the queue are two separate writes. If the service crashes between them, the event is lost. The transactional outbox fixes this: write the event to an outbox table in the same transaction, and a relay publishes it.

    When to reach for it

    • Work that does not need to finish before you reply: emails, notifications, thumbnails, video transcoding, search indexing.
    • Spiky traffic that a downstream system cannot absorb directly (a flash sale feeding an order processor that handles 1,000 per second).
    • One event that many services react to (order placed → inventory, email, analytics, fraud).
    • Calls to unreliable third parties that need retries.

    Do not use a queue when the caller needs the result now (a price lookup, a login), or when the extra latency and complexity buy nothing.

    Example

    Video upload on a course site. Assumptions: 2,000 uploads a day, each needs about 10 minutes of transcoding on one worker, and uploads cluster in the evening (5× the average rate).

    • Without a queue: the upload request would wait 10 minutes, and an evening burst would need 5× the transcoding machines sitting mostly idle the rest of the day.
    • With a queue: the Upload Service stores the file, publishes video-uploaded {video_id} and replies "processing". Transcoding workers pull jobs. At the evening peak the queue grows, and an autoscaler adds workers based on queue depth (for example, one more worker per 20 waiting jobs).
    • Reliability: each job has a 30-minute visibility timeout. If a worker dies mid-transcode, the job reappears and another worker picks it up. The handler writes outputs under the video ID, so a repeat just overwrites the same files. After 3 failures (a corrupt file), the job goes to a DLQ and the video is marked "failed" for the user.
    • Monitoring: alert on queue depth and on the age of the oldest message, not just on errors.

    Common mistakes

    • Assuming exactly-once delivery and writing handlers that double-charge or double-send on a repeat.
    • No dead-letter queue, so one poison message retries forever and blocks a partition.
    • Putting the queue in a path that needs an immediate answer.
    • Ignoring ordering when it matters, or demanding global order when per-key order is enough.
    • Publishing from inside the request without an outbox, so a crash loses events.
    • Not watching queue lag. A growing backlog is the earliest sign that consumers are falling behind.

    Say this in the interview

    "The order is saved synchronously, and everything else, email, inventory sync, analytics, goes through an order-placed topic so checkout replies in about 30 ms. Delivery is at-least-once, so every consumer is idempotent on order_id. Events are partitioned by order_id to keep each order's events in order. Failed messages retry with backoff and land in a dead-letter queue after five tries, and we autoscale workers on queue depth. To avoid losing events between the database write and the publish, I'd use an outbox table."

    Used in