Course map

Topic 110 / Core

Queue-based load leveling

Chapter 7.1 · Asynchronous Foundations

What and why

A producer's traffic is often spiky (a flash sale, a batch upload) while the downstream processor has fixed, steady capacity; calling it directly during a spike overloads it. Queue-based load leveling places a queue between the spiky producer and the steady consumer so the queue absorbs the burst and the consumer drains it at a sustainable rate. Builds on: → 106.

Diagram

Queue-based load leveling Three aligned timelines: arrivals spike into a burst and fall back, queue depth rises during the burst and drains afterward, and consumer rate stays constant throughout. arrivals: (burst) queue depth: (rises, then drains) consumer rate: constant
Arrivals spike into a burst, then fall back. The queue absorbs the burst — depth rises, then drains — while the consumer keeps pulling at its constant, provisioned rate. A burst becomes a processing delay, not an overload, as long as the backlog eventually clears.
Text version of this diagram
arrivals:  ▁▁▂▇▇▇▇▂▁▁   (burst)
queue depth:    ╱▔▔▔╲      (rises, then drains)
consumer rate:  ───── constant

How it works

  1. The producer writes to the queue as fast as it wants; the write is cheap and almost always succeeds even during a burst.
  2. Queue depth rises during the burst, absorbing it instead of pressuring the consumer.
  3. Consumers pull at a constant, provisioned rate sized for sustained average load, not peak.
  4. Queue depth falls back toward zero once the burst passes and the backlog clears.
  5. If bursts are sustained rather than transient, depth keeps rising — the signal to autoscale consumers (→ 95) or admit requests more conservatively upstream (→ 97).

Practical example

(illustrative, via Little's Law → 101). A flash sale produces 2,000 orders in a 10-second burst (200/sec) against a consumer that processes 50 orders/sec steady. In those 10 seconds the consumer clears 50 × 10 = 500 orders, leaving a backlog of 2,000 − 500 = 1,500. Draining that backlog at 50/sec takes 1,500 / 50 = 30 more seconds — a 30-second processing delay for the user, not a dropped order or an overloaded service.

When to use it

  • Use when bursts are expected and temporary, and some processing delay after acceptance is acceptable — the waiting room of Project 16 is this pattern at the front door.
  • Avoid when the caller needs the outcome before returning — then it's a synchronous call (→ 105), sized for peak instead.
  • Failure scenarios: an unbounded queue hides a permanently under-provisioned consumer until storage or latency becomes the real outage — the moment "leveled" becomes "silently growing forever" is exactly what lag monitoring (→ 108) should catch.
  • Simpler alternative: over-provision the consumer for peak directly — simpler, but pays for idle capacity most of the time.

Practice

(pattern — implement a minimal version; 15–20 min, lab Kafka or a Redis list). Simulate a producer bursting 500 messages in 5 seconds into a queue drained by a consumer fixed at 50/sec. Compute by hand (Little's Law, → 101) the expected peak depth and drain time, then measure and compare. Done when measured drain time is within a small margin of the calculation.

Shared lab setup

Key takeaway

A queue converts a burst into a delay instead of an overload, as long as the backlog eventually clears. Done when you can: compute how long a given burst takes to drain at a known consumer rate. Used next in → 111 (Kafka as a durable, high-retention version of this buffer); Project 16 (ticket booking's waiting room), Project 10 (notifications).

References

None — this topic makes no external claims.

Source directory

Search the guide