Backpressure

What it is

Backpressure is information travelling upstream when work is flowing
downstream. A consumer that can process only a few more items lets
the producer know how much it can accept, and the producer slows or
stops. In a demand-driven stream this may be an explicit request for
a number of items. In other systems a bounded channel blocks a
sender until space opens, or a task stops reading from a connection
when its local work queue reaches a limit. The visible behaviour
differs. In all three, the receiving stage constrains how much
additional work the preceding stage can hand over.

The Reactive Streams JVM specification makes the allowance concrete.
A subscriber requests a positive number of elements through its
subscription. A compliant publisher stays within the aggregate
requested quantity. If the subscriber asks for eight and receives
five, three units of demand remain. The same accounting applies
across asynchronous stages. The code responsible for rendering,
storing or otherwise using a value can decide when to ask for more.
A buffer may absorb small differences in timing inside the publisher
or between stages. Demand and the buffer have separate jobs. The
former constrains the work admitted, while the latter holds work
already admitted.

Some producers can stop producing at source. Others, such as a feed
delivering events from a physical device, may keep generating
whether a consumer can read them or not. The boundary may then apply
pressure to a listener that accepts connections or pulls from a
durable log. The device has its own limits. Every design needs to
identify where the signal actually reaches and what the system does
if input continues beyond that point.

Why it matters

A rate mismatch shows up in space and time before it shows up as an
outage. If data arrives at 600 items per second and one stage
reliably completes 400, the queue adds roughly 200 items each second
for as long as the mismatch lasts. Twelve thousand items accumulate
in a minute. Even with enough memory, the last item waits behind
nearly thirty seconds of work at the same processing rate. A
successful enqueue or a fast acknowledgement at the front may make
that delay invisible to a latency chart measuring only the intake
request.

The business decision is how much outstanding work to accept on
somebody's behalf. An image pipeline may tolerate a short wait
because the result is displayed later. A control message in a
customer-facing operation may be useless after a short deadline.
Different constraints lead to different queue lengths, time limits
and admission policies. Counting objects alone is rarely enough. One
small metric sample and one multi-megabyte record occupy one slot
each. The appropriate limit might combine a maximum number of tasks,
an estimate of bytes and the age of the oldest item.

A pressure signal also helps locate a bottleneck. When storage slows
down, the stage just before it may be the first to run out of
capacity. Earlier stages can then spend time waiting instead of
consuming large amounts of memory. The Apache Flink monitoring
documentation describes interpreting time spent busy, idle and
backpressured along a pipeline. The useful operational question is
which downstream resource is keeping the system from clearing work
and under which traffic conditions it happens.

What it looks like in practice

Imagine a document conversion service. The upload endpoint stores
files and submits jobs, a worker renders them, and an exporter
writes PDFs to durable storage. Start by deciding what a successful
response to the upload promises. If it promises durable admission,
the job must survive even if the service restarts before processing
it. Reserve a small number of rendering slots per worker, then give
the worker at most a modest queue for files ready to be rendered. It
should stop pulling additional jobs when its local slots and queue
are full. A small amount of prefetch can keep renderers busy while a
job arrives, provided it is included in memory and cancellation
budgets.

Next define the policy at intake. A brief spike can wait in the
recorded queue. A prolonged burst may exceed the maximum queue age.
Return an explicit busy response for work the service declines to
accept and tell the caller how to retry, if that is part of the API
contract. Once a document is accepted, leave its state visible to
the caller so retries do not quietly create duplicate work. Separate
the capacity spent on large conversions from interactive jobs so a
single file class cannot consume every slot.

Measure the service while storage is deliberately slowed in a test.
Watch the combined bytes held by renderers and the age distribution
of accepted jobs. Ensure the signal reaches the worker intake and
does not simply hide an extra queue in a client library. Repeating
the test after storage recovers should show queued work draining,
with temporary concessions accounted for rather than left in place.

Engineer ApS's case study on moving slow work onto a River queue
explains why background work is made durable in PostgreSQL and why
the operator needs a visible retry history. It also identifies the
storage system as a practical capacity limit. The conversion service
above is an illustrative extension. It shows where to put the
separate control for how much work workers pull from a durable
queue.

Where it goes wrong

An unlimited batch request bypasses the capacity decision. A worker
might fetch thousands of jobs from the durable queue and place them
in memory while its throughput stays fixed. Count each reserved item
against a known limit and include prefetched and partially completed
work. Inspect heap usage and queue age under load, rather than
judging capacity from successful fetches.

A bounded queue can conceal an unbounded second queue. If every
rejected request immediately retries, each dropped attempt generates
another attempt during the busiest period. Use bounded retries with
increasing delays and randomisation where retries are appropriate.
Include retries in the same admission budget as fresh work, and
report sustained overload to the people who can change capacity or
the request mix.

Teams often retain all event classes under one policy. Keeping every
progress update from a dropped connection may prevent a later
important result from arriving. Choose delivery and retention rules
by business meaning. A fresh progress value can replace an older one
if the contract permits it. An accepted financial instruction needs
a durable record and a failure path an operator can trace.

Finally, insist on a way for waiting to end. A downstream
cancellation, process shutdown or missed deadline should release
credits and resources held for that job. Otherwise pressure remains
in the system after the reader has left. A test that holds storage
indefinitely and cancels clients can tell whether the clean-up path
really restores capacity.

Further reading

  * Reactive Streams specification for the JVM defines how
publishers and subscribers account for demand.

  * Java Flow subscription API documents request and cancellation in
the JDK.

  * Apache Flink backpressure monitoring explains signs of
downstream congestion in a running job.

hThis page on the web	URL:https://engineer.company/glossary/backpressure/	engineer.company	70
