Quick revision
The short answer
flatMap subscribes to up to 256 inner publishers at once by default, so .flatMap(x -> repo.save(x)) starts up to 256 concurrent saves, each needing a connection, while the pool has perhaps 10; the rest queue for a connection until the acquire timeout fails them. The fix is to bound the concurrency at or below the pool size, such as flatMap(repo::save, 8), or better, to group records with buffer(500) and write each group with a bulk insert. Raising the acquire timeout alone only hides the mismatch.
What to remember
In this answer, we saw that flatMap's default concurrency of 256 starts far more saves than a pool of 10 connections can serve, so most saves wait in the pool's queue until the acquire timeout fails them. We fixed it by limiting concurrency to the pool size or below with flatMap(repo::save, 8), made the load much faster by batching with buffer(500) and bulk inserts, and saw that acquire timeouts should be tuned together with concurrency rather than simply raised.
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.
flatMap subscribes to up to 256 inner publishers at once by default, so .flatMap(x -> repo.save(x)) starts up to 256 concurrent saves, each needing a connection, while the pool has perhaps 10; the rest queue for a connection until the acquire timeout fails them. The fix is to bound the concurrency at or below the pool size, such as flatMap(repo::save, 8), or better, to group records with buffer(500) and write each group with a bulk insert. Raising the acquire timeout alone only hides the mismatch.
Why this question is asked
Bulk loads are where reactive pipelines meet the hard limits of a database, and the default behaviour of flatMap surprises many developers: the code works with 100 records in tests and fails with a million in production. Interviewers ask this question to see whether we connect operator defaults to resource limits and choose the right throughput strategy. In this answer, we will recap how the connection pool works, see why flatMap overwhelms it, fix it by bounding concurrency, make it much faster with batching, and discuss how timeouts should be tuned.
1A Quick Recap of the Connection Pool
A reactive application talks to the database through R2DBC with a connection pool, a fixed set of open connections that are lent out and returned. Spring Boot's R2DBC pool has a maximum size of 10 by default, set with spring.r2dbc.pool.max-size. When all connections are in use, a new request for a connection waits in a queue. If an acquire timeout is configured, a request that waits longer than that fails with a timeout error; the exact exception and property name depend on the pool version, so check your version.
Each save in a reactive repository borrows a connection, runs the insert and returns the connection. At, say, 5 ms per insert, one connection can do about 200 inserts per second, so a pool of 10 can do about 2,000 per second at best.
2Why flatMap Exhausts the Pool
flatMap subscribes to inner publishers eagerly, up to its concurrency limit, which defaults to 256. So it requests 256 records from the source and immediately starts 256 saves. Ten of them get a connection; 246 wait in the pool's queue. As saves finish, flatMap immediately pulls more records and starts more saves, so the queue stays at around 246 for the whole run of one million records.
The waiting saves do not make anything faster, because the database can still only work on 10 at a time. They only wait. A save that joins the back of a queue of 246 waits about 246 / 10 x 5 ms, roughly 120 ms, in the best case, and much longer when the database slows down under load, for example during a checkpoint or index maintenance. Once the wait exceeds the acquire timeout, saves start failing, the error cancels the flatMap, and the whole load stops.
7 messages in order. Play them, or step through them one at a time.
Throughput is set by the pool; extra concurrency only lengthens the queue.
3Fix 1: Bound the Concurrency to the Pool
The direct fix is to tell flatMap how many saves may run at once, at or below the pool size. Leaving a little headroom, such as 8 for a pool of 10, keeps connections free for other work in the same application, such as health checks or user requests.
Notice that throughput does not drop. The database was only ever working on 10 saves at a time; we have simply stopped queuing hundreds more in front of it. Backpressure now flows all the way back: flatMap pulls a new record only when a save completes, so the source is read at the speed the database can write.
4Fix 2: Batch the Inserts
Even with bounded concurrency, one million single-row inserts mean one million round trips and one million statement executions. Batching is usually far faster. We group records with buffer(500), which turns the Flux<Record> into a Flux<List<Record>>, and write each group with one bulk operation, for example a multi-row R2DBC statement.
With batches of 500, we make 2,000 round trips instead of a million, and four batches in flight keep the database busy without monopolising the pool. The best batch size and concurrency depend on row size and the database, so we measure; 500 to 1,000 rows per batch is a common starting point. For really large one-off loads, the database's own bulk path, such as PostgreSQL's COPY, is faster still. Each batch can run in its own transaction, so a failure affects one batch rather than the whole load.
5Tuning Timeouts With Concurrency, Not Instead of It
A tempting fix is to raise the acquire timeout from, say, 5 seconds to 60. The errors may disappear, but the queue of 246 waiting saves is still there, memory holds hundreds of pending records, and any other part of the application that needs a connection, such as a user request, now waits behind the bulk load for up to a minute. The timeout was doing its job: telling us that demand exceeded capacity.
So we tune the two together. First we set concurrency to match the pool, so that queues stay short; then the acquire timeout can stay short, a few seconds, and act as a real alarm. If we need more throughput, we look at batching or, carefully, a larger pool, keeping in mind that the database itself has a connection limit shared by all instances of all services.
6Follow-up questions
Why doesn't the R2DBC pool apply backpressure to flatMap by itself?
It does in a way: it queues the acquire requests. But flatMap does not know about the pool; it only sees that its inner publishers have not completed yet, and it is allowed to have 256 of them in flight. Bounding the concurrency is how we tell flatMap about the real limit.
Would concatMap fix the problem?
Yes, it would use one connection at a time and never time out, but it would also be about ten times slower than using the whole pool. flatMap with a concurrency near the pool size, or batching, gives the throughput without the queue.
Is saveAll(records) a better choice?
It is simpler to write, but how it executes depends on the Spring Data module and version, often still one insert per entity. For a million rows, explicit batching or the database's bulk loading is the reliable way to get high throughput; check what saveAll does in your version before relying on it.
7Conclusion
In this answer, we saw that flatMap's default concurrency of 256 starts far more saves than a pool of 10 connections can serve, so most saves wait in the pool's queue until the acquire timeout fails them. We fixed it by limiting concurrency to the pool size or below with flatMap(repo::save, 8), made the load much faster by batching with buffer(500) and bulk inserts, and saw that acquire timeouts should be tuned together with concurrency rather than simply raised.
Open with
The cause is flatMap's default concurrency. flatMap subscribes to up to 256 inner publishers at once, so flatMap(x -> repo.save(x)) starts 256 saves immediately, and every time one finishes it starts another.
Then, if they want more
Each save needs a connection, and the R2DBC pool in Spring Boot has 10 by default, so about 246 saves are always waiting in the pool's queue. The database can still only do 10 at a time, so the extra concurrency adds no throughput; it only adds waiting.
As soon as the database slows down a bit, waits exceed the acquire timeout, a save fails, and the error cancels the whole load. The fix is to tell flatMap the real limit: flatMap(repo::save, 8), at or below the pool size with some headroom for other traffic.
Throughput stays the same and the timeouts disappear, because backpressure now pulls records at the speed the database writes. For a million records I'd go further and batch: buffer(500), then write each batch with a multi-row R2DBC statement in its own transaction, with three or four batches in flight. That turns a million round trips into about two thousand.
And I wouldn't just raise the acquire timeout; that hides the mismatch and makes user requests wait behind the bulk load. Concurrency and timeouts have to be tuned together.
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.
- Official documentation
- Spring Framework reference, Spring WebFlux, Project Reactor reference guide
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 Flux pipeline uses .flatMap(x -> repo.save(x)) to save 1 million records and exhausts...": https://scrivio-interview-library.netlify.app/spring-boot/reactive-fundamentals/9c7bc4f9d5/