How to handle asynchronous operations in Spring Boot?

Spring Boot handles asynchronous operations through the @Async annotation, which offloads a method's execution to a separate thread pool so the calling thread doesn't block waiting for it to finish.

Key Points: • @EnableAsync on a configuration class activates Spring's asynchronous method execution support. • Methods annotated @Async run on a separate thread managed by a TaskExecutor rather than the caller's thread. • Async methods typically return void, Future, or CompletableFuture so callers can track completion or results. • A custom TaskExecutor bean lets you tune thread pool size instead of relying on the default SimpleAsyncTaskExecutor. • Well suited for operations like sending emails, generating reports, or calling slow external services that don't need to block the main flow.

Example: An order-processing service calls an @Async method to send a confirmation email after checkout, letting the HTTP response return immediately while the email is dispatched in the background.

Code Example:

@EnableAsync
@Configuration
public class AsyncConfig {}

@Service
public class NotificationService {

    @Async
    public CompletableFuture<Void> sendConfirmationEmail(String to) {
        emailClient.send(to, "Order confirmed");
        return CompletableFuture.completedFuture(null);
    }
}

Interview Tip: A concise interview answer is:

"I enable async support with @EnableAsync and mark methods that shouldn't block the caller with @Async, returning a CompletableFuture when I need to track completion. It's ideal for things like sending emails or calling slow external services, since the main thread keeps going instead of waiting."