What is a message queue?

A message queue holds messages from a producer until a consumer is ready to process them. The producer doesn't wait for the work, and a message that fails to process can be delivered again.

Also called message queues, message broker, job queueUpdated

A message queue holds messages sent by one part of a system, the producer, until another part, the consumer, is ready to process them. The producer doesn't wait for the work to finish, and the two don't need to be running at the same moment. Queues absorb traffic spikes, let you add consumers to get through work faster, and keep work from being lost when a consumer crashes.

The term also appears in operating systems, where a message queue is a way for processes on one machine to exchange data (inter-process communication). This page is about queues between services.

How a queue delivers a message

  1. The producer sends a message, such as {"type": "GenerateInvoice", "orderId": "ord_552"}.
  2. The broker stores it durably.
  3. A consumer receives it. The message is hidden from other consumers but not yet deleted.
  4. The consumer does the work and acknowledges the message, which deletes it.
  5. If the consumer crashes, or doesn't acknowledge in time, the message becomes visible again and another consumer receives it.

Step 5 is why most queues deliver at least once. A message can arrive twice, for example when the work finished but the acknowledgement was lost. Consumers therefore have to be idempotent: record each message ID, or check whether the work is already done, and skip repeats.

After a set number of failed attempts, the message moves to a dead-letter queue, so one bad message doesn't retry forever or block the rest.

Example: invoice PDFs on Amazon SQS

An accounting app generates a GST invoice PDF when an order is paid. Each PDF takes 4 to 8 seconds, too slow to do inside the API request.

  • The API saves the order, records a GenerateInvoice message through a transactional outbox so a crash can't lose it, and responds in about 150 ms.
  • Three workers poll the queue. SQS hides each received message for the visibility timeout, 30 seconds by default. The team raises it to 60 seconds to cover slow PDFs.
  • A redrive policy with maxReceiveCount set to 5 moves a message to the dead-letter queue after five receives without a delete. An alarm fires whenever the dead-letter queue isn't empty.
  • At month-end the backlog grows to 20,000 messages. The team adds workers, the backlog clears, and the API stays fast throughout.

Before generating a PDF, each worker checks whether that invoice already exists, so a duplicate delivery never emails the customer twice.

Message queue vs message broker, Kafka and webhooks

  • Broker vs queue. A message broker is the server software, such as RabbitMQ, or a managed service such as Amazon SQS. A queue is one structure a broker provides. Brokers can also route messages into queues by topic or routing key.
  • Queue vs Kafka. A queue gives each message to one consumer and deletes it once processed. Kafka keeps an ordered log for a retention period, and many consumer groups read the same events independently and can replay them. Use a queue for work ("send this email") and a log for events that many systems need ("payment captured").
  • Queue vs webhook. A webhook pushes an event over HTTP to another company's endpoint. A good webhook receiver verifies the event, puts it on a queue and returns 200 quickly.

Our Kafka vs RabbitMQ vs SQS comparison covers ordering, replay and when a Postgres-backed job queue is enough.

Any idea in mind? Let's make it real.

Tell us what you're building. You'll hear back from an engineer, not a sales team, usually within one working day.

Book a 30-min call