Implementing Backpressure Patterns
1. Backpressure Pattern
| Aspect | Detail |
|---|---|
| Definition | Slow producer to match consumer capacity |
| Without It | Buffers grow → OOM → cascading failure |
| Mechanisms | Pull-based, credit-based, blocking, drop, sample |
2. Reactive Streams Backpressure
| Mechanism | Detail |
|---|---|
| request(n) | Subscriber tells publisher how many it can take |
| Pull-Based | Publisher only emits up to demand |
| Implementations | Project Reactor, RxJava, Akka Streams |
| Spec | Reactive Streams (JDK 9+ java.util.concurrent.Flow) |
3. Buffer-Based Backpressure
| Aspect | Detail |
|---|---|
| Bounded Buffer | Fixed-size queue between producer and consumer |
| When Full | Producer blocks, drops, or signals slowdown |
| Sizing | Latency × throughput (Little's Law) |
4. Drop Strategies Pattern
| Strategy | When Buffer Full |
|---|---|
| Drop Latest | Reject newest; keep oldest |
| Drop Oldest | Evict oldest; admit new (rolling window) |
| Drop Random | Statistical sampling |
| Drop Low Priority | Class-based eviction |
5. Queue-Based Load Leveling
| Aspect | Detail |
|---|---|
| Pattern | Insert durable queue between burst-prone producer and steady consumer |
| Effect | Smooths bursts; consumer drains at sustainable rate |
| Tools | SQS, RabbitMQ, Kafka |
6. Credit-Based Flow Control
| Aspect | Detail |
|---|---|
| Mechanism | Receiver grants N credits; sender sends ≤ N |
| Refill | Receiver issues more after processing |
| Used In | HTTP/2 flow control, RSocket, AMQP 1.0 |
7. Windowed Backpressure Pattern
| Aspect | Detail |
|---|---|
| Definition | Allow N in-flight messages per window; halt sending until ack |
| Examples | TCP sliding window, Kafka producer max.in.flight |
8. Sampling Pattern
| Approach | Use |
|---|---|
| Head-Based | Decide at request entry (e.g., 1% trace) |
| Tail-Based | Decide after seeing all spans (keep errors) |
| Adaptive | Sample rate adjusts to load |
| Use Case | Telemetry, logs at high volume |
9. Load Shedding Pattern
| Aspect | Detail |
|---|---|
| Trigger | Health metric threshold breached |
| Action | Reject excess requests with 503 |
| Prioritization | Drop low-priority first; protect health checks |
| Recovery | Resume admission as load drops |
10. Admission Control Pattern
| Aspect | Detail |
|---|---|
| Definition | Decide before accepting whether system can handle the request |
| Inputs | Predicted cost, current capacity, priority |
| Reject Early | Better than accepting and timing out later |