Working with Backpressure

1. Understanding Backpressure Mechanisms

MechanismDetail
Block senderTCP zero-window; Java BlockingQueue
Drop / shedDiscard excess (UDP, load shedding)
Buffer + spillDisk overflow when memory full
Reactive request(n)Pull-based credit (Reactive Streams)
Adaptive throttleRate adjusts to observed latency

2. Implementing Reactive Streams

InterfaceMethod
Publishersubscribe(Subscriber)
SubscriberonSubscribe, onNext, onError, onComplete
Subscriptionrequest(n), cancel()
ImplementationsProject Reactor, RxJava, Akka Streams, Mutiny

3. Implementing Flow Control

LayerMechanism
TCPSliding window, zero-window probe
HTTP/2Per-stream + connection windows
gRPCInherits HTTP/2 flow control
ApplicationSemaphores, request(n)

4. Implementing Queue-Based Backpressure

StrategyDetail
Bounded queueBlock / reject when full
Drop oldestRing buffer; favors recent
Drop newestPreserve in-flight
Spill to diskSlow but no loss

5. Implementing Rate Limiting for Backpressure

PatternDetail
Producer limitCap requests/sec at source
Consumer-drivenReactive pull rate
Token bucket per consumerFair share
Dynamic adjustmentAIMD style

6. Implementing Admission Control

MethodDetail
Concurrency limitReject when in-flight ≥ limit
Adaptive (Vegas / Gradient)Limit auto-tuned by latency derivative
Priority queueDrop low-priority first
LibraryNetflix concurrency-limits

7. Handling Unbounded Queues

RiskMitigation
OOMReplace with bounded queue
Latency growth (Little's Law)Cap at 1-2× steady-state
Hidden backlogMonitor depth as SLI

8. Implementing Bounded Queues

ImplementationDetail
ArrayBlockingQueueJava; FIFO, fixed capacity
LinkedBlockingQueue(N)Bounded variant
DisruptorLock-free ring buffer
channels (Go)Buffered with capacity

9. Understanding Push vs Pull Models

Push

  • Producer sends as ready
  • Lower latency
  • Risk: overwhelm consumer
  • Examples: WebSocket, SSE

Pull

  • Consumer requests when ready
  • Natural backpressure
  • Higher latency
  • Examples: Kafka poll, Reactive Streams

10. Implementing Backpressure Propagation

HopMechanism
End-to-endEach layer signals upstream when saturated
HTTP 429 / 503Retry-After header
gRPC RESOURCE_EXHAUSTEDStatus code
Kafka producer block.msBlock when buffer full