What is backpressure, and how do you implement it in a reactive system?
Backpressure refers to the requirement that when a consumer cannot keep up with the rate a producer is generating data, the producer must be slowed down rather than either silently dropping data or letting unbounded data pile up in memory. There are several ways to implement this. The simplest is a blocking put(): calling BlockingQueue.put() blocks the producer automatically once the queue is full, giving natural backpressure with almost no extra code. In reactive streams frameworks like Project Reactor, the subscriber explicitly signals how much data it's ready to receive by calling request(N), and the publisher only ever sends up to that many items at a time; when the subscriber is busy, it simply doesn't request more, and the producer naturally slows to match. Thread pool executors can use a CallerRunsPolicy, where once the pool and its queue are full, the calling thread itself is forced to execute the task, which prevents it from submitting new work any faster than it can actually be processed. At the network layer, TCP's own sliding window provides a similar form of flow control. In every case, backpressure propagates upstream from the slowest, most overwhelmed consumer through the entire pipeline back toward the original producer.
Ready to master this question?
Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.
Sign in to generate a response