Implementing Backpressure Patterns

1. Backpressure Pattern

AspectDetail
DefinitionSlow producer to match consumer capacity
Without ItBuffers grow → OOM → cascading failure
MechanismsPull-based, credit-based, blocking, drop, sample

2. Reactive Streams Backpressure

MechanismDetail
request(n)Subscriber tells publisher how many it can take
Pull-BasedPublisher only emits up to demand
ImplementationsProject Reactor, RxJava, Akka Streams
SpecReactive Streams (JDK 9+ java.util.concurrent.Flow)

3. Buffer-Based Backpressure

AspectDetail
Bounded BufferFixed-size queue between producer and consumer
When FullProducer blocks, drops, or signals slowdown
SizingLatency × throughput (Little's Law)

4. Drop Strategies Pattern

StrategyWhen Buffer Full
Drop LatestReject newest; keep oldest
Drop OldestEvict oldest; admit new (rolling window)
Drop RandomStatistical sampling
Drop Low PriorityClass-based eviction

5. Queue-Based Load Leveling

AspectDetail
PatternInsert durable queue between burst-prone producer and steady consumer
EffectSmooths bursts; consumer drains at sustainable rate
ToolsSQS, RabbitMQ, Kafka

6. Credit-Based Flow Control

AspectDetail
MechanismReceiver grants N credits; sender sends ≤ N
RefillReceiver issues more after processing
Used InHTTP/2 flow control, RSocket, AMQP 1.0

7. Windowed Backpressure Pattern

AspectDetail
DefinitionAllow N in-flight messages per window; halt sending until ack
ExamplesTCP sliding window, Kafka producer max.in.flight

8. Sampling Pattern

ApproachUse
Head-BasedDecide at request entry (e.g., 1% trace)
Tail-BasedDecide after seeing all spans (keep errors)
AdaptiveSample rate adjusts to load
Use CaseTelemetry, logs at high volume

9. Load Shedding Pattern

AspectDetail
TriggerHealth metric threshold breached
ActionReject excess requests with 503
PrioritizationDrop low-priority first; protect health checks
RecoveryResume admission as load drops

10. Admission Control Pattern

AspectDetail
DefinitionDecide before accepting whether system can handle the request
InputsPredicted cost, current capacity, priority
Reject EarlyBetter than accepting and timing out later