Quick revision
The short answer
ThreadLocal context only works while all the work stays on one thread, so CompletableFuture, executors and reactive pipelines lose it or, worse, see stale values. I would standardise one context API in the platform library, backed by Micrometer's context-propagation library, which captures a ContextSnapshot at every async boundary and restores it on the other side, wired automatically into executors with TaskDecorator and into Reactor through the Reactor Context. New code on recent Java can use ScopedValue with StructuredTaskScope, bridged by the same API, and the library ships with leak and propagation tests and a migration guide.
What to remember
In this answer, we saw that ThreadLocal context breaks as soon as work moves to another thread, so every async boundary must capture and restore it. The design standardises that in one platform API: Micrometer context-propagation snapshots as the engine, TaskDecorator and wrapped executors for imperative code, the Reactor Context bridged automatically for reactive code, and ScopedValue with StructuredTaskScope for new structured code. One supported API, a shared test kit for leaks and propagation, static checks against home-made ThreadLocals and a risk-ordered migration make it work across 100 services.
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.
ThreadLocal context only works while all the work stays on one thread, so CompletableFuture, executors and reactive pipelines lose it or, worse, see stale values. I would standardise one context API in the platform library, backed by Micrometer's context-propagation library, which captures a ContextSnapshot at every async boundary and restores it on the other side, wired automatically into executors with TaskDecorator and into Reactor through the Reactor Context. New code on recent Java can use ScopedValue with StructuredTaskScope, bridged by the same API, and the library ships with leak and propagation tests and a migration guide.
Background to this question
Request context, the current user, the tenant and the trace id, is used everywhere: in authorisation checks, in data partitioning, in every log line and in distributed tracing. When it goes missing, logs cannot be correlated; when it is wrong, one tenant can see another tenant's data. With 100 services and several concurrency styles, the risk is not one bug but a hundred slightly different home-made fixes. In this answer, we will recap why ThreadLocal context breaks at async boundaries, look at the capture-and-restore pattern, see how Micrometer context-propagation provides it for executors and Reactor, consider ScopedValue for new code, and finish with how to roll the design out to 100 services safely.
1A Quick Recap: Why ThreadLocal Context Breaks
ThreadLocal stores one value per thread. A filter sets the tenant on the request thread, and any code running on that same thread can read it. But modern code rarely stays on one thread. CompletableFuture.supplyAsync runs its task on a pool thread, an @Async method runs on an executor thread, and a Reactor pipeline may continue on whichever thread delivered the last signal.
On the new thread, the ThreadLocal holds either nothing, so the tenant is null and the trace id is lost, or a leftover value from an earlier task on that pooled thread, which is far more dangerous because it is wrong without looking wrong. Virtual threads do not change this: each one has its own ThreadLocal values, and a task submitted to another virtual thread does not see the caller's.
2The Pattern: Capture at the Boundary, Restore on the Other Side
Every correct solution follows the same pattern. At the moment work is handed to another thread, capture the current context into an immutable snapshot. When the work starts on the other thread, restore the snapshot, run the work, and then clear it, restoring whatever was there before. The important design decision is that this happens in one place per kind of boundary, not in every call site.
6 messages in order. Play them, or step through them one at a time.
Done once in a wrapper, so business code never touches it.
3Micrometer Context Propagation as the Engine
We do not need to invent this machinery. Micrometer's context-propagation library defines a ThreadLocalAccessor for each piece of context and a ContextSnapshot that captures all registered accessors at once. Spring Boot 3, Micrometer Tracing and Reactor already use it, so trace ids and MDC propagate consistently if we register our own context in the same registry.
Notice that the Spring Boot auto-configuration applies a TaskDecorator bean to the executors it builds, so @Async methods are covered without changes in business code. The weak spot is the bare CompletableFuture.supplyAsync without an executor argument, which uses the common pool; the library should make that a build-time warning.
4Reactive Code Uses the Reactor Context
Reactor pipelines hop between threads at every asynchronous step, so ThreadLocal is the wrong carrier there. Reactor has its own immutable Context, attached to the subscription and flowing upstream from the subscriber. The same registry bridges the two worlds: with Hooks.enableAutomaticContextPropagation(), available since Reactor 3.5.3, Reactor restores registered ThreadLocal values from its Context around each operator, so log lines and our TenantContext work inside a WebFlux handler (check your Reactor version).
So the design gives one source of truth, the registered accessors, and two carriers: ThreadLocal on imperative threads and Reactor Context in reactive pipelines.
5ScopedValue and StructuredTaskScope for New Code
On recent Java, ScopedValue is a better carrier for request context than ThreadLocal. It is immutable while bound, it is unbound automatically when the scope ends, so it cannot leak, and child tasks forked in a StructuredTaskScope inherit it without any capture step. ScopedValue is final in Java 25, while StructuredTaskScope was still a preview API there (check your version).
The platform API should hide which carrier is used. A call like PlatformContext.tenant() can read a ScopedValue when one is bound and fall back to the ThreadLocal otherwise, so a service can move to ScopedValue without changing its business code. The limitation is that ScopedValue does not cross executors or reactive pipelines by itself, so the snapshot mechanism is still needed for those boundaries.
6Rolling It Out Across 100 Services
The technical design is half the work; the other half is making it the only supported way. The platform library should provide one context API (PlatformContext), auto-configuration that registers the accessors and decorates executors, and a filter that sets and clears context per request. Home-made ThreadLocals for user, tenant or trace should be deprecated, with an ArchUnit or static analysis rule that flags new ones.
Tests are part of the library. A shared test kit runs a request through every boundary type, @Async, CompletableFuture with the wrapped executor, a virtual thread executor and a Reactor chain, and asserts that the tenant and trace id arrive intact and that pool threads are clean afterwards. A second test runs many tenants concurrently on a tiny pool to catch leaks. The migration guide gives teams a short checklist, and the rollout goes by risk: multi-tenant data services first, because a leaked tenant there is a security incident, then the rest.
- Business codecalls
PlatformContext.tenant(),user(),traceId() - Platform context APIone supported entry point, carrier hidden
- Carriers
ScopedValue,ThreadLocal, Reactor Context - Propagation engineMicrometer context-propagation snapshots
- Boundary wiring
TaskDecorator, wrapped executors, Reactor hook, request filter
5 layers, from the top down. Step through them one at a time.
7Follow-up questions
Why not just pass the context as a method parameter everywhere?
Explicit parameters are the clearest and safest option, and for a small codebase I would consider it. Across 100 services with logging, tracing and libraries that cannot take extra parameters, it is impractical, so we keep implicit context but make propagation automatic and tested.
What happens to tracing if a team bypasses the wrapped executor?
The task runs without the trace context, so its spans start a new trace or attach to nothing, and its logs lose the trace id. The tenant may be null or stale. That is why the library flags bare supplyAsync calls and why the propagation tests cover every boundary.
How do you avoid a stale tenant being worse than a missing one?
Fail closed. Data access in the library refuses to run when the tenant is missing, and every restore step clears the previous values afterwards, so a pool thread cannot carry a tenant from one task to the next. A missing tenant is then a visible error rather than a silent leak.
8Conclusion
In this answer, we saw that ThreadLocal context breaks as soon as work moves to another thread, so every async boundary must capture and restore it. The design standardises that in one platform API: Micrometer context-propagation snapshots as the engine, TaskDecorator and wrapped executors for imperative code, the Reactor Context bridged automatically for reactive code, and ScopedValue with StructuredTaskScope for new structured code. One supported API, a shared test kit for leaks and propagation, static checks against home-made ThreadLocals and a risk-ordered migration make it work across 100 services.
Open with
The root problem is that ThreadLocal context only exists on the thread that set it. As soon as work moves to a CompletableFuture, an executor or a reactive pipeline, the context is either missing or, on a pooled thread, stale from a previous task, which for a tenant id is a security problem.
Then, if they want more
So I would standardise capture and restore at every async boundary, done once in the platform library rather than by each team. The engine would be Micrometer's context-propagation library: we register a ThreadLocalAccessor for user, tenant and trace, and a ContextSnapshot captures them all.
We wire it into executors with a TaskDecorator and wrapped executor services, so @Async and CompletableFuture with our executor keep context, and into Reactor with automatic context propagation, so reactive code carries it in the Reactor Context.
For new code on recent Java I would allow ScopedValue with structured concurrency, because it is immutable and cannot leak, but behind the same PlatformContext API so the carrier is hidden.
Then I would make it the only supported way: deprecate home-made ThreadLocals with a static rule, ship a test kit that checks propagation through every boundary and checks pool threads are clean, fail closed when the tenant is missing, and migrate the multi-tenant data services first.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 25, Spring Boot 3
- 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
- JLS chapter 17, Threads and Locks
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 "Your platform library uses ThreadLocal for request context (user, tenant, trace id)...": https://scrivio-interview-library.netlify.app/java/java-memory-model/f3c310c189/