Queue-Based Load Levelling
Place a queue between the part of a system that receives work and the part that does it, so that spikes in demand are absorbed by the queue rather than overwhelming the consumer.
The problem
Demand is rarely smooth. A marketing email goes out, a batch job finishes, or it’s 9am on a Monday, and requests arrive many times faster than usual. If the service doing the work (or something it depends on, such as a database or a third-party API) can’t scale as fast as the spike, requests time out, errors cascade and users get a bad experience.
The solution
The producer writes a message to a durable queue and moves on. One or more consumers read from the queue at a rate they can sustain. The queue absorbs the peaks; the consumers work through the backlog.
In the cloud this is usually a managed queue (Amazon SQS, Azure Service Bus or Storage Queues, Google Cloud Pub/Sub) with consumers that are serverless functions or containers scaled on queue depth.
When to use it
- Work can be done asynchronously: the user doesn’t need the result in the same request.
- Load is spiky or unpredictable.
- A downstream dependency has a fixed capacity or rate limit that you must protect.
- You want to decouple the availability of the producer from the consumer, so the front end keeps accepting work while the back end is down.
Trade-offs
- Latency: work completes later. The user experience must handle “accepted, processing” states.
- At-least-once delivery: messages may be delivered more than once, so consumers must be idempotent.
- Poison messages: a message that always fails will be retried forever unless you configure a dead-letter queue, with alerting and a way to replay.
- Backlog visibility: monitor queue depth and the age of the oldest message. A growing backlog is an early warning that consumers can’t keep up.
- Ordering: standard queues don’t guarantee order. FIFO options exist but limit throughput.
Related
- Event-Driven Architecture
- Resilience
- Circuit Breaker for when you can’t make the call asynchronous