Timing out the execution time of a controller/method in Spring
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Timing out a Spring controller method is trickier than it first sounds, because there are at least two different goals. Sometimes you want the HTTP request to stop waiting and return a timeout response. Other times you want the underlying work itself to be cancelled.
Those are not the same thing. A web timeout may end the request while the background work keeps running unless you design cancellation deliberately.
Timeout at the Web Layer With WebAsyncTask
In Spring MVC, a common way to apply request-level timeout behavior is to return a WebAsyncTask.
This tells Spring MVC to give up waiting after two seconds and return the timeout result.
That solves the HTTP response problem. It does not automatically guarantee that deeper work such as database calls or remote requests have stopped.
Using CompletableFuture With Timeout
If the controller delegates to async code, you can enforce timeout behavior at the future level.
This is useful when your controller already works with async flows. But again, it is important to understand whether the underlying work is truly interruptible.
Global MVC Async Timeout
If you want a global timeout for asynchronous MVC requests, configure it centrally:
This applies to asynchronous request handling rather than to every blocking controller method automatically.
Timeout the Real Dependency, Not Just the Controller
Very often the controller is not the real problem. The real problem is a slow downstream dependency:
- HTTP client call
- database query
- message broker request
- external service
In those cases, the best timeout is usually at the client boundary. For example, set timeouts on WebClient, RestTemplate, the JDBC driver, or the HTTP client itself.
That is usually more reliable than trying to treat the controller as a magical kill switch for all deeper work.
Example With Service-Level Timeout
You can also push timeout behavior into a service layer using Future.get(timeout) style logic or a resilience library.
This makes the timeout rule reusable outside the controller, which is often a better design.
Request Timeout Versus Cancellation
This is the core distinction:
- request timeout means the client stops waiting
- cancellation means the underlying work actually stops
Cancellation only works if the code underneath supports interruption or cooperative cancellation. For example, a blocking library call that ignores interruption may continue running even after the HTTP layer already returned a timeout.
So if you truly need cancellation, design the work units and dependencies with that requirement explicitly.
When Resilience Libraries Help
For production systems, resilience libraries such as Resilience4j often provide a cleaner approach than hand-rolled timeout logic. They allow timeout policies, fallbacks, and monitoring around service calls instead of scattering timeout code across controllers.
That is especially useful when the timeout policy belongs to a downstream integration rather than to the HTTP endpoint itself.
Common Pitfalls
One common mistake is thinking a controller timeout automatically kills the underlying task. Often it only ends the HTTP wait.
Another mistake is timing out only at the controller while leaving downstream HTTP clients or database calls with no timeout at all.
It is also easy to use async timeout mechanisms without configuring a proper executor, which leads to misleading behavior under load.
Finally, timeout handling should return a useful error response or fallback. A raw stack trace or silent hanging thread is not an operational strategy.
Summary
- In Spring, timing out a controller can mean timing out the HTTP response or cancelling the underlying work.
- '
WebAsyncTaskis a common MVC solution for request-level timeout handling.' - '
CompletableFuture.orTimeoutworks well for async flows.' - The best timeout often belongs at the slow dependency boundary, not only at the controller.
- If true cancellation matters, make sure the underlying work is actually interruptible.
Related reading
- Tips for using Vim as a Java IDE?
- Tips implementing permutation algorithm in Java
- .toArraynew MyClass0 or .toArraynew MyClassmyList.size?
- Tomcat How to find out running Tomcat version?
- Tomcat takes too much time to start - Java SecureRandom
- Tomcat threads vs Java threads
- Tomcat VS Jetty
- TomcatEmbeddedServletContainerFactory is missing in Spring Boot 2

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.