0
0
Spring Bootframework~8 mins

@Async for async methods in Spring Boot - Performance & Optimization

Choose your learning style9 modes available
Performance: @Async for async methods
MEDIUM IMPACT
This affects how quickly the server responds to requests by running methods asynchronously, improving user interaction speed.
Running a long task without blocking the main request thread
Spring Boot
@Async
public void longTask() throws InterruptedException {
  Thread.sleep(5000); // simulate long work
}

@GetMapping("/process")
public String process() {
  longTask(); // runs asynchronously
  return "done";
}
The long task runs in a separate thread, so the request returns immediately without waiting.
📈 Performance GainNon-blocking request thread, improves INP by reducing response delay.
Running a long task without blocking the main request thread
Spring Boot
@GetMapping("/process")
public String process() throws InterruptedException {
  longTask(); // runs synchronously
  return "done";
}

public void longTask() throws InterruptedException {
  Thread.sleep(5000); // simulate long work
}
The request thread waits for the long task to finish, blocking response and causing slow user experience.
📉 Performance CostBlocks main thread for 5 seconds, increasing INP and delaying response.
Performance Comparison
PatternThread BlockingResponse DelayServer LoadVerdict
Synchronous method callBlocks main threadHigh delayLower concurrency[X] Bad
@Async method callNo main thread blockLow delayHigher concurrency[OK] Good
Rendering Pipeline
The @Async annotation causes the method to run in a separate thread pool, freeing the main request thread to respond faster. This reduces server-side blocking and improves how quickly the browser receives a response.
Server Request Handling
Thread Management
⚠️ BottleneckMain request thread blocking during long synchronous tasks
Core Web Vital Affected
INP
This affects how quickly the server responds to requests by running methods asynchronously, improving user interaction speed.
Optimization Tips
1Use @Async to run long tasks in background threads to avoid blocking requests.
2Configure thread pools to prevent thread exhaustion when using @Async.
3Monitor server response times to ensure asynchronous methods improve performance.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance benefit of using @Async on a method in Spring Boot?
AIt compresses the response to reduce network load.
BIt runs the method in a separate thread, reducing request blocking.
CIt caches the method result to speed up future calls.
DIt delays method execution until the server is idle.
DevTools: Network
How to check: Open DevTools, go to Network tab, trigger the request, and observe the response time.
What to look for: Faster Time To First Byte (TTFB) indicates the server responded quickly without blocking.