Quick revision
The short answer
The MDC is stored in a ThreadLocal, so it belongs to the request thread, and code that runs on another thread, such as an @Async method or a CompletableFuture callback on a pool, starts with an empty MDC. We fix it by capturing the context when the task is submitted and restoring it on the worker thread: a TaskDecorator on Spring's executors, or Micrometer's context propagation library, and for CompletableFuture we pass a wrapped executor instead of the default pool. In Reactor, we call Hooks.enableAutomaticContextPropagation() so the trace context follows the reactive chain.
What to remember
In this answer, we saw that the MDC lives in a ThreadLocal, so @Async methods and CompletableFuture stages running on pool threads start without the request's trace id. We fixed @Async with a TaskDecorator that captures the MDC on submit and restores it on the worker, preferred Spring's ContextPropagatingTaskDecorator from Micrometer's context propagation when tracing is used, gave CompletableFuture an explicit wrapped executor instead of the common pool, and enabled automatic context propagation in Reactor.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The MDC is stored in a ThreadLocal, so it belongs to the request thread, and code that runs on another thread, such as an @Async method or a CompletableFuture callback on a pool, starts with an empty MDC. We fix it by capturing the context when the task is submitted and restoring it on the worker thread: a TaskDecorator on Spring's executors, or Micrometer's context propagation library, and for CompletableFuture we pass a wrapped executor instead of the default pool. In Reactor, we call Hooks.enableAutomaticContextPropagation() so the trace context follows the reactive chain.
Why this question is asked
This bug shows up exactly when we need logs most: during an incident, the lines from the slow part of a request, usually some asynchronous call, are the ones we cannot find by trace id. Interviewers ask it because the fix is easy to apply wrongly, for example by copying the MDC but never clearing it, which leaks one request's id into another. In this answer, we will recap why the MDC is per thread, reproduce the missing trace id, fix @Async executors with a TaskDecorator, fix CompletableFuture with a wrapped executor, use Micrometer's context propagation for the whole tracing context, and see what Reactor needs.
1Why the MDC Stays on the Request Thread
The MDC, the Mapped Diagnostic Context of SLF4J, is a map kept in a ThreadLocal. A ThreadLocal is a variable where every thread sees its own copy. Micrometer Tracing puts traceId and spanId into it when a request starts, and every log line on that thread prints them.
When we call an @Async method, Spring does not run it on our thread. It hands a task to an executor, a pool of worker threads, and one of those threads runs it later. That worker thread has its own ThreadLocal copy, which is empty or, worse, still holds whatever the previous task left there. The same is true for CompletableFuture.supplyAsync(), which runs on the common ForkJoinPool when we give it no executor, and for callbacks such as thenApplyAsync().
5 messages in order. Play them, or step through them one at a time.
The task moves to another thread, but the ThreadLocal values stay behind.
So the trace id is not lost by a bug in our logging setup. The asynchronous code simply runs somewhere the ThreadLocal never reached. Notice that InheritableThreadLocal would not help either: it copies values only when a thread is created, and pool threads are created once and reused for thousands of tasks.
2Fix 1: A TaskDecorator for @Async Executors
Spring's ThreadPoolTaskExecutor accepts a TaskDecorator, a hook that wraps every Runnable at the moment it is submitted. Submission happens on the request thread, so that is where we can still read the MDC. We capture a copy there and restore it on the worker thread around the task.
The finally block restores the worker's previous state, so one request's id never stays on a pooled thread. In recent Spring Boot versions, a single TaskDecorator bean is applied to the auto-configured application task executor that @Async uses; if we define our own ThreadPoolTaskExecutor, we call setTaskDecorator() on it ourselves.
Copying only the MDC is enough for log lines. But if the asynchronous code also calls other services and we want those calls to join the same trace, we need the tracing context, not only its printed copy. That is what the next fix gives us.
3Fix 2: Context Propagation From Micrometer
Micrometer has a small library called context-propagation, which Spring Boot uses with Micrometer Tracing. It knows how to capture every registered ThreadLocal, including the current tracing span and the MDC values tracing manages, into a snapshot, and how to restore that snapshot on another thread. Spring Framework ships a ready decorator built on it.
With this decorator, the worker thread does not just print the old trace id: it has the same current span, so a RestClient call or a Kafka send made inside the @Async method is linked to the original trace. This is the option we prefer when tracing is in use, because it moves the real context rather than a copy of some strings.
4Fix 3: CompletableFuture With a Wrapped Executor
CompletableFuture does not know about Spring's TaskDecorator. supplyAsync() without an executor uses ForkJoinPool.commonPool(), which we cannot decorate. So we always pass our own executor, and we make that executor capture the context on submit.
Alternatively, we can pass the Spring ThreadPoolTaskExecutor that already has the decorator, since it implements Executor. The rule to remember is simple: every Async variant of CompletableFuture needs an explicit executor that propagates context. Non-async stages such as thenApply() run on whichever thread completed the previous stage, so they inherit that thread's context, which is correct only if the previous stage had it. The class names of the wrapping helpers have moved between context-propagation releases, so check your version.
5Reactor and WebFlux
Reactive code is harder, because one request can hop between threads at every operator. Reactor keeps its own Context that travels with the subscription instead of with the thread. Since Reactor 3.5.3, calling Hooks.enableAutomaticContextPropagation() makes Reactor restore registered ThreadLocals, including the tracing context and MDC, from that Context whenever an operator runs, so ordinary log statements inside map() or flatMap() print the trace id.
This has a cost on every operator, which is why it is opt-in. In older code we may see a manual approach that reads the trace from the Reactor Context inside doOnEach(); automatic propagation replaces it.
6Follow-up questions
Why not just use InheritableThreadLocal for the MDC?
InheritableThreadLocal copies values only when a new thread is created. Executor threads are created once and reused for many tasks, so they would keep the value from whichever request happened to create them. That gives wrong trace ids, which are more misleading than missing ones. Capturing at submit time is the only correct point.
Why must the decorator restore or clear the MDC after the task?
Because the worker thread is reused. If we set the request's MDC and leave it there, the next task on that thread, possibly from another request or a scheduled job, logs with the old trace id until something overwrites it. Restoring the previous state in a finally block keeps each task's context isolated.
Do virtual threads change this?
Not the basic rule. Each virtual thread has its own ThreadLocal values, and an executor that starts a new virtual thread per task still starts it with an empty MDC. We still capture context at submit time. Scoped values are a newer alternative to ThreadLocal for passing context down structured code, but logging libraries and tracing still rely on ThreadLocal-based propagation today, so check your versions.
7Conclusion
In this answer, we saw that the MDC lives in a ThreadLocal, so @Async methods and CompletableFuture stages running on pool threads start without the request's trace id. We fixed @Async with a TaskDecorator that captures the MDC on submit and restores it on the worker, preferred Spring's ContextPropagatingTaskDecorator from Micrometer's context propagation when tracing is used, gave CompletableFuture an explicit wrapped executor instead of the common pool, and enabled automatic context propagation in Reactor.
Open with
The reason is that the MDC is a ThreadLocal. Micrometer Tracing puts traceId and spanId into it on the request thread, but an @Async method runs on a worker thread from an executor, and supplyAsync without an executor runs on the common ForkJoinPool.
Then, if they want more
Those threads have their own, empty ThreadLocal, so the lines have no trace id. InheritableThreadLocal does not help because pool threads are created once and reused. The fix is to capture the context when the task is submitted, which happens on the request thread, and restore it on the worker. For Spring executors I register a TaskDecorator.
A hand-written one copies MDC.getCopyOfContextMap and restores the previous map in a finally block, but when tracing is in use I prefer Spring's ContextPropagatingTaskDecorator, built on Micrometer's context-propagation library, because it moves the actual span, so downstream calls also join the trace.
For CompletableFuture I never use the default pool; I pass an executor that is either the decorated Spring executor or wrapped with ContextExecutorService, for every Async stage. In Reactor, I enable Hooks.enableAutomaticContextPropagation, or spring.reactor.context-propagation=auto, so ThreadLocals are restored from the Reactor context at each operator.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Boot 3.2, Spring Framework 6
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Boot reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Log lines from @Async methods and CompletableFuture callbacks miss the trace id, while...": https://scrivio-interview-library.netlify.app/spring-boot/boot-logging/cd486c58cc/