Resilience & Service PatternsLoad Shedding & BackpressureMedium

Backpressure: Signaling Upstream to Slow Down

Definition
Backpressure is a flow control mechanism where consumers signal producers to slow down when overwhelmed, preventing buffer overflow by matching production rate to consumption capacity.

Load Shedding vs Backpressure

Load shedding drops excess work. Backpressure slows upstream producers. Shedding discards data; backpressure preserves it by controlling flow. A queue with shedding drops messages when full. The same queue with backpressure tells producers to stop until consumers catch up. Shedding works when data loss is acceptable (telemetry). Backpressure is necessary when every message matters (financial transactions).

⚠️ Key Trade-off: Backpressure preserves data but increases latency. Shedding loses data but maintains low latency. Choose based on whether latency or completeness matters more.

Backpressure Propagation

Backpressure must propagate through the entire chain. If Service C is slow, it signals B to slow down, B signals A, and clients receive 429 Too Many Requests with Retry-After headers. Without propagation, pressure accumulates in the middle, causing fragile components to fail.

Bounded Queues

The critical implementation detail is bounded queues. Unbounded queues hide backpressure by growing until memory exhausts causing OOM (Out of Memory) crashes. A bounded queue with 1000 items capacity forces blocking when full, naturally slowing the pipeline. Size for 2-5 seconds of burst capacity at expected throughput.

Implementation Mechanisms

HTTP services signal via 429 with Retry-After. Kafka lets consumers control read rate. TCP provides backpressure through window sizing. Credit based flow control grants producers permission to send: consumer grants 100 credits, each message consumes one, at zero credits producers wait.

💡 Key Takeaways
Backpressure slows producers to match consumer capacity; load shedding drops excess work
Backpressure must propagate through entire service chain to be effective
Bounded queues are essential - unbounded queues hide backpressure until OOM
📌 Interview Tips
1Explain when to use each: backpressure for data that cannot be lost, shedding for optional data
2Mention specific mechanisms: HTTP 429 with Retry-After, Kafka consumer-controlled reads, TCP windows
3Interviewers love hearing about bounded vs unbounded queues and their memory implications
← Back to Load Shedding & Backpressure Overview