Queue-Based Load Leveling
Put a queue between a spiky source of work and the service that does it, so bursts are absorbed and processed at a steady, safe rate.
Description
Traffic is rarely smooth: a flash sale or a marketing email sends a burst many times the normal load. If the API does all the work inside the request, the database and downstream services take the full spike and may fall over.
With load leveling, the API only validates the request, puts a message on a queue and answers "accepted" right away. Workers pull messages at the pace the backend can handle. The queue grows during the burst and drains afterwards; nothing is lost, and the backend never sees more than it can take.
System design
Queue-Based Load Leveling system design diagram
- Shoppers (client): spiky: up to 2 000/s.
- Checkout API (service).
- Order queue (queue).
- Order worker (service): steady 50/s.
- Orders DB (database).
- Step 1: Shoppers → Checkout API: burst of orders.
- Step 2: Checkout API → Order queue: enqueue.
- Step 3: Checkout API → Shoppers: 202 Accepted.
- Step 4: Order queue → Order worker: pull at own pace.
- Step 5: Order worker → Orders DB: write.
- A flash sale sends a burst of orders far above normal traffic.
- The API validates each order and puts it on the queue; that's all it does in the request.
- Shoppers get "202 Accepted, we're processing your order" immediately, even at the peak.
- Workers pull orders at the rate the backend can handle. The queue grows during the burst and drains afterwards.
- The database sees a steady 50 writes per second instead of the spike.
- AWS
- Amazon SQS with Lambda or ECS workers; queue-depth alarms in CloudWatch.
- Azure
- Azure Service Bus or Storage Queues with Functions or Container Apps workers.
- Google Cloud
- Cloud Tasks or Pub/Sub with Cloud Run workers.
When to use it
Use it when
- Load is bursty, and the work doesn't have to finish inside the user's request.
- A downstream system, such as a legacy database or a rate-limited partner API, can only take a fixed rate.
- You want the front end to stay fast and available even when the backend is slow or briefly down.
Avoid it when
- The user needs the result immediately to continue, such as a search or a balance check.
- Load is steady and well within capacity. The queue adds latency and moving parts for nothing.
Pitfalls
- No way for users to see the outcome. Give them a status ("processing", "done", "failed") and notify them when it's finished.
- Unbounded queues hiding a real capacity problem. Alert on queue depth and age of the oldest message.
- Workers that aren't idempotent. Messages can be delivered more than once.
- Poison messages that fail forever and block the queue. Use a dead-letter queue.
Alternatives
Throttling
Choose it when you'd rather reject excess requests than accept and queue them.
Competing Consumers
Choose it when you also want to scale processing out with several workers. Often combined.
Autoscaling
Choose it when the backend can scale quickly enough to serve the spike directly.