Skip to content
AgenticAIWiki
  1. Learn
  2. Part 13
  3. 68. Serving patterns
  4. 68.2

Lesson 68.2 · 2 of 5 · 4 min read · Reviewed 24 Nov 2026

Queues, workers and back-pressure

By the end, you can

  • Design queues and workers for model calls.
  • Apply back-pressure when demand exceeds capacity.

Model calls are slow and rate limited. Putting a queue in front of them changes how the system behaves under load.

The shape

API --> queue --> workers (N) --> model API
          ^           |
          |           +--> results store --> webhook or poll
     reject or defer when too deep
  • Queue: holds jobs durably. A worker crash doesn’t lose work.
  • Workers: each pulls a job, runs it, writes the result, acknowledges. Their number is limited by your rate limitRate limit: A cap on how many requests or tokens you can use in a period, such as per minute., not only by CPU.
  • Results store: keyed by job id.

Back-pressure

When the queue grows faster than workers drain it, you must choose:

Option When
Reject new work with a clear “busy, try later” Low priority or retryable by the caller
Defer to batch or off-peak Not time sensitive
Degrade: smaller model, shorter answer Some answer beats none
Shed oldest low-priority jobs Very stale work is worthless anyway

Set a maximum queue age. A support reply delivered two hours late may be worse than a quick “we’re busy” message.

Visibility

Chart queue depth, oldest job age and worker utilisation. Queue depth rising steadily is your earliest warning, long before users notice.

Key points

  • A queue between your API and your workers smooths bursts and lets work survive restarts.
  • Workers pull jobs at the rate your rate limits and budget allow.
  • Back-pressure means saying 'not now' early (reject, defer or degrade) instead of letting everything get slow.
Hands-on labRuns in the Playground

Add back-pressure to a burst queue

Show how deferring low-priority work early keeps interactive waits short during a burst.

You will have: A queue simulation that compares worst interactive wait with and without back-pressure.

Steps

  1. Run the starter: both runs give the same long worst wait.
  2. Defer background jobs once the queue is at max_depth.
  3. Serve interactive jobs before background ones.
  4. Compare worst wait, jobs still queued and jobs deferred.
  5. Try max_depth values of 60 and 600 and explain the trade-off.

Done when

  • Without back-pressure the worst interactive wait is tens of minutes.
  • With back-pressure it drops to 0 or 1 minute.
  • The deferred count shows the price: background work pushed to later.

Open in the Playground No API key needed.

Starter code (Python)
# Back-pressure: during a burst, defer background work early so interactive users stay fast.
CAPACITY = 60  # jobs per minute the rate limit allows


def arrivals(minute):
    total = 180 if 10 <= minute < 30 else 50
    interactive = total * 3 // 10
    return interactive, total - interactive


def simulate(back_pressure, max_depth=120):
    queue, waits, deferred = [], [], 0
    for minute in range(60):
        inter, bg = arrivals(minute)
        queue += [(minute, "interactive")] * inter
        for _ in range(bg):
            # TODO: with back_pressure, defer background jobs once the queue reaches max_depth
            queue.append((minute, "background"))
        # TODO: with back_pressure, serve interactive jobs first
        served, queue = queue[:CAPACITY], queue[CAPACITY:]
        waits += [minute - t for t, kind in served if kind == "interactive"]
    return max(waits), len(queue), deferred


for bp in (False, True):
    worst, left, deferred = simulate(bp)
    label = "with back-pressure" if bp else "no back-pressure"
    print(f"{label:<19} worst interactive wait {worst} min, still queued {left}, deferred {deferred}")
Show a solution
# Back-pressure: during a burst, defer background work early so interactive users stay fast.
CAPACITY = 60  # jobs per minute the rate limit allows


def arrivals(minute):
    total = 180 if 10 <= minute < 30 else 50
    interactive = total * 3 // 10
    return interactive, total - interactive


def simulate(back_pressure, max_depth=120):
    queue, waits, deferred = [], [], 0
    for minute in range(60):
        inter, bg = arrivals(minute)
        queue += [(minute, "interactive")] * inter
        for _ in range(bg):
            if back_pressure and len(queue) >= max_depth:
                deferred += 1  # say "not now": retry later or tomorrow's batch
            else:
                queue.append((minute, "background"))
        if back_pressure:  # interactive work first
            queue.sort(key=lambda j: (j[1] != "interactive", j[0]))
        served, queue = queue[:CAPACITY], queue[CAPACITY:]
        waits += [minute - t for t, kind in served if kind == "interactive"]
    return max(waits), len(queue), deferred


for bp in (False, True):
    worst, left, deferred = simulate(bp)
    label = "with back-pressure" if bp else "no back-pressure"
    print(f"{label:<19} worst interactive wait {worst} min, still queued {left}, deferred {deferred}")
Quick checkTraffic spikes far above your rate limit for an hour. What is the best behaviour?
How sure are you?
Pick an answer and how sure you are