In brief
A queue offloads a system at peak because it accepts work faster than the work can be done and hands it to consumers at a pace the database and external systems can sustain. This only works when the user does not need the result immediately and, on average, consumers keep up with the inflow. Without acknowledgements, a bounded number of retries, a dead-letter queue and idempotent consumers, a queue turns a peak into lost or duplicated operations.
How a queue offloads a system
In synchronous processing the client waits until the system has done all the work: saved the order, generated a document, sent it to the ERP and emailed the customer. At peak every one of these steps loads the system at the same moment. In asynchronous processing the application saves only what is essential, puts a task on a queue and replies immediately, for example "application received, number 4821". Consumers do the rest at their own pace.
The Azure Architecture Center calls this the queue-based load leveling pattern: the queue is a buffer between a task and a service, smooths out bursts and lets you design the service for average load instead of peak. In an online application system for public enrollment, thousands of applications submitted in the first minute go into a queue, and their verification and documents are produced over the following minutes without overloading the database.
Choosing between a service bus and an event platform is covered in the article on ESB or event-driven architecture, and the first decisions with Kafka in the article on Apache Kafka in the enterprise.
When a queue helps and when it does not
| Situation | Does a queue help? | Why |
|---|---|---|
| sending emails, SMS, notifications | yes | the customer does not wait for delivery, and the provider has its own limits |
| generating PDFs, exporting reports | yes | heavy work, the result can be downloaded later |
| passing an order to the ERP or a partner system | yes | the target system may be slow or unavailable |
| payment authorization, balance check | usually not | the customer needs a decision now |
| login, search, viewing data | no | these are reads, which a cache or an index helps |
| inflow consistently higher than consumer throughput | no | the queue grows forever and the problem is only postponed |
The last row matters most. A queue moves work in time, but it does not remove it. If a typical hour brings more tasks than consumers complete, you need more throughput, not a buffer.
The math: will consumers keep up
A simple calculation shows how many consumers you need and how long the queue will take to drain. The numbers are illustrative:
- The peak lasts 10 minutes and brings 600 tasks per second, or 360,000 tasks.
- One consumer completes 25 tasks per second, and the database can sustain 12 consumers in parallel, which is 300 tasks per second.
- During the peak the queue grows by 300 tasks per second and after 10 minutes holds 180,000 tasks.
- After the peak the inflow drops to 100 per second, so the queue shrinks by 200 per second and drains in 15 minutes.
The calculation gives you a promise for the user ("document within 30 minutes") and a limit you must not exceed, which is set by the database's capacity, not the number of consumers. Adding consumers beyond that limit moves the bottleneck to the database, as we describe in the article on capacity planning for transaction systems.
Kafka adds a partition constraint. Its documentation explains that each partition of a topic is consumed by exactly one consumer within a consumer group at any given time, so the number of partitions caps a group's parallelism. Kafka guarantees order only within a partition, which is why events for one customer go to the same partition through a shared key.
Reliability: acknowledgements, retries and idempotency
A peak in a queue is also a peak in failures: a slow target system, timeouts, malformed messages. Four mechanisms protect against loss and duplicates:
- Acknowledgements. A consumer acknowledges a message only after the work is done. In RabbitMQ, manual acknowledgements are combined with a bounded prefetch, which the documentation describes as a limit on in-flight deliveries per channel; automatic acknowledgements have no such limit and risk overloading the consumer.
- Visibility timeout. In Amazon SQS a received message is hidden from the queue for the visibility timeout, 30 seconds by default, with a 12-hour maximum. If the work takes longer and the consumer does not extend the timeout, the message reappears and runs a second time.
- Dead-letter queue. A message that fails repeatedly moves to a dead-letter queue. In SQS this is governed by maxReceiveCount. Without such a queue, one bad message blocks consumers or circulates forever.
- Idempotency. Most queues guarantee at-least-once delivery, so the same message can arrive twice. The consumer records the IDs of handled messages or uses an idempotency key in the target system, so a second delivery does not issue a second invoice.
Spread retries over time with increasing intervals, because immediate retries at peak add traffic to an already overloaded system.
What to measure and how to test
A queue needs its own metrics, because API response times look fine even when work waits for an hour:
- queue depth and the age of the oldest message,
- consumer lag in Kafka,
- task processing time and consumer throughput,
- the number of retries and messages in the dead-letter queue.
The age of the oldest message is the best metric for the business, because it says directly how long a customer waits for a document. You can also base consumer autoscaling on it or on consumer lag, as we describe in the article on autoscaling in Kubernetes.
A load test of a system with a queue should cover the inflow peak, the time to drain the queue afterward, a consumer failure mid-task, a slow target system and a message that cannot be processed. An example of moving payments to event processing is the project on replacing an ESB with a Kafka event platform.
Questions and answers
When should I move from synchronous to asynchronous processing?
When the user does not need the result in the same response and the work is heavy, depends on a slow external system or arrives in waves. Typical examples are documents, notifications, exports and passing data to the ERP. Decisions needed immediately, such as payment authorization, stay synchronous and have to be made faster in other ways.
Kafka or RabbitMQ for a task queue?
RabbitMQ and similar brokers fit task queues well, with per-message acknowledgements, priorities and dead-letter queues. Kafka is a distributed event log: it retains history, lets many groups read the same events and scales through partitions. Teams more often choose Kafka for streams of business events and a queue broker for simple background tasks.
How do I avoid processing a message twice?
Assume a duplicate will arrive and make the consumer idempotent. Every message has a unique ID, and before doing the work the consumer checks whether it has already handled it, ideally in the same transaction that writes the result. When the target system supports an idempotency key, pass it in the call.
What should I do with messages in the dead-letter queue?
Monitor their number and alert on every increase, because each message is a customer operation that did not happen. After fixing the cause, move the messages back to the main queue with a tool that preserves their IDs. Messages that cannot be fixed need a manual decision and a record of what was done with them.