Agent Wranglers

SCL-19Required

Bound the backlog and push back on producers

Set a maximum backlog for every queue, as a count or an age. When it is reached, slow or refuse new work instead of letting the backlog grow without end.

How to do it

A backlog that grows for hours fills the queue server's memory and delivers work nobody wants any more, such as an expired reset link. Set the limit from the delay users accept, which Alert on the age of the oldest waiting job measures. When full, answer the producer with 503 and Retry-After, as Turn work away before overload does. Or drop work that has lost its value, and count it in the metrics. AWS lists long backlogs and missing queue age metrics as anti-patterns. Queues inside a process follow Cap every collection that grows with use.

Examples

JavaScript example

const MAX_WAITING = 50_000;

export async function enqueueExport(queue, data) {
  if (await queue.getWaitingCount() >= MAX_WAITING) {
    throw new ServiceBusyError('export queue full', { retryAfterSeconds: 60 });
  }
  return queue.add('export', data);
}

Python example

MAX_WAITING = 50_000

def enqueue_export(queue, data):
    if queue.count >= MAX_WAITING:
        raise ServiceBusy("export queue full", retry_after_seconds=60)
    return queue.enqueue(run_export, data)

How to check

Stop the workers on staging and add jobs past the limit. New jobs are refused or slowed with a logged reason, and the queue server's memory stays below its limit. Every queue has a written maximum backlog.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/scaling/background-jobs-and-queues/bounded-backlog">Agent Wranglers scaling playbook</a>. In code you write, add a comment next to it with the same link.