Backpressure
See a full queue tell senders to slow down instead of growing without end.
Explain it simply
Each mark in the lanes is one request. The teal bars show the work that arrives each second. The ochre line shows what the four replicas can finish. During the flood, work arrives five to six times faster than it leaves, so the extra work must wait somewhere.
Three lanes of request marks for the keys K1, K2 and K3. From 10 s to 20 s, the work bars pass the capacity line.
Work arrives faster than the four replicas can serve it.
Show the code ▾Hide the code ▴ · Try it yourself · Python 3
import heapq
# (id, key, arrival in s, tokens of work = prompt + output)
REQUESTS = [
("R01", "K1", 1.00, 728), ("R02", "K1", 3.00, 614), ("R03", "K2", 4.00, 1590), ("R04", "K2", 4.25, 1750),
("R05", "K2", 4.50, 1796), ("R06", "K2", 4.75, 1741), ("R07", "K1", 5.00, 421), ("R08", "K2", 5.00, 1843),
("R09", "K2", 5.25, 1784), ("R10", "K1", 7.00, 855), ("R11", "K1", 9.00, 565), ("R12", "K3", 10.10, 7324),
("R13", "K3", 10.35, 7477), ("R14", "K3", 10.60, 7358), ("R15", "K3", 10.85, 7065), ("R16", "K1", 11.00, 847),
("R17", "K3", 11.10, 7092), ("R18", "K3", 11.35, 7345), ("R19", "K3", 11.60, 7093), ("R20", "K3", 11.85, 7336),
("R21", "K3", 12.10, 7327), ("R22", "K3", 12.35, 7331), ("R23", "K3", 12.60, 7035), ("R24", "K3", 12.85, 7424),
("R25", "K1", 13.00, 826), ("R26", "K3", 13.10, 7377), ("R27", "K3", 13.35, 7097), ("R28", "K3", 13.60, 7322),
("R29", "K3", 13.85, 7383), ("R30", "K3", 14.10, 7359), ("R31", "K3", 14.35, 7008), ("R32", "K3", 14.60, 7223),
("R33", "K3", 14.85, 7065), ("R34", "K1", 15.00, 449), ("R35", "K3", 15.10, 7051), ("R36", "K3", 15.35, 7495),
("R37", "K3", 15.60, 7205), ("R38", "K3", 15.85, 7184), ("R39", "K3", 16.10, 7463), ("R40", "K3", 16.35, 7495),
("R41", "K3", 16.60, 7188), ("R42", "K3", 16.85, 7447), ("R43", "K1", 17.00, 569), ("R44", "K3", 17.10, 7489),
("R45", "K3", 17.35, 7129), ("R46", "K3", 17.60, 7296), ("R47", "K3", 17.85, 7476), ("R48", "K3", 18.10, 7319),
("R49", "K3", 18.35, 7489), ("R50", "K3", 18.60, 7051), ("R51", "K3", 18.85, 7420), ("R52", "K1", 19.00, 535),
("R53", "K2", 19.00, 1704), ("R54", "K3", 19.10, 7311), ("R55", "K2", 19.25, 1904), ("R56", "K3", 19.35, 7404),
("R57", "K2", 19.50, 1708), ("R58", "K3", 19.60, 7103), ("R59", "K2", 19.75, 1713), ("R60", "K3", 19.85, 7453),
("R61", "K2", 20.00, 1757), ("R62", "K2", 20.25, 1589), ("R63", "K1", 21.00, 409), ("R64", "K1", 23.00, 460),
("R65", "K1", 25.00, 757), ("R66", "K1", 27.00, 605), ("R67", "K1", 29.00, 836),
]
REPLICAS, RATE = 4, 1500 # 4 replicas, 1,500 tokens of work a second each
LIMIT = 4 # most requests in the server's queue (None: no limit)
POLICY = "reject" # "reject": HTTP 503 at once; "pause": the request waits at the client
def simulate(limit, policy):
free = [0.0] * REPLICAS # when each replica is free
queue, paused, wait, out = [], [], {}, {}
events = [(t, 1, rid, w) for rid, _, t, w in REQUESTS] # arrivals; a finish (kind 0) sorts first
heapq.heapify(events)
room = lambda: limit is None or len(queue) < limit
def pump(now): # idle replicas take the queue head; paused requests fill the queue
while True:
idle = [j for j in range(REPLICAS) if free[j] <= now]
if queue and idle:
rid, w, t0 = queue.pop(0)
free[idle[0]] = now + w / RATE
wait[rid] = now - t0
heapq.heappush(events, (free[idle[0]], 0, None, None))
elif paused and room():
queue.append(paused.pop(0))
else:
return
while events:
now, kind, rid, w = heapq.heappop(events)
if kind == 1: # an arrival
if paused or not room():
if policy == "reject" and not paused:
out[rid] = 503
else:
paused.append((rid, w, now))
else:
queue.append((rid, w, now))
pump(now)
return wait, out
wait, out = simulate(LIMIT, POLICY)
keys = {}
for rid, key, t, w in REQUESTS:
s, n = keys.get(key, (0, 0))
keys[key] = (s + (rid in wait), n + 1)
for key, (s, n) in sorted(keys.items()):
print(key, f"served {s}/{n}")
# K1 served 12/15
# K2 served 7/12
# K3 served 12/40
print("HTTP 503:", len(out))
# HTTP 503: 36
print("longest wait:", round(max(wait.values()), 1), "s")
# longest wait: 4.6 s
print("R25:", f"waits {wait['R25']:.1f} s" if "R25" in wait else "HTTP 503")
# R25: HTTP 503
Copy it into a file and run it. The requests are the toy's 67 (key, arrival s, tokens of work).
Common mix-ups
MYTHA bigger buffer absorbs the overload.
FIXA buffer only stores work. With no limit, the queue reaches 41 requests and R63 waits 39.7 s.
MYTHPausing the sender makes requests finish sooner.
FIXPausing keeps every request, but R25 still waits 11.9 s. Of that, 7.1 s is at the client: the wait moves upstream.
Takeaways
- Backpressure tells the sender to slow down when the next stage is full.
- A bounded queue keeps the server's queue short; extra requests fail fast or wait upstream.
- Backpressure moves or refuses work; it never adds capacity.