Senior LevelSoftware Engineering Principles
How does Backpressure work in Reactive Streams to prevent subscriber memory exhaustion?
❓ Interview Prompt: Explain Backpressure, Reactive Streams specification (`Publisher`, `Subscriber`, `Subscription.request(n)`), push vs pull-based flow control, and rate matching.
Backpressure is a feedback mechanism in Reactive Streams that allows a fast data Subscriber to signal to a fast data Publisher how much data it can handle, preventing out-of-memory crashes:
Reactive Streams Protocol:
Instead of the Publisher pushing data blindly, the Subscriber explicitly requests chunks of items using 'Subscription.request(n)':
•
Flow: Subscriber calls 'subscription.request(10)'. Publisher emits up to 10 items and then pauses until the Subscriber requests more items!
Code Implementation Example
typescript
// Reactive Stream Subscriber requesting backpressure chunk
class BoundedSubscriber {
onSubscribe(subscription: any) {
this.subscription = subscription;
this.subscription.request(100); // ✅ Backpressure: Request only 100 items at a time!
}
onNext(item: any) {
this.process(item);
this.subscription.request(1); // Request next item after processing
}
}💡Handling stream backpressure using explicit subscription request signaling.
🎯 Interview Key Takeaway
Implement Backpressure in streaming data pipelines to prevent fast producers from overwhelming downstream consumers.
#Backpressure#Reactive Streams#Flow Control#Senior
Back to all Software Engineering Principles Questions→