Quick revision
The short answer
A non-async stage such as thenApply runs its callback on whichever thread completes the future, and here that is one of the HTTP client's few I/O threads. repository.save blocks that thread on the database, so while it waits the thread cannot read any other responses, and those other calls time out. The fix is to move blocking work to a dedicated executor with thenApplyAsync(fn, executor) and to check every non-async stage that follows an async client.
What to remember
In this answer, we saw that non-async stages like thenApply run on the thread that completes the future, which for an async HTTP client is one of its few I/O threads. A blocking repository.save on that thread stops it from reading other responses, so throughput collapses and unrelated calls time out. Moving the blocking step to thenApplyAsync with a dedicated, sized executor fixes it, and reviewing every non-async stage after an async client keeps it from coming back.
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.
A non-async stage such as thenApply runs its callback on whichever thread completes the future, and here that is one of the HTTP client's few I/O threads. repository.save blocks that thread on the database, so while it waits the thread cannot read any other responses, and those other calls time out. The fix is to move blocking work to a dedicated executor with thenApplyAsync(fn, executor) and to check every non-async stage that follows an async client.
Why this question is asked
This bug looks harmless in a code review: one extra line that saves the response. Yet it can take down a whole service, because it quietly changes which thread does the database work. Interviewers ask it to see whether we know where CompletableFuture callbacks actually run, which is the single most important fact for writing safe async code. In this answer, we will recap how an async HTTP client uses its I/O threads, find out which thread runs a thenApply callback, reproduce the collapse, and then fix it and look at how to prevent it across a codebase.
1A Quick Recap of Async HTTP Clients and Their I/O Threads
An async HTTP client, such as one built on Netty or the JDK's own HttpClient, does not give each request its own thread. Instead it keeps a small number of I/O threads, often one or two per CPU core, that watch many network connections at once. When bytes arrive on any connection, one of these threads reads them, decodes the HTTP response, and completes the CompletableFuture that the caller is holding.
This design is why such clients scale: four I/O threads can serve thousands of requests in flight, because none of them ever waits. The whole model rests on one promise, though. The I/O threads must never block. If one of them stops to wait for something, every connection it looks after stops being read until it comes back.
2Which Thread Runs a thenApply Callback?
A CompletableFuture is a box that will hold a value later. When we call thenApply, we register a function to run once the value is there. The non-async methods (thenApply, thenAccept, thenCompose, thenCombine and the rest) do not choose a thread. They run the function on whichever thread happens to be present when the time comes.
There are two cases. If the future is not complete yet when we attach the callback, the function runs later on the thread that completes the future. If the future is already complete, the function runs immediately on the thread that called thenApply. With an async HTTP client the response almost always arrives after we attach the callback, so the first case applies: our function runs on the I/O thread that read the response.
Notice that nothing in the code mentions a thread. That is exactly why this goes unnoticed: the callback silently borrows the thread of the code that completed the future.
3Reproducing the Collapse
Suppose the client has 4 I/O threads and a database insert takes 20 milliseconds. Before the change, each I/O thread spent a few microseconds per response, so the client could easily handle thousands of responses per second. After the change, every response holds its I/O thread for at least 20 milliseconds while repository.save waits for the database. Four threads can now handle at most 4 x 50 = 200 responses per second, and if the database slows down to 200 milliseconds, only 20.
Meanwhile, responses for other requests keep arriving on the sockets. Nobody reads them, because all four I/O threads are sitting inside JDBC calls. Those requests hit their read timeout and fail, even though the remote server answered quickly. The client reports timeouts, and it is easy to blame the remote service when the real cause is in our own callback.
6 messages in order. Play them, or step through them one at a time.
While the I/O thread waits on the database, every other connection it owns goes unread.
So the failure has two faces. Throughput collapses because a fast, non-blocking thread pool is now doing slow blocking work, and unrelated calls time out because their responses wait in socket buffers that no thread is free to read.
4Fix: Move Blocking Work to a Dedicated Executor
The fix is to tell CompletableFuture which thread should run the blocking step. The Async variants accept an Executor, and the callback is then submitted to that executor instead of running on the I/O thread. The I/O thread only hands the work over and goes back to reading sockets.
Notice that we left parse on the I/O thread. Parsing a small JSON body is quick CPU work and moving it would only add a thread hop. The rule is not that every stage must be async, but that no stage that can block may run on a thread that must not block.
We size the executor to match the database connection pool, here 20, because more threads would only queue for connections. On Java 21 and later, Executors.newVirtualThreadPerTaskExecutor() is another good choice: each save gets a cheap virtual thread, and the connection pool still limits how many run against the database at once. We should not use the no-argument thenApplyAsync, because that sends the work to ForkJoinPool.commonPool(), which is shared by the whole JVM and sized for CPU work, so we would just move the starvation somewhere else.
5Preventing It Across the Codebase
This bug appears wherever a non-async stage follows a future that an async client completes, so we review those places on purpose. When a method returns a future from an HTTP client, a message client or an async database driver, every thenApply, thenAccept, whenComplete or handle attached to it must be cheap and non-blocking, or must use the Async form with an explicit executor.
Two habits make this easier. Logging the thread name in a test, as we did above, shows immediately where a callback runs. And many teams wrap their async clients so that the futures they return already complete on an application executor, which removes the trap for every caller at once. Some clients also offer a setting for the executor that completes futures; check your client's documentation for it.
6Follow-up questions
Would thenApply ever run on the calling thread instead?
Yes. If the future is already complete when thenApply is called, the function runs immediately on the thread that called thenApply. This makes the behaviour depend on timing: in a test with a stubbed client that returns completed futures, the callback runs on the test thread and the bug stays hidden, while in production it runs on the I/O thread.
Why not just give the HTTP client more I/O threads?
It would only delay the problem. With 16 I/O threads and 20 ms inserts we would still be capped at 800 responses per second, and a slow database would bring the timeouts back. I/O threads are meant to never block; adding more of them treats the symptom and wastes the efficiency that made the async client worth using.
Is thenApplyAsync without an executor good enough?
Usually not for blocking work. Without an executor it uses ForkJoinPool.commonPool(), which has about as many threads as CPU cores and is shared with parallel streams and other libraries. Blocking database calls there starve everything else that uses the pool. Pass an executor that is meant for blocking work.
7Conclusion
In this answer, we saw that non-async stages like thenApply run on the thread that completes the future, which for an async HTTP client is one of its few I/O threads. A blocking repository.save on that thread stops it from reading other responses, so throughput collapses and unrelated calls time out. Moving the blocking step to thenApplyAsync with a dedicated, sized executor fixes it, and reviewing every non-async stage after an async client keeps it from coming back.
Open with
The key is which thread runs the callback. thenApply does not pick a thread; if the future is not complete yet, the function runs on whichever thread completes it, and with an async HTTP client that is one of its I/O threads.
Then, if they want more
Those threads are few, maybe four, and they are supposed to never block, because each one reads responses for many connections. Once repository.save is in the callback, each I/O thread sits in a JDBC call for maybe 20 milliseconds, so four threads can handle only about 200 responses a second.
Worse, while they wait, responses for other requests sit unread in the sockets, and those requests hit their read timeouts, which is why the client reports timeouts even though the remote service is fine.
The fix is to keep cheap work like parsing on the I/O thread and move the blocking save to thenApplyAsync with a dedicated executor, sized to the connection pool or using virtual threads. I would not use the default thenApplyAsync, because that moves the blocking into the common pool.
And I would review every non-async stage that follows an async client, because the same trap is there for any blocking call.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 21
- 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
- java.util.concurrent, package documentation, JLS chapter 17, Threads and Locks
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 service uses an async HTTP client that completes futures on its few I/O threads. After...": https://scrivio-interview-library.netlify.app/java/completable-future/04abf3a80b/