Skip to content
BytePatterns

Backpressure

Concurrency: lesson 11 of 15

An unbounded queue is not a buffer, it is a delayed crash.

Lesson 11 of 15 · 5 min

Backpressure

Step 1 of 9

A fast producer, a slow consumer, and a belt between them that holds exactly two.

The Idea

Backpressure is the signal that travels backwards when the consumer cannot keep up. A bounded queue creates it for free: once full, the producer blocks, fails, or drops. Unbounded queues delete that signal, so latency and memory grow until something dies.

Real-World Example

A bakery with one wrapper and a two-tray shelf. When both trays are full the baker stops pulling loaves out of the oven. Remove the shelf limit and the kitchen fills with cooling bread nobody can wrap.

The Code

import queue, threading, time
belt = queue.Queue(maxsize=2)          # the bound IS the backpressure
placed = shed = 0

def wrap():                            # slow consumer
    for _ in range(3):
        time.sleep(0.08); belt.get()

t = threading.Thread(target=wrap); t.start()
for loaf in range(6):                  # fast producer
    try:
        belt.put(loaf, timeout=0.05); placed += 1
    except queue.Full:
        shed += 1                      # refuse work instead of hoarding it
t.join()
print(placed + shed == 6, shed > 0)    # True True

Python

Your turn

What does this print?

import queue
q = queue.Queue(maxsize=2)
q.put("a"); q.put("b")
try:
  q.put("c", block=False)
  print("accepted", q.qsize())
except queue.Full:
  print("Full", q.qsize())

Mini quiz

1 / 3

An unbounded queue in front of a slow consumer mainly hides:

New lessons land every few weeks

Leave an address and we will tell you when the next one is up. That is the only reason we will use it.

One address, stored so we can email you. Nothing else, ever.