What Is a Message Queue? How It Works and When to Use One
A queue lets one part of your system hand off work and move on, while another part processes it at its own pace. Here's how queues work, the guarantees they give, and when you need one.
Message queue vs pub/sub
|
Message queue |
Pub/sub | |
|---|---|---|
|
Who receives a message |
Exactly one consumer |
Every subscriber to the topic |
|
Pattern |
Point to point, work distribution |
Broadcast, fan-out |
|
After delivery |
Deleted once acknowledged |
Each subscriber gets its own copy |
|
Adding consumers |
Shares the work and increases throughput |
Adds another recipient of every message |
|
Typical use |
Background jobs, order processing, email sends |
Event notifications, cache invalidation, live updates |
|
Examples |
Amazon SQS, RabbitMQ queues |
Amazon SNS, Google Cloud Pub/Sub, Redis Pub/Sub |
How a message queue works
A producer sends a message, usually a small JSON payload describing a job or event, to the queue and gets an immediate confirmation. It doesn't wait for the work to happen. The queue, run by a message broker such as RabbitMQ or a managed service such as Amazon SQS, stores the message durably.
A consumer asks for the next message, processes it and sends an acknowledgement. Only then is the message deleted. If the consumer crashes before acknowledging, the message becomes visible again and another consumer picks it up. In SQS this window is called the visibility timeout.
Run several consumers on the same queue and they share the load: each message goes to one of them. If work piles up, add consumers. If traffic spikes, the queue grows and drains later instead of the spike hitting your database all at once.
Delivery guarantees
At-most-once
The message is delivered zero or one times. Fast, but a crash can lose it. Fine for metrics you can afford to drop.
At-least-once
The message is redelivered until it's acknowledged, so nothing is lost, but a consumer can see the same message twice. This is the default for most queues, which is why consumers should be idempotent: processing a message twice must have the same effect as processing it once. Store a message or job ID and skip duplicates.
Exactly-once
The hardest to get. Some systems, such as SQS FIFO queues and Kafka transactions, offer exactly-once processing within their own boundaries. Across a whole system with external side effects, like sending an email or charging a card, you still need idempotency.
Ordering
Standard queues don't always preserve order. FIFO queues do, often per group key, at lower throughput. Only ask for strict ordering where it matters, such as messages within a single conversation.
Rule of thumb: assume every message can arrive twice. If processing it twice is harmless, most queue problems become routine.
Common use cases
Background jobs. Resize an upload, generate a PDF or send a welcome email after the API has already responded.
Absorbing spikes. A flash sale or a viral post pushes thousands of writes into a queue that workers drain at a pace the database can handle.
Webhook handling. Accept an incoming webhook, put it on a queue and return 200 straight away, then process it in the background, so slow processing doesn't trigger the sender's retries.
Calling rate-limited APIs. Workers pull from a queue at a pace that stays under a provider's rate limit.
Decoupling services. The checkout service doesn't need the invoicing service to be up; it only needs the queue.
Limits and pitfalls
Poison messages. A message that always fails will be retried forever. Set a maximum receive count and move it to a dead-letter queue for inspection.
Duplicates. At-least-once delivery means duplicates will happen. Design consumers to be idempotent from day one.
Hidden backlogs. A queue can quietly grow for hours. Alert on queue depth and on the age of the oldest message.
Not for real-time chat delivery. Queues are built for durable, one-consumer handoff. Pushing live messages to many connected users is a fan-out problem, closer to pub/sub over WebSockets.
Large payloads. Brokers cap message size (256 KB on standard SQS messages). Store big files elsewhere and put a reference in the message.
Where queues fit with CometChat
You don't need to run your own broker to deliver chat messages: CometChat stores and delivers messages, and pushes real-time events to connected users over WebSockets.
Queue the events CometChat sends to your backend
Queues are useful on your side of the integration. CometChat can send events to your own endpoints, for example notification payloads routed to a custom push, email or SMS provider, or moderation decisions from a custom moderation API. Accept each request quickly, put it on a queue, and let workers process it, so a slow downstream service never holds up delivery.
Frequently asked questions
What is a message queue in simple terms?
It's a waiting line for messages between two programs. One program drops off a task and moves on; another picks it up and does the work when it's ready.
What is the difference between a message queue and pub/sub?
In a queue, each message goes to one consumer, which suits distributing work. In pub/sub, each message goes to every subscriber, which suits broadcasting events.
Is Kafka a message queue?
Kafka is a distributed log. It can act like a queue through consumer groups, where each partition is read by one consumer in the group, and like pub/sub because many groups can read the same topic independently.
What is a dead-letter queue?
A separate queue that holds messages that failed processing a set number of times, so they stop blocking the main queue and can be inspected and replayed.
When should I use a message queue?
Use one when work can happen after you respond to the user, when traffic is bursty, or when you need to keep two services working even if one is temporarily down.