Quick revision
The short answer
I would not return CompletableFuture, because any caller can complete it or call obtrudeValue on it and change the result for everyone else who holds the same future. For async methods I would return CompletionStage, or a minimalCompletionStage() copy, so callers can only compose. With virtual threads available, though, I would make plain blocking methods the main API, since they are simpler for most callers, and offer an async variant that runs callbacks on a documented executor and always applies a timeout.
What to remember
In this answer, we saw that returning CompletableFuture hands every caller the power to complete or obtrudeValue a result others may share, so a shared async API should return CompletionStage or a minimalCompletionStage copy. With virtual threads, plain blocking methods are the simpler default for most callers and can still be wrapped in async code. Whatever the return type, callbacks must run on a documented executor rather than the client's I/O threads, and every call needs a default timeout on the transport plus a way for callers to pass their own deadline.
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.
I would not return CompletableFuture, because any caller can complete it or call obtrudeValue on it and change the result for everyone else who holds the same future. For async methods I would return CompletionStage, or a minimalCompletionStage() copy, so callers can only compose. With virtual threads available, though, I would make plain blocking methods the main API, since they are simpler for most callers, and offer an async variant that runs callbacks on a documented executor and always applies a timeout.
Why this question is asked
A shared client library lives much longer than the service that first needs it. Every team that adopts it inherits its threading model, its failure behaviour and its timeouts, and changing the signature later means changing every caller. Interviewers ask this design question to see whether we think about who controls a future, which threads run callbacks, and how long anyone will wait. In this answer, we will look at what each return type allows, why CompletableFuture is too powerful to hand out, how virtual threads change the default, and then how to treat executors and timeouts in the API.
1What Each Return Type Lets the Caller Do
A CompletableFuture is two things at once: a read side, from which we compose further stages with thenApply and friends, and a write side, from which anyone holding it can call complete, completeExceptionally or obtrudeValue. CompletionStage is the interface that has only the read side. A plain blocking method returns the value itself and makes the calling thread wait.
| Return type | Caller can | Risk or cost |
|---|---|---|
CompletableFuture<T> | compose, join, complete, obtrudeValue, cancel | a caller can change the result others see |
CompletionStage<T> | compose; toCompletableFuture() if needed | callback style still spreads into caller code |
| T (blocking) | use the value directly | needs a thread per waiting call, cheap with virtual threads |
CompletionStage keeps composition and removes the write side.The difference matters because a library often shares one future between several callers, for example when it caches an in-flight request so that ten identical lookups share one network call. If those callers each hold the same CompletableFuture, any one of them can rewrite the answer for the other nine.
2Why Handing Out CompletableFuture Is Dangerous
Here is the failure. A library caches futures for in-flight lookups, and one careless caller decides to provide a default when it gets impatient.
Notice that caller B did nothing wrong. Caller A's complete call won the race against the real network response, so the real user was thrown away. obtrudeValue is worse still: it overwrites a result even after the future completed. Returning CompletableFuture makes every caller a potential writer of shared state.
3Returning CompletionStage or a Minimal Copy
If we return CompletionStage, callers can only attach stages, which is what they need. Since Java 9, CompletableFuture also has minimalCompletionStage(), which returns a new stage that follows the original but supports only CompletionStage methods, and copy(), which returns a new CompletableFuture that completes when the original does. Completing the copy does not touch the original.
So callers keep full composition, and they can still call toCompletableFuture() when another API demands one, but they can no longer damage the shared result.
4With Virtual Threads, Blocking Is the Better Default
Async APIs existed mostly because platform threads were expensive. A service with 500 requests in flight, each waiting 200 ms on our client, needed 500 threads if the client blocked. Since Java 21, virtual threads make a blocked thread cost a few kilobytes, so a blocking method like User findUser(long id) scales to thousands of concurrent waits.
A blocking API is also easier for most callers: plain try and catch, ordinary stack traces, and no callbacks. And it is easy to wrap in async code when a caller needs a fan-out, using CompletableFuture.supplyAsync(() -> client.findUser(id), executor) with a virtual-thread executor. The reverse direction, making an async API look blocking with join(), is the one that tends to cause trouble. So I would make the blocking method the primary API and keep an async variant returning CompletionStage for callers that already compose futures.
5Executors: Never Run Callbacks on the Client's I/O Threads
If the library uses an async transport internally, its futures are completed on the transport's I/O threads. Any caller's non-async stage, such as thenApply, would then run on those threads, and one blocking callback could stall the whole client. The library must prevent that by completing the returned stage on a separate, documented executor.
The executor is part of the contract. We document which thread completes the stage, provide a safe default, and let callers supply their own executor when they want to control it.
6Timeouts and Deadlines
A library that can wait forever will eventually make some service wait forever. Every call should have a default timeout, for example 2 seconds, set on the transport itself (connect timeout and read or response timeout), so that sockets and buffers are freed when it fires. orTimeout on the returned future is only a backstop, because it stops the caller waiting but does not stop the request.
Callers must also be able to say how long they can wait, because a request that already used 1.5 seconds of a 2-second budget should not start a call with a fresh 2 seconds. A per-call option such as client.findUser(id, CallOptions.deadline(deadline)) lets the library use the remaining time and pass it downstream, so the whole chain respects one deadline.
7Follow-up questions
Is it ever fine to return CompletableFuture?
Inside one module, where the code that creates the future and the code that consumes it are maintained together, it is often fine and convenient. The concern is a shared library with many independent callers, especially if it ever shares one future between callers. At that boundary CompletionStage or a copy is the safer contract.
Does returning CompletionStage stop callers from blocking with join?
Not completely. They can call toCompletableFuture().join(). But that is a deliberate choice they make on their own copy, and it cannot affect other callers. If many callers want to block, that is a sign the blocking method should be the primary API.
Should the library offer cancellation?
Only if it can actually stop the request. Cancelling a CompletableFuture just completes it with CancellationException; the request keeps running. If the transport supports aborting a request, the library can hook that up, for example by listening for the cancellation and closing the call, and document that it does so.
8Conclusion
In this answer, we saw that returning CompletableFuture hands every caller the power to complete or obtrudeValue a result others may share, so a shared async API should return CompletionStage or a minimalCompletionStage copy. With virtual threads, plain blocking methods are the simpler default for most callers and can still be wrapped in async code. Whatever the return type, callbacks must run on a documented executor rather than the client's I/O threads, and every call needs a default timeout on the transport plus a way for callers to pass their own deadline.
Open with
I would start by asking who controls the future. A CompletableFuture has a write side: any caller can call complete or obtrudeValue on it, and if the library shares one future for identical in-flight requests, one caller can change the result for all the others.
Then, if they want more
So I would never return CompletableFuture from a shared library. For async methods I would return CompletionStage, ideally a minimalCompletionStage copy, so callers can compose but not complete. That said, now that we have virtual threads, I would make plain blocking methods the main API.
They read like normal code, give normal stack traces, scale fine on virtual threads, and a caller who needs fan-out can wrap them in supplyAsync with a virtual-thread executor.
For the async variant, I would make sure the returned stage completes on a documented callback executor, never on the transport's I/O threads, so a caller's blocking thenApply cannot stall the client.
Finally, every call gets a default timeout set on the transport itself, connect and read, so resources are freed, plus a per-call deadline option so callers can pass the remaining budget of their request instead of getting a fresh timeout each time.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 21, Java 9
- 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 "You are designing a shared asynchronous client library for internal services. Should its...": https://scrivio-interview-library.netlify.app/java/completable-future/0c59b9e908/