InfraSim system design guide

Queue backpressure

The workers slowed to a quarter speed. Shoppers noticed nothing: availability stayed at 100%. The backlog grew by about 213 jobs a second.

A queue lets the part of your system that creates work keep going while the part that does the work catches up. That is the point of it. It is also the danger: when the consumers fall behind, the queue absorbs the difference quietly, and everything upstream looks healthy while the work gets later and later.

Backpressure is the opposite instinct. It means letting the slowness of the consumer flow back to the producer: fewer new jobs accepted, callers told to slow down, or work deliberately dropped, instead of a backlog that grows until something gives.

The experiment

A gift shop takes 600 requests a second. Every order queues about two gift-wrap jobs (paper, ribbon, a card), and four small workers pull them from the queue. On a normal day they wrap about 292 jobs a second and the queue stays empty.

Thirty seconds in, a new card template jams the workers and each one finishes a quarter of the jobs it used to. Orders keep arriving. We ran four versions with the same seed.

Jobs waiting in the queue

  • 4 workers
  • 4 workers, queue capped at 5,000
  • Workers scale on queue depth
0 5,000 10,000 15,000 20,000 0s 30s 60s 90s 120s Seconds into the run (workers slow down at 30)
InfraSim, Advent of Outages door 5, seed 261205. Sampled every 5 simulated seconds.
WorkersJobs done / sDeepest backlogWait for a new jobJobs dropped
4 (as shipped)7919,129231.9 s0
4, queue capped at 5,000795,00060.6 s14,129
16, all the time29200 s0
4, scaling on depth to 203092,76626.1 s0

Jobs done is the rate once the jam had settled in. Wait for a new job is how long the backlog at its deepest would take to clear at the speed the workers were going: how late a job joining the queue then would be. Shoppers saw 100% availability and a p95 of 42 to 43 ms in every version.

Nobody noticed

That last line is the important one. With four workers stuck at a quarter speed, the shop kept taking orders at 100% availability. Every dashboard about requests was green. Meanwhile the workers were finishing 79 jobs a second against 292 arriving, the backlog grew by about 213 jobs every second, and by the end of the run a job joining the queue faced a wait of 231.9 seconds, with the number still climbing.

A queue does not fix a shortfall in its consumers. It converts it into delay, and delay does not page anyone unless you measure it.

Watch age, not depth

A depth of 19,129 sounds alarming, but it means nothing without knowing how fast the queue drains: 19,129 jobs is a few seconds of work for a big fleet and an afternoon for a small one. How long the work waits is the number that answers the real question, which is how late the work is. Alert on the age of the oldest message, or on depth divided by how fast it drains.

Capping the queue

A maximum depth is backpressure in its bluntest form. Capped at 5,000, the queue stopped growing and the wait for a new job never got past 60.6 seconds. It got there by throwing away 14,129 gift-wrap jobs, and the workers were still finishing 79 a second.

That can be the right call. A bounded queue keeps a slow-motion outage from eating all your memory, and some work is worthless once it is late. But it is a decision about what to lose, and it is better made on purpose. In InfraSim a full queue drops new jobs. Real systems often push back one step further, by refusing or slowing new orders while the backlog is deep, so the loss is visible to whoever created the work.

More consumers

Enough for the bad day. Sixteen workers at a quarter speed do the work of four normal ones, so the backlog never formed. They also cost $1,273 a month against $937 for the original shop, every month, to cover a jam that may never happen.

Scale on queue depth. Starting from four and adding workers as the queue grew, the backlog peaked at 2,766 jobs and a 26.1 second wait before twenty workers cleared it, 45 seconds after the jam began. The bill was $957 a month on a normal day and $1,320 while the jam lasted. Scale consumers on depth or age, not on how busy the producers look: the shop servers were fine the whole time.

Where a dead-letter queue fits

A dead-letter queue is where a message goes after it has failed too many times, so one bad job stops being retried forever and stops taking worker time from the good ones. It is for jobs that will never succeed. It would not have helped here: these jobs were fine, the workers were just slow. Knowing which of those two problems you have is most of the diagnosis.

About these numbers

They describe InfraSim's simulation, which models jobs as flows per tick rather than one by one, with a fixed seed so every run repeats exactly. Costs are the game's simulated monthly figures, not any provider's prices.

Break it yourself

This is Advent of Outages door 5, "Wrapped Up": the same shop and the same jammed workers, free in your browser with no account. The door opens on 5 December and stays open after.

In the InfraSim app, The Queue Grows (chapter 4, Asynchronous Systems) starts with a backlog that has been growing since the run began, while request latency looks wonderful.

New to InfraSim? The first five campaign levels play in your browser right now. No install, no account.

Get InfraSim: System Design Game

InfraSim is a system-design game: drag load balancers, caches, databases and queues onto a canvas, deploy, and watch simulated live traffic flow through what you built. Then survive traffic spikes, zone outages and bot swarms across a guided campaign, endless Survival runs and a Daily Challenge. Tutorial and 15 levels free, with optional InfraSim Pro.

More about InfraSim