Quick revision
The short answer
Blocking queues provide backpressure by blocking the producer's thread, which works well inside one process where waiting threads are cheap enough. Asynchronous, non-blocking systems cannot block threads, so Reactive Streams and java.util.concurrent.Flow make the consumer signal demand, request(n), and the producer may send only that many items. Between services the same idea appears as consumer-driven pull, credit-based flow control and bounded broker partitions, and every flow needs an explicit answer to what happens when the consumer is slower than the producer.
What to remember
In this answer, we saw that blocking queues provide backpressure by blocking the producer's thread, which suits threads in one process. Non-blocking systems cannot block, so Reactive Streams and Java 9's Flow let the consumer signal demand with request(n), and the producer sends no more than requested. Between services the same idea appears as consumer pull in Kafka, prefetch credits in RabbitMQ and flow-control windows in HTTP/2 and TCP. Without backpressure, buffers grow until something fails, so every flow needs an explicit, measured answer to what happens when the consumer is slower.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Blocking queues provide backpressure by blocking the producer's thread, which works well inside one process where waiting threads are cheap enough. Asynchronous, non-blocking systems cannot block threads, so Reactive Streams and java.util.concurrent.Flow make the consumer signal demand, request(n), and the producer may send only that many items. Between services the same idea appears as consumer-driven pull, credit-based flow control and bounded broker partitions, and every flow needs an explicit answer to what happens when the consumer is slower than the producer.
Why this question is asked
Java's queue APIs tell a story about how we build systems. Vector and Stack were simple synchronized containers. Java 5 and 6 brought Queue, Deque and the BlockingQueue family for thread pools and producer-consumer code. Java 9 added Flow, a small set of interfaces from the Reactive Streams specification, where the consumer tells the producer how much it can take. Interviewers ask senior engineers about this because the underlying question, what happens when a consumer is slower than a producer, decides how systems behave under overload. In this answer, we will see how blocking queues create backpressure, why non-blocking systems needed a different mechanism, how Flow's demand signalling works with code, how the same idea appears between services, and what it means for designing flows.
1Backpressure by Blocking a Thread
Backpressure is any mechanism that makes a fast producer slow down to the pace of a slow consumer, instead of letting work pile up. A bounded BlockingQueue does it in the simplest possible way: when the queue is full, put blocks the producer's thread until the consumer takes something. The producer does not need to know anything about the consumer; it just waits.
This works well inside one process, where producer and consumer share memory and a blocked thread costs only its stack and a scheduler entry. It also composes naturally: if a thread pool's queue is full and the submitting thread blocks, whatever feeds that thread slows down too, all the way back to the source.
2Why Non-Blocking Systems Needed Demand Signalling
Around 2010 to 2015, many servers moved to asynchronous, non-blocking designs, such as Netty and later Spring WebFlux, where a handful of event-loop threads serve thousands of connections. In such a design, blocking an event-loop thread stalls every connection it serves, so put on a full queue is not an option. Without blocking, the only alternatives are to buffer without limit, which turns overload into memory growth, or to drop data.
Reactive Streams solved this by reversing the flow of control. The consumer, called the Subscriber, tells the producer, the Publisher, how many items it is ready to receive, through request(n) on a Subscription. The Publisher may send at most that many items and then must wait for more demand, without blocking any thread: it simply does not emit. Java 9 adopted these interfaces as java.util.concurrent.Flow, so libraries such as Reactor, RxJava and the JDK's HTTP client can interoperate.
8 messages in order. Play them, or step through them one at a time.
The consumer decides the pace; the producer never sends more than was requested.
3Flow in Code
The JDK includes one Publisher implementation, SubmissionPublisher, which is handy for seeing the protocol. Our Subscriber requests one item at a time, processes it slowly, and asks for the next.
Notice where the backpressure ends up. Inside the reactive pipeline, nothing blocks: the publisher holds items until demand arrives. But our loop is ordinary imperative code, so SubmissionPublisher bridges the two worlds by making submit block when the subscriber's buffer is full. The alternative, offer with a timeout and a drop handler, lets the producer drop items instead. Every boundary between a demand-driven world and the outside must make that same choice: block, buffer, or drop.
4The Same Idea Between Services
Between services we cannot block a thread in another process, so backpressure takes the form of the consumer controlling the rate. A Kafka consumer pulls: it calls poll when it is ready and receives at most max.poll.records records, so a slow consumer simply polls less often and the backlog stays in the broker, bounded by disk and retention, as consumer lag. RabbitMQ consumers set a prefetch count, the number of unacknowledged messages the broker may push to them, which is credit-based flow control. HTTP/2 and gRPC streams use flow-control windows: the receiver grants the sender a number of bytes it may send and grows the window as it reads. TCP itself does the same with its receive window.
All of these are versions of request(n). The consumer grants credit, the producer spends it, and when credit runs out, data waits at the producer side, where it is bounded. Broker partitions are bounded too, in a different way: by retention and disk, so a consumer that falls far enough behind starts losing old data, which is another overload behaviour to decide on.
| Where | Mechanism | When the consumer is slow |
|---|---|---|
| Threads in one JVM | bounded BlockingQueue | producer thread blocks on put |
| Reactive pipeline | Flow request(n) | publisher holds items, no thread blocked |
| Kafka | consumer pull, max.poll.records | lag grows in the broker, up to retention |
| RabbitMQ | prefetch count | broker stops pushing to that consumer |
| HTTP/2, gRPC, TCP | flow-control windows | sender waits for the window to open |
5Without Backpressure, Buffers Grow Somewhere
If no part of a flow pushes back, overload does not disappear; it turns into a growing buffer somewhere. It might be an unbounded executor queue, a reactive operator configured to buffer without limit, a client library's request queue, or the socket buffers of the operating system. The symptom is always the same: latency grows first, because items wait longer and longer, then memory grows, and finally something crashes and drops everything at once.
Virtual threads, final in Java 21, change part of this picture. Blocking a virtual thread is cheap, so plain blocking code with bounded queues can again provide backpressure at high concurrency, without reactive operators. The design question does not change, though: someone must still decide what the producer does when the consumer is slower.
6Designing Flows Between Services
So for every flow, in a design review, we ask one question and expect a concrete answer: what happens when the consumer is slower than the producer, for a minute, and for an hour? Acceptable answers are explicit: the producer blocks or is slowed by credit, the request is rejected with a retryable error, data waits in a durable broker with an alert on lag before retention runs out, old or low-value data is dropped and counted, or the consumer scales out. The unacceptable answer is silence, because silence means an unbounded buffer nobody chose.
We then make the chosen behaviour visible, with metrics for queue depth, lag, rejections and drops, and we test it with a load test that deliberately slows the consumer.
7Follow-up questions
Is Flow meant to be used directly by application code?
Mostly not. Flow defines the interfaces so that libraries can interoperate; writing a correct Publisher by hand is hard because the specification has many rules about signalling and concurrency. Applications usually use Reactor, RxJava or a framework built on them, and only touch Flow at integration points.
Does Kafka need backpressure if the broker stores everything?
The broker gives a large, durable buffer, but it is still bounded by retention. If consumers fall behind for longer than the retention period, unconsumed data is deleted. So we monitor consumer lag in time, not just in messages, and alert well before it approaches retention.
How does request(Long.MAX_VALUE) fit in?
It means unbounded demand: the subscriber accepts everything as fast as it comes, which turns backpressure off. It is fine when the subscriber is known to be faster than the publisher, but in other cases it brings back the unbounded buffer problem somewhere downstream.
8Conclusion
In this answer, we saw that blocking queues provide backpressure by blocking the producer's thread, which suits threads in one process. Non-blocking systems cannot block, so Reactive Streams and Java 9's Flow let the consumer signal demand with request(n), and the producer sends no more than requested. Between services the same idea appears as consumer pull in Kafka, prefetch credits in RabbitMQ and flow-control windows in HTTP/2 and TCP. Without backpressure, buffers grow until something fails, so every flow needs an explicit, measured answer to what happens when the consumer is slower.
Open with
Backpressure is how a slow consumer slows a fast producer. With a bounded BlockingQueue it is simple: when the queue is full, put blocks the producer thread, which is fine inside one process where a waiting thread is cheap.
Then, if they want more
Demand-based backpressure appeared when systems became asynchronous and non-blocking. On an event loop, a few threads serve thousands of connections, so blocking one stalls everything, and without blocking the only options were unbounded buffering or dropping.
Reactive Streams, adopted in Java 9 as java.util.concurrent.Flow, reverses the control: the subscriber calls request(n) on its subscription, and the publisher may emit at most n items, then simply holds further items until more demand arrives, without blocking any thread. At the edges, something like SubmissionPublisher has to choose to block, buffer or drop.
Between services it is the same idea under different names: Kafka consumers pull and take at most max.poll.records, RabbitMQ uses prefetch credits, and HTTP/2, gRPC and TCP use flow-control windows. The lesson for design is that overload never disappears; without backpressure it becomes a growing buffer somewhere, latency first, then memory, then a crash.
So for every flow I want an explicit answer to what happens when the consumer is slower than the producer, block, reject, wait in a durable broker with a lag alert, drop and count, or scale, plus metrics and a load test that proves it. Virtual threads make blocking cheap again, but they do not remove that decision.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 9, Java 21, Java 5
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Collections Framework, overview, java.util, package documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Java's queue APIs grew from Vector and Stack, to Queue and Deque (5 and 6), to the...": https://scrivio-interview-library.netlify.app/java/queues-and-deques/d2c9760137/