Cloud & Data Centers

Message Queues and Event-Driven Systems

How queues and events let services work at their own pace, and what you take on when you use them.

Key takeaways

  • A queue lets a sender finish quickly while the slow work happens later, at a pace the receiver can handle.
  • A queue hands each message to one worker, while publish and subscribe delivers a copy of each event to every interested subscriber.
  • Most systems deliver messages at least once, so your consumers need to handle duplicates safely.
  • Dead-letter queues, backlog alerts and clear message formats keep an asynchronous system understandable.
  • Add a queue when work is slow, bursty or optional for the immediate reply, and skip it when the caller needs the answer now.

A message queue is a holding area that sits between a program that creates work and a program that does it. The sender drops off a message and moves on. The receiver picks it up when it's ready. Neither one has to be online, fast or even aware of the other at that exact moment.

An event-driven system builds on that idea. Services announce things that happened, such as "order placed" or "file uploaded", and other services react. This article explains how both work, when they help, and the new problems they bring along.

The problem a queue solves

Say your online store receives an order. The web server has to charge the card, save the order, send a receipt, update inventory, notify the warehouse and refresh a sales report. If it does all of that before replying, the customer waits for the slowest step, and one broken email server can make checkout fail.

Calls where the sender waits for a reply are called synchronous. They're simple to reason about, but they chain your fortunes together. If the receipt service is down for ten minutes, every order in those ten minutes inherits the problem.

With a queue, the web server does only what the customer needs right away, then writes a message saying the order exists. The receipt, inventory and reporting work happens asynchronously, meaning later and separately. If the email service is down, messages pile up and get processed when it returns.

Producers, brokers and consumers

Three roles appear in nearly every design. The producer creates messages. The broker is the server or managed service that stores them. The consumer, often called a worker, reads and processes them.

A typical exchange has an important detail in the last step. When a consumer receives a message, the broker doesn't delete it yet. It hides it or marks it as in flight. The consumer deletes or acknowledges it only after the work succeeds. If the worker crashes halfway through, the acknowledgment never arrives, and the broker hands the message out again.

How a message survives a crashed worker
Producer Broker Worker A Worker B Publish: order 1042 1 Stored, carry on 2 Deliver order 1042 3 Message is hidden, not deleted Redeliver after timeout 4 Worker A crashed and never acknowledged Acknowledge: done 5 Only now is the message removed Producer Broker Worker A Worker B Publish: order 1042 1 Stored, carry on 2 Deliver order 1042 3 Message is hidden, not deleted Redeliver after timeout 4 Worker A crashed and never acknowledged Acknowledge: done 5 Only now is the message removed

The window during which an unacknowledged message stays hidden has different names in different products, such as a visibility timeout or acknowledgment deadline. Set it longer than your slowest normal job, or a healthy worker will find its message handed to someone else while it's still working.

Queues, publish-subscribe and logs

The word "queue" covers a few related patterns, and picking the right one matters more than picking a product.

A work queue gives each message to exactly one of several workers. Add workers and the backlog drains faster. This suits jobs like resizing images or generating invoices.

Publish and subscribe, usually shortened to pub/sub, works differently. A producer publishes to a topic, and every subscription on that topic gets its own copy. The producer doesn't know who is listening, so you can add a fraud checker or analytics feed later without touching the order service.

A log-based system, such as Apache Kafka, keeps messages in an ordered, append-only record for a set retention period. Consumers track their own position, called an offset, and can rewind to reprocess history. That's powerful and also more to operate.

Three ways to pass messages
Work queue One worker handles each message Removed after acknowledgment Good for background jobs Publish and subscribe Every subscription gets a copy Producer doesn't know listeners Good for announcing events Event log Records kept for a retention period Readers track their own position Can replay history Work queue One worker handles each message Removed after acknowledgment Good for background jobs Publish and subscribe Every subscription gets a copy Producer doesn't know listeners Good for announcing events Event log Records kept for a retention period Readers track their own position Can replay history

Many real systems combine them. A topic fans an event out to several queues, and each queue has its own pool of workers.

What "event-driven" means

An event is a statement about something that already happened: OrderPlaced, PasswordChanged, PaymentFailed. A command, by contrast, asks for something to happen: SendReceipt. The difference shapes who is responsible for what.

With commands, the sender decides what should occur next. With events, the sender just reports facts, and each subscriber decides what to do. That loosens the connections between teams and services, which is one reason event-driven design often shows up alongside a microservices architecture.

A good event carries a unique ID, a type, a timestamp, and enough data for consumers to act or look up the rest. For example:

{
  "id": "evt-7f3a",
  "type": "order.placed",
  "occurred": "2026-03-04T15:20:11Z",
  "order_id": 1042
}

Some teams put the full order in the event, which saves consumers a lookup. Others send only the ID, which keeps messages small and avoids spreading sensitive data. Either works if you choose deliberately.

Delivery guarantees and duplicates

Brokers describe their promises in three phrases. "At most once" means a message may be lost but never repeats. "At least once" means it won't be silently lost but may arrive more than once. "Exactly once" is offered in limited forms by some systems, usually within their own boundaries and with conditions.

At least once is the common case, so assume duplicates. A worker can finish a job and crash before acknowledging it, and the broker has no way to know the work was done.

The fix is idempotency: processing the same message twice has the same result as processing it once. Practical techniques include recording processed message IDs in your database, using a unique constraint so a second insert fails harmlessly, or writing "set the status to shipped" instead of "add one to the count". Our guide to retries, backoff and idempotency goes further into this.

Ordering is harder than it looks

It's natural to expect messages to arrive in the order they were sent. With several workers running in parallel, that rarely holds. Message 2 may finish before message 1, and a retried message can arrive much later.

When order matters, brokers usually let you group messages by a key, such as a customer ID. Messages with the same key go to the same partition or group and are processed in sequence, while different keys still run in parallel. A busy key then becomes a bottleneck, because it can't be split.

Often the better answer is to make order unimportant. Include a version number or timestamp, and have consumers ignore anything older than what they've already applied.

Failures, retries and dead letters

Some messages will never succeed. A record might be malformed, or refer to something deleted. Without a limit, that "poison message" gets redelivered forever and can block others behind it.

A dead-letter queue is a side queue where messages go after a set number of failed attempts. It keeps the main flow moving and preserves the evidence. Someone, or some alert, needs to watch it, because a dead-letter queue nobody reads is just a slow way of losing data.

Retries should wait longer each time and include some randomness, so a recovering database isn't hit by every worker at once. You can experiment with timing in the retry and backoff simulator.

A message failed. What next?
Is the failure likely temporary, like a timeout? YES Retry later with growing delays 1 NO Has it already succeeded once? YES Acknowledge and skip the duplicate 2 NO Has it used up its allowed attempts? YES Move it to the dead-letter queue and alert 3 NO Retry once more Log the error with the message ID so the cause can be traced. Is the failure likely temporary, like a timeout? 1 IF YES Retry later with growing delays NO Has it already succeeded once? 2 IF YES Acknowledge and skip the duplicate NO Has it used up its allowed attempts? 3 IF YES Move it to the dead-letter queue and alert NO Retry once more Log the error with the message ID so the cause can be traced.

Backlogs, capacity and visibility

A queue absorbs bursts, but it doesn't create capacity. If messages arrive faster than workers finish them for long enough, the backlog grows without limit and results get stale.

Watch two numbers first: how many messages are waiting, and the age of the oldest one. Age is often the more useful signal, because it maps to what users feel. Then scale the number of workers when the backlog rises, as long as whatever they depend on, such as a database, can take the extra load. See scaling up versus scaling out for that trade-off.

Debugging also changes. A single user action may now pass through five services over several minutes. Give each request a correlation ID, copy it into every message and log line, and use logs, metrics and traces to follow the path.

When not to use a queue

A queue adds a component to run, secure and monitor. It also introduces a delay between "accepted" and "done" that your interface must explain. If a user clicks to check a password, they need the answer now, and a queue only gets in the way.

Queues earn their place when work is slow, when traffic arrives in spikes, when a downstream service is unreliable, or when several independent consumers care about the same event. A small application can often start with a jobs table in its existing database and a single worker, then move to a dedicated broker when volume or complexity calls for it. Cloud providers also offer managed queues that pair naturally with serverless functions, which run only when a message arrives.

Common questions

Is a webhook the same thing as a message queue?

No. A webhook is an HTTP request one system sends to another when something happens. Many receivers put incoming webhooks straight onto a queue, reply quickly, and process them afterward.

What happens if the broker itself fails?

It depends on how it's set up. Durable queues write messages to disk, and replicated brokers keep copies on several machines. In-memory settings are faster but can lose messages on a restart. Check what your configuration actually guarantees.

How do I avoid saving data but losing the message?

Writing to a database and publishing a message are two separate actions, and a crash can separate them. A common remedy, the outbox pattern, saves the outgoing message in the same database transaction as the data. A separate process then publishes it, retrying until it succeeds.

Written for 4Internet with AI writing tools and not reviewed line by line by a person. Check anything you rely on with the maker, provider or standard concerned. Report an error.

Keep reading

All 45 articles in Cloud & Data Centers →

Latest news on this topic