Scrivio Interview library
ScenarioIntermediate7 min read · updated 12 November 2026

A WebFlux endpoint streams database rows to slow clients over HTTP. How does backpressure flow end to end, and where can it break?

When a client reads slowly, its TCP receive window fills, our socket's send buffer fills, Netty marks the channel as not writable, and Reactor Netty stops requesting items from the Flux it is writing. That missing demand travels up through every operator to the R2DBC driver, which stops fetching rows if it supports demand-driven fetching. The chain breaks wherever something buffers without limit, such as collectList, an unbounded onBackpressureBuffer, Flux.create with a buffering strategy, or a bridge from a listener that cannot be paused, so we bound those places and watch memory under slow clients.

Why this question is asked

Streaming a large result to a browser or a partner system is one of the use cases WebFlux is genuinely good at. The promise is that a slow client should cost us a small, fixed amount of memory, not the whole result set. That promise only holds if every link from the socket back to the database passes demand along, and one careless operator is enough to break it. Interviewers ask this because it tests whether we understand backpressure as an end-to-end property rather than a feature of one library. In this answer, we will recap the pieces in the path, follow demand from the socket back to the database, look at the places where it breaks, handle the case of a source that cannot be paused, and see what to monitor.

1A Quick Recap of the Path

Our endpoint returns a Flux<Row>, produced by an R2DBC query, a reactive database driver that returns rows as a stream instead of a list. WebFlux encodes each item, for example as one JSON object per line with the application/x-ndjson media type, and hands the encoded buffers to Reactor Netty. Reactor Netty is the reactive layer over Netty, the networking library that owns the socket.

Below Netty sits TCP. The operating system keeps a send buffer for each socket, and the client advertises a receive window, the amount of data it is willing to accept. When the client application reads slowly, its window shrinks, our send buffer fills, and the kernel stops accepting new bytes from Netty.

Java
@GetMapping(value = "/export", produces = MediaType.APPLICATION_NDJSON_VALUE)
Flux<OrderRow> export() {
return client.sql("SELECT * FROM orders ORDER BY id")
.map((row, meta) -> OrderRow.from(row))
.all(); // a Flux, not a List: rows stream as demand allows
}

Notice that the method returns the Flux itself. Nothing is read from the database until Reactor Netty subscribes and requests the first items.

2Following Demand From the Socket Back to the Database

Netty keeps its own outbound buffer per channel and compares it with a high water mark (64 KB by default in Netty; check your version and configuration). When the pending bytes go above that mark, channel.isWritable() becomes false. Reactor Netty watches that flag. While the channel is writable it requests more items from our Flux, writes them and flushes; when the channel stops being writable it simply stops requesting, and when the buffer drains below the low water mark it requests again.

That pause in request(n) calls travels upstream. The encoder requests only what Reactor Netty requested, map requests only what the encoder requested, and the R2DBC Flux receives no new demand. A driver that supports demand-driven fetching, such as r2dbc-postgresql when a fetch size is set so it uses a cursor, then stops asking the database for the next batch of rows. The database connection sits idle, holding the cursor, until the client catches up. The reason this works is that each layer only produces what the layer below asked for.

Step throughDemand travelling up from a slow client
ClientReactor NettyWebFlux encoderR2DBC driverPostgreSQL
request(n)
request(n)
fetch next batch (cursor)
rows
onNext rows
encoded buffers
write while channel is writable
TCP window full: channel not writable
stop requesting
no demand: no further fetch

10 messages in order. Play them, or step through them one at a time.

No request means no fetch: memory per client stays at a few buffers.

3Where the Chain Breaks

Demand can only travel through operators that respect it. Several common operators do not, by design. collectList() and collectMap() must hold every item before they can emit anything, so they request everything upstream and keep it all in memory. An onBackpressureBuffer() without a size accepts everything the source produces and queues it, so the slow client's pace never reaches the database. Flux.create with its default FluxSink.OverflowStrategy.BUFFER does the same: whatever our code pushes into the sink is queued without limit when downstream demand is zero.

There are also less obvious breaks. Some drivers read the whole result set eagerly unless we configure a fetch size, so the stream looks reactive but the memory is already spent. A flatMap with a high concurrency can subscribe to many inner sources at once and buffer their results. And a Mono<List<Row>> returned from the repository layer, instead of a Flux, means the list was built in full before streaming started.

Java
// Broken: the whole result is collected before anything is written
Mono<List<OrderRow>> export() { return rows().collectList(); }
// 2 million rows x ~500 bytes = ~1 GB held per request
// Broken: demand is cut at the buffer
Flux<OrderRow> export() { return rows().onBackpressureBuffer(); }
// a slow client lets the buffer grow until the heap is gone
// Fixed: stream the Flux; if a buffer is needed, bound it
Flux<OrderRow> export() { return rows().onBackpressureBuffer(500, BufferOverflowStrategy.ERROR); }

So when reviewing a streaming endpoint we read the chain from the source to the response and ask, at each operator, whether it passes demand along or absorbs it.

4Bridging a Source That Cannot Be Paused

Not every source can be pulled. If the rows come from a message listener, a callback API or a websocket from another system, items arrive whenever the other side sends them. When we bridge such a source into a Flux with Flux.create or a Sinks.Many, there is no way to tell the sender to wait. We must decide what happens when the client is slower than the sender, and write that decision into the code: a bounded buffer that errors when full, dropping with a counter, keeping only the latest value, or, when the source has its own pause mechanism such as a Kafka consumer, calling pause when our queue passes a threshold and resume when it drains.

The worst choice is the default one we did not notice, because it is usually an unbounded queue.

5Monitoring Memory and Buffers Under Slow Clients

A streaming endpoint that works on a fast laptop can still fall over in production, because production has slow mobile clients and proxies with small buffers. We test it on purpose: start a few hundred clients that read at one kilobyte per second, and watch heap usage, direct memory (Netty allocates its buffers outside the heap), the number of open connections and database cursors, and how long each request stays open. If heap grows with the number of slow clients, something in the chain is buffering.

Two settings complete the picture. A write timeout or an idle timeout closes connections whose clients stopped reading entirely, so a stuck client cannot hold a database cursor for an hour. And the database connection pool must be sized for the number of concurrent exports, since each paused stream holds a connection while it waits.

6Follow-up questions

Does returning Flux<T> with application/json stream the response?

The JSON encoder can write a Flux as a JSON array, but whether it streams element by element or not depends on the codec and the Spring version, so check your version. For large exports we choose a streaming media type such as application/x-ndjson or text/event-stream, where each element is written and flushed on its own and backpressure clearly applies per element.

What happens to the database connection while the client is slow?

With a cursor-based fetch, the connection stays checked out from the pool and the cursor stays open, idle, until demand returns. That is cheap in memory but it uses a pool slot, so many slow clients can exhaust the pool. Timeouts on idle streams and a pool size matched to the expected number of concurrent exports protect against that.

How would you prove the backpressure works end to end?

Write a test client that reads very slowly, for example with WebClient and a limitRate(1) plus a delay per element, and watch the log() operator output on the server side to confirm that request(n) calls pause and that the driver fetches in batches. Then run a load test with many slow clients and confirm that heap usage stays flat as the client count grows.

7Conclusion

In this answer, we followed backpressure from a slow client back to the database. The full TCP window makes Netty's channel unwritable, Reactor Netty stops requesting, and the missing demand passes up through each operator to the R2DBC driver, which stops fetching rows when it supports cursor-based fetching. The chain breaks at any operator that absorbs demand, such as collectList, an unbounded onBackpressureBuffer or Flux.create with a buffering strategy, and at bridges from sources that cannot be paused, where we must choose an explicit buffer or drop policy. Finally, we test with deliberately slow clients and watch heap, direct memory, connections and cursors.

What to say in the interviewAbout 100 seconds aloud

Open with

Backpressure here is end to end, and it starts at the socket. When a client reads slowly, its TCP receive window fills, our send buffer fills, and Netty's outbound buffer goes above its high water mark, so the channel becomes not writable.

Then, if they want more

Reactor Netty watches that and simply stops calling request on the Flux it is writing. That missing demand propagates upstream through the encoder and through every operator back to the R2DBC driver.

A driver that fetches on demand, for example the Postgres driver with a fetch size so it uses a cursor, then stops pulling the next batch of rows, and the query just pauses until the client catches up. So memory per client stays at a few buffers.

Where it breaks is any operator that absorbs demand instead of passing it on. collectList holds the entire result. An onBackpressureBuffer without a size, or Flux.create with the default buffer strategy, accepts everything and queues it, so the database is read at full speed while the heap grows.

Eager drivers without a fetch size and returning a Mono of List from the repository do the same. And if the data comes from something I cannot pause, like a message listener, I have to choose explicitly between a bounded buffer, dropping, or pausing the consumer.

Finally I would test it with a few hundred deliberately slow clients and watch heap, direct memory, open cursors and pool usage, and add idle timeouts so a stuck client cannot hold a connection forever.

About this answer

Last updated
12 November 2026
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.
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 "A WebFlux endpoint streams database rows to slow clients over HTTP. How does...": https://scrivio-interview-library.netlify.app/spring-boot/reactive-fundamentals/e9f7b21a23/