Spring MVC Async Processing Interview Questions and Answers
Master Spring MVC Async Processing with interview questions covering @Async, Callable, DeferredResult, WebAsyncTask, CompletableFuture, AsyncTaskExecutor, request lifecycle, thread management, and production best practices.
Introduction
Modern enterprise applications frequently execute long-running operations.
Examples
- Payment Processing
- Report Generation
- File Uploads
- Image Processing
- Third-Party REST API Calls
- AI Model Inference
- Email Notifications
If these operations execute on the request thread, users experience
- Slow responses
- Thread blocking
- Poor scalability
- Request timeouts
Spring MVC supports Asynchronous Request Processing, allowing request threads to be released while background work continues.
This significantly improves scalability and throughput.
Spring MVC Async Architecture
flowchart LR
Client --> DispatcherServlet
DispatcherServlet --> RequestThread
RequestThread --> AsyncExecutor
AsyncExecutor --> BackgroundTask
BackgroundTask --> Response
Response --> Client
Q1. What is Async Processing?
Answer
Async Processing allows long-running tasks to execute on a separate thread instead of blocking the request thread.
Benefits
- Better scalability
- Faster response
- Improved throughput
- Reduced request blocking
The servlet container thread becomes available to serve other requests.
Q2. Why do we need Async Processing?
Without Async
Request Thread
↓
Wait
↓
Database
↓
REST Call
↓
Response
Thread remains blocked.
With Async
flowchart LR
Request --> AsyncThread
Request --> FreeRequestThread
AsyncThread --> Response
The request thread returns to the thread pool immediately.
Q3. What is @Async?
@Async executes a method asynchronously.
Example
@Async
public void sendEmail(){
}
Requirements
@EnableAsync
Spring creates background threads automatically.
Q4. What is Callable?
Spring MVC supports asynchronous controllers using Callable.
Example
@GetMapping
public Callable<String> report(){
}
Workflow
- Request thread exits.
- Worker thread executes.
- DispatcherServlet resumes response.
Callable Lifecycle
sequenceDiagram
Client->>DispatcherServlet: Request
DispatcherServlet->>Callable: Execute Async
Callable-->>DispatcherServlet: Result
DispatcherServlet-->>Client: Response
Q5. What is DeferredResult?
DeferredResult allows another thread to complete the response later.
Example use cases
- Event-driven systems
- Webhooks
- Long polling
- Message queues
Example
DeferredResult<String>
Controller returns immediately while another thread sets the result.
Q6. What is WebAsyncTask?
WebAsyncTask extends Callable.
Additional features
- Timeout handling
- Error callbacks
- Completion callbacks
Useful for production-grade asynchronous APIs.
Q7. What is CompletableFuture?
CompletableFuture enables asynchronous programming with composable operations.
Example
CompletableFuture
Capabilities
- Parallel execution
- Chaining
- Exception handling
- Combining multiple tasks
Widely used with REST APIs and database calls.
Q8. What is AsyncTaskExecutor?
Spring executes asynchronous tasks using a task executor.
Example
ThreadPoolTaskExecutor
Recommended configuration
- Core Pool Size
- Maximum Pool Size
- Queue Capacity
Avoid using unbounded thread creation.
Q9. What are common Async use cases?
Examples
- Email sending
- PDF generation
- Video processing
- Report generation
- AI processing
- Notification delivery
- Background imports
These tasks should not block user requests.
Q10. Async Processing Best Practices
Use Thread Pools
Avoid creating raw threads.
Configure Timeouts
Prevent infinite waiting.
Handle Exceptions
Use centralized logging.
Avoid Shared Mutable State
Ensure thread safety.
Monitor Thread Pools
Track queue size and utilization.
Banking Example
flowchart TD
Customer --> PaymentController
PaymentController --> PaymentService
PaymentService --> Database
PaymentService --> AsyncEmail
PaymentService --> AsyncSMS
AsyncEmail --> EmailServer
AsyncSMS --> SMSGateway
The payment response returns immediately while notifications are processed asynchronously.
Common Interview Questions
- What is Async Processing?
- Why use asynchronous processing?
- What is
@Async? - What is
Callable? - What is
DeferredResult? - What is
WebAsyncTask? - What is
CompletableFuture? - What is
AsyncTaskExecutor? - Common async use cases?
- Async best practices?
Quick Revision
| Topic | Summary |
|---|---|
| Async Processing | Background execution |
@Async |
Execute method asynchronously |
Callable |
Async controller return type |
DeferredResult |
Delayed response |
WebAsyncTask |
Callable with timeout support |
CompletableFuture |
Modern async programming |
ThreadPoolTaskExecutor |
Thread pool implementation |
| Timeout | Prevent hanging requests |
| Thread Pool | Resource management |
| Monitoring | Observe async execution |
Async Request Lifecycle
sequenceDiagram
Client->>DispatcherServlet: HTTP Request
DispatcherServlet->>Controller: Invoke
Controller-->>DispatcherServlet: Callable
DispatcherServlet-->>Client: Release Request Thread
DispatcherServlet->>AsyncExecutor: Execute
AsyncExecutor->>BusinessService: Process
BusinessService-->>AsyncExecutor: Result
AsyncExecutor->>DispatcherServlet: Complete
DispatcherServlet-->>Client: HTTP Response
Production Example – Banking Statement Generation
A banking application allows customers to download a one-year account statement.
Problem
Generating a PDF statement requires
- Database queries
- Transaction aggregation
- PDF generation
- Digital signature
Execution time: 15–30 seconds
Blocking the request thread is inefficient.
Solution
- Customer requests the statement.
- Controller immediately starts asynchronous processing.
- Request thread returns to the servlet container.
- Background thread generates the PDF.
- When complete, the response is returned or a download notification is sent.
@GetMapping("/statement")
public Callable<StatementResponse> generateStatement() {
return () ->
statementService.generate();
}
flowchart LR
Customer --> DispatcherServlet
DispatcherServlet --> StatementController
StatementController --> Callable
Callable --> ThreadPoolTaskExecutor
ThreadPoolTaskExecutor --> StatementService
StatementService --> PostgreSQL
StatementService --> PDFGenerator
PDFGenerator --> StatementResponse
StatementResponse --> Customer
Async Options Comparison
| Feature | @Async |
Callable |
DeferredResult |
CompletableFuture |
|---|---|---|---|---|
| Async Service Methods | ✅ | ❌ | ❌ | ✅ |
| Async Controller | ❌ | ✅ | ✅ | ✅ (with Spring MVC support) |
| Timeout Support | Limited | Basic | Manual | Flexible |
| Composition | ❌ | ❌ | ❌ | ✅ |
| External Completion | ❌ | ❌ | ✅ | ✅ |
| Best Use Case | Background tasks | Long-running request | Event-driven APIs | Parallel workflows |
Production Thread Pool Configuration
@Bean
public ThreadPoolTaskExecutor taskExecutor() {
ThreadPoolTaskExecutor executor =
new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10);
executor.setMaxPoolSize(50);
executor.setQueueCapacity(500);
executor.setThreadNamePrefix("async-");
executor.initialize();
return executor;
}
Using a properly configured thread pool prevents resource exhaustion and improves throughput.
Key Takeaways
- Async Processing improves scalability by releasing request threads while long-running tasks execute in the background.
@Asyncis ideal for asynchronous service methods such as sending emails, notifications, or generating reports.Callable,DeferredResult, andWebAsyncTaskenable asynchronous request processing in Spring MVC controllers.- CompletableFuture provides powerful asynchronous composition, parallel execution, and exception handling.
- Always execute asynchronous tasks using a properly configured ThreadPoolTaskExecutor rather than creating raw threads.
- Configure timeouts, monitor thread pool utilization, and handle exceptions to build reliable asynchronous systems.
- Asynchronous processing is particularly valuable for I/O-bound operations such as remote service calls, report generation, and file processing.
- Proper use of asynchronous programming significantly improves responsiveness, throughput, and user experience in enterprise Spring MVC applications.