Working with Backpressure
1. Understanding Backpressure Mechanisms
| Mechanism | Detail |
|---|---|
| Block sender | TCP zero-window; Java BlockingQueue |
| Drop / shed | Discard excess (UDP, load shedding) |
| Buffer + spill | Disk overflow when memory full |
| Reactive request(n) | Pull-based credit (Reactive Streams) |
| Adaptive throttle | Rate adjusts to observed latency |
2. Implementing Reactive Streams
| Interface | Method |
|---|---|
| Publisher | subscribe(Subscriber) |
| Subscriber | onSubscribe, onNext, onError, onComplete |
| Subscription | request(n), cancel() |
| Implementations | Project Reactor, RxJava, Akka Streams, Mutiny |
3. Implementing Flow Control
| Layer | Mechanism |
|---|---|
| TCP | Sliding window, zero-window probe |
| HTTP/2 | Per-stream + connection windows |
| gRPC | Inherits HTTP/2 flow control |
| Application | Semaphores, request(n) |
4. Implementing Queue-Based Backpressure
| Strategy | Detail |
|---|---|
| Bounded queue | Block / reject when full |
| Drop oldest | Ring buffer; favors recent |
| Drop newest | Preserve in-flight |
| Spill to disk | Slow but no loss |
5. Implementing Rate Limiting for Backpressure
| Pattern | Detail |
|---|---|
| Producer limit | Cap requests/sec at source |
| Consumer-driven | Reactive pull rate |
| Token bucket per consumer | Fair share |
| Dynamic adjustment | AIMD style |
6. Implementing Admission Control
| Method | Detail |
|---|---|
| Concurrency limit | Reject when in-flight ≥ limit |
| Adaptive (Vegas / Gradient) | Limit auto-tuned by latency derivative |
| Priority queue | Drop low-priority first |
| Library | Netflix concurrency-limits |
7. Handling Unbounded Queues
| Risk | Mitigation |
|---|---|
| OOM | Replace with bounded queue |
| Latency growth (Little's Law) | Cap at 1-2× steady-state |
| Hidden backlog | Monitor depth as SLI |
8. Implementing Bounded Queues
| Implementation | Detail |
|---|---|
| ArrayBlockingQueue | Java; FIFO, fixed capacity |
| LinkedBlockingQueue(N) | Bounded variant |
| Disruptor | Lock-free ring buffer |
| channels (Go) | Buffered with capacity |
9. Understanding Push vs Pull Models
10. Implementing Backpressure Propagation
| Hop | Mechanism |
|---|---|
| End-to-end | Each layer signals upstream when saturated |
| HTTP 429 / 503 | Retry-After header |
| gRPC RESOURCE_EXHAUSTED | Status code |
| Kafka producer block.ms | Block when buffer full |