Quick revision
The short answer
When a controller returns DeferredResult or CompletableFuture, Spring MVC puts the servlet request into async mode and the container thread returns to the pool while the response stays open. Later, another thread completes the result, and Spring asks the container to dispatch the request again to DispatcherServlet, which writes the value as if the controller had just returned it. Filters and interceptors can run again on that async dispatch, AsyncHandlerInterceptor.afterConcurrentHandlingStarted is called on the first thread instead of postHandle, and spring.mvc.async.request-timeout sets how long the request may wait.
What to remember
In this answer, we saw that returning DeferredResult or CompletableFuture puts the request into servlet async mode: the controller returns, the container thread goes back to the pool and the response stays open. When another thread sets the result, Spring triggers an async dispatch, and DispatcherServlet writes the stored value through the normal converters and exception handling without calling the controller again. Filters and interceptors may run on that second dispatch, afterConcurrentHandlingStarted marks the hand-off, and spring.mvc.async.request-timeout or a per-result timeout bounds the wait, ending in a 503 by default.
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.
When a controller returns DeferredResult or CompletableFuture, Spring MVC puts the servlet request into async mode and the container thread returns to the pool while the response stays open. Later, another thread completes the result, and Spring asks the container to dispatch the request again to DispatcherServlet, which writes the value as if the controller had just returned it. Filters and interceptors can run again on that async dispatch, AsyncHandlerInterceptor.afterConcurrentHandlingStarted is called on the first thread instead of postHandle, and spring.mvc.async.request-timeout sets how long the request may wait.
Why this question is asked
Async return types are how a Spring MVC service waits for slow work without holding a container thread, for example while a downstream call or a message from another system arrives. They also change the request lifecycle in ways that surprise people: filters that run twice, interceptors whose postHandle seems to run at the wrong time, context that disappears, and requests that end with a 503 after 30 seconds. Interviewers ask this to see whether we understand the two-dispatch model behind it. In this answer, we will recap the normal lifecycle, then follow an async request through its first dispatch, the waiting period and the second dispatch, and finish with what that means for filters, interceptors and timeouts.
1A Quick Recap of the Normal Lifecycle
Normally one container thread carries a request through the filters, DispatcherServlet, the controller and the response writing, and only when everything has returned is the response completed. That thread is busy for the whole time, including any time the controller spends waiting on a database or another service.
The Servlet specification has supported asynchronous requests since Servlet 3.0. A servlet can call request.startAsync(), return from its service method, and finish the response later from any thread. Spring MVC builds its async support on this, so we never call startAsync ourselves.
2The First Dispatch: Starting Async and Releasing the Thread
Take a controller that returns a DeferredResult, an object that will receive its value later from some other thread.
When the method returns, the return value handler for DeferredResult sees that no value is available yet. It asks Spring's WebAsyncManager to start async processing, which calls request.startAsync() on the servlet request. Interceptors that implement AsyncHandlerInterceptor get afterConcurrentHandlingStarted instead of postHandle and afterCompletion, because the handler has not really finished.
Then the call stack unwinds: DispatcherServlet returns, each filter's code after chain.doFilter() runs, and the container thread exec-3 goes back to the pool to serve other requests. The response, however, is not completed. The connection stays open, waiting. A CompletableFuture return value works the same way; Spring adapts it to a DeferredResult internally.
3Completing the Result on Another Thread
Some time later the pricing callback runs on its own thread and calls setResult(quote). DeferredResult stores the value and tells the WebAsyncManager, which asks the container to dispatch the request again, using the async context's dispatch method. The container schedules that dispatch on one of its threads, possibly a different one, say exec-7.
Notice that the thread that called setResult does not write the response. It only hands over the value and triggers the dispatch; the writing happens on a container thread, inside the normal Spring MVC machinery. That is why message converters, ResponseBodyAdvice and exception handling all still apply to async results.
9 messages in order. Play them, or step through them one at a time.
The controller method runs only once; the second dispatch only writes the result.
4The Second Dispatch: Writing the Result
The async dispatch enters through the filter chain and DispatcherServlet again, with DispatcherType.ASYNC. DispatcherServlet finds the same handler, but the handler adapter notices that a concurrent result is waiting and does not call our controller method again. Instead it processes the stored Quote exactly like a normal return value: the Jackson converter writes it, interceptors' postHandle and afterCompletion now run, and the response completes.
If the result was an exception, for example the future completed exceptionally, it goes through the usual HandlerExceptionResolvers, so a @ControllerAdvice handler can still turn it into a ProblemDetail response.
5What Changes for Filters, Interceptors and Timeouts
Filters may run twice: once for the REQUEST dispatch and once for the ASYNC dispatch, if they are registered for async dispatches. Filters that extend OncePerRequestFilter skip the async dispatch by default, which is usually right for correlation ids and logging, but a metrics filter then measures only until the first thread returned, not the full request. Interceptors see preHandle on the first dispatch, afterConcurrentHandlingStarted when async starts, and, on the async dispatch, the handler is invoked again through preHandle before postHandle and afterCompletion run.
ThreadLocal context such as the logging MDC or the security context is tied to threads, so code that runs on the pricing thread does not see it unless it is propagated. Spring Security propagates its context to the async dispatch, but our own callback threads need explicit propagation.
Finally, a request that never gets a result would hold the connection forever, so there is a timeout. We set it per result, as with new DeferredResult<>(10_000L), or globally with spring.mvc.async.request-timeout=15s in Spring Boot; without either, the container's default applies, which is 30 seconds in Tomcat. When it expires, Spring raises AsyncRequestTimeoutException, which by default becomes a 503 Service Unavailable, unless the DeferredResult was given a timeout result or an onTimeout callback.
6Follow-up questions
Does an async controller make the request faster?
No. The client waits just as long. What improves is the server's capacity: container threads are not tied up while waiting, so the same pool can serve more concurrent slow requests. If the work itself is slow, async only moves where the waiting happens.
What is the difference between Callable and DeferredResult here?
With Callable, Spring itself runs the code on its async task executor and completes the result when the Callable returns. With DeferredResult, Spring does not run anything; some other thread, such as a message listener or a callback, must call setResult. Both use the same two-dispatch model.
How does this compare to virtual threads?
With virtual threads, the controller can simply block on the slow call, because a blocked virtual thread does not hold a scarce platform thread. That gives a similar capacity benefit with plain synchronous code and no second dispatch, which is why many teams now use async return types only for cases like long polling or server-sent events.
7Conclusion
In this answer, we saw that returning DeferredResult or CompletableFuture puts the request into servlet async mode: the controller returns, the container thread goes back to the pool and the response stays open. When another thread sets the result, Spring triggers an async dispatch, and DispatcherServlet writes the stored value through the normal converters and exception handling without calling the controller again. Filters and interceptors may run on that second dispatch, afterConcurrentHandlingStarted marks the hand-off, and spring.mvc.async.request-timeout or a per-result timeout bounds the wait, ending in a 503 by default.
Open with
The key idea is that one request gets two dispatches. When my controller returns a DeferredResult that isn't set yet, or a CompletableFuture that isn't complete, Spring's WebAsyncManager calls startAsync on the servlet request.
Then, if they want more
Interceptors that implement AsyncHandlerInterceptor get afterConcurrentHandlingStarted instead of postHandle, then the stack unwinds through the filters and the container thread goes back to the pool. The response stays open. Later another thread, maybe a callback or a message listener, calls setResult or completes the future.
That thread doesn't write the response; Spring asks the container to dispatch the request again, with DispatcherType ASYNC. On that dispatch, the request goes through the filters and DispatcherServlet again, the adapter sees there's a concurrent result and processes it like a normal return value, without calling my controller method again. So converters, ResponseBodyAdvice and @ControllerAdvice for exceptions still work.
Practical consequences: filters registered for async dispatch run twice, OncePerRequestFilter skips the second one by default, so timing filters can under-measure. ThreadLocal context like MDC doesn't follow onto my own threads. And there's a timeout, either per DeferredResult or spring.mvc.async.request-timeout, otherwise the container's default, 30 seconds on Tomcat.
On timeout we get AsyncRequestTimeoutException and a 503 by default, unless I set a timeout result.
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 Web MVC
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 "What changes in the request lifecycle when a controller returns DeferredResult or...": https://scrivio-interview-library.netlify.app/spring-boot/mvc-architecture/9b7fc128bf/