Skip to content

Message queue

Introduction

Decouple services with a queue: the sender returns after enqueueing, and the consumer processes at its own pace.

✗ Without it
User places an order
Sync: send email, deduct stock, notify warehouse
One step gets stuck
The user keeps spinning, and the order fails
✓ With it
User places an order
Only write the order and enqueue a message
Tell the user success immediately
Background slowly handles email, stock, and notifications

Why use it

  • Async decoupling: after an order is placed, “send a confirmation email” does not have to make the user wait. The order service only puts a message on the queue; the email service processes it when free — even if the email service is down, placing orders still works.
  • Peak smoothing: a flash sale dumps 10,000 requests at once; the queue absorbs them, and the background processes them at, say, 200 per second.
  • Retryable: failed messages can stay in the queue and be tried again later.

How to do it

Key concepts:

  • Producer: the side that sends messages. Consumer: the side that takes messages and processes them.
  • Acknowledgement (ack): after processing, the consumer tells the queue “you can delete this”. If it crashes without acking, the message is redelivered.
  • At-least-once delivery: the queue guarantees messages “are not lost, but may be duplicated”. Consumers must therefore be idempotent, so a duplicate delivery does not cause errors — see Idempotency.
  • Dead-letter queue: “poison messages” that keep failing are moved to a dedicated queue for humans to investigate, so they do not block healthy traffic forever.

Choosing a solution

Option Best for Traits Watch out
RabbitMQ General task dispatch and business messages Flexible routing, easy to learn Messages are deleted after consume; not suited to replay
Kafka High-volume logs, event streams, replay needs Very high throughput; messages can be retained and reread Heavier to operate; overkill for small projects
Redis Streams You already have Redis and needs are simple No extra component Durability and features trail dedicated queues — see Redis
Cloud queues (SQS, Azure Service Bus, etc.) You do not want to operate your own Managed and low-ops Tied to a cloud vendor; billed by usage

Component comparison

RabbitMQ

Self-hosted open sourceOpen sourceOpsMedium

A classic "post office" style message queue: producers drop messages in, it distributes them to consumers by rule, and deletes a message only after it is processed and acknowledged.

Best forBusiness messages such as orders, notifications and emails, at small to mid scale where reliability is required.

Avoid whenYou have only a small 2 GB server and very simple needs, or must handle log streams of hundreds of thousands of messages per second.

Pros

Flexible routing rules (distribute by topic or condition)

Supports acknowledgements, retries and dead-letter queues (a "to-handle box" where messages that fail processing are moved automatically), so business messages are hard to lose

Mature documentation and community, with clients in every language

Cons

You must deploy and maintain a service yourself, and its memory usage is not small

Lower throughput than Kafka, unsuited to massive log streams

Clustering and high availability need some ops experience, and when messages pile up, memory alarms make it pause receiving messages

Official docs ↗

Kafka

Self-hosted open sourceOpen sourceOpsHigh

Like an append-only, never-erased "ledger": messages are recorded in order and kept for a while, and multiple consumers each read at their own pace.

Best forLarge-volume log collection, behavior tracking, data synchronization and event stream analysis.

Avoid whenLow business volume and nobody on the team understands ops, and you just want to send emails or handle order notifications.

Pros

Throughput is extremely high, which suits logs, tracking events, and event streams

Messages can be kept and replayed, which helps backfill data and investigate

You scale by partition and add consumption capacity horizontally

Cons

There are many concepts, so learning and debugging are harder than with RabbitMQ

Cluster deployment and maintenance are both heavy, and a small team struggles

Flexible routing and delayed retries both have to be put together by you

Official docs ↗

Redis Streams

Self-hosted open sourceOpen sourceOpsLow

Use your existing Redis directly as a simple queue, with no new service to install.

Best forSmall projects already using Redis where losing a message or two is acceptable.

Avoid whenLosing a message would cause financial loss (such as payment notifications), or messages will pile up in large numbers.

Pros

If you already have Redis, you do not deploy another queue

Read and write performance is good, and the way you call it is fairly direct

Consumer groups are supported, so several workers can share the work

Cons

Data lives mostly in memory, so a crash can lose the most recent messages

Delayed retries and a dead-letter queue both have to be added by you

A large backlog of messages squeezes the memory used by the cache

Official docs ↗

Cloud message queues (e.g. Alibaba Cloud RocketMQ, cloud-provider managed queues)

Cloud-managedPay-as-you-goOpsLow

Cloud providers turn message queues into ready-made services you can use once purchased, with servers, scaling and backups handled by the provider.

Best forBusiness messaging for teams already on a cloud with no dedicated ops staff who want less hassle.

Avoid whenDeployed in your own data center or not using a cloud, or the project has an extremely low budget and very little traffic.

Pros

You do not install the machines yourself, and the vendor guarantees availability

It usually includes monitoring, message lookup, and alerts

When you need more capacity, a few clicks in the console are enough

Cons

You keep paying by usage or by size, and a larger volume costs more

It is tied to one cloud vendor, so moving away means changing the integration

Local development uses an emulator, or it connects to an instance in the cloud

Checklist

  • Consumers are idempotent; duplicate messages do not cause double charges or double shipments
  • Ack only after successful processing, not on receipt
  • Queues and messages are durable so restarts do not lose them
  • A dead-letter queue and failure alerts are configured
  • Queue depth is monitored; backlog means consumers cannot keep up
  • Message bodies carry a unique ID and a version for debugging and evolution