Spring Boot & Microservices (50 Qs)•Part 3: Microservices, Distributed Systems & Observability (25 - 35)
Asynchronous Execution with `@Async` and Context Propagation.
senior LevelAsync & PromisesSystem DesignMicroservices
In Simple Words:
Annotating a method with @Async executes it on a background thread pool:
- SecurityContext Loss: Thread-local state (
SecurityContextHolder, MDC correlation IDs) does not automatically transfer to the async thread! - Fix: Configure a
DelegatingSecurityContextAsyncTaskExecutorto propagate security tokens and trace IDs to the background thread.