Quick revision
The short answer
The standard is one request context object and one propagation mechanism, provided by a shared library rather than per-team ThreadLocals. The library uses Micrometer context-propagation to bridge ThreadLocals, executors and Reactor's Context, requires capture and restore at every async boundary the platform provides, such as executors and message listeners, and is tested for cross-thread propagation and clearing. New code reads context through the library's accessor, so the storage can move to ScopedValue, final in Java 25, for immutable per-request values while frameworks that still read ThreadLocals keep working.
What to remember
In this answer, we set a platform standard for request context: one immutable context object behind one accessor, propagated by Micrometer context-propagation across executors, Reactor and listeners, with capture and restore at every async boundary the platform provides. We planned ScopedValue, final in Java 25, as the storage for new platform-owned context while keeping ThreadLocal bridges for frameworks, and required the shared library to test propagation and clearing at each boundary.
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 standard is one request context object and one propagation mechanism, provided by a shared library rather than per-team ThreadLocals. The library uses Micrometer context-propagation to bridge ThreadLocals, executors and Reactor's Context, requires capture and restore at every async boundary the platform provides, such as executors and message listeners, and is tested for cross-thread propagation and clearing. New code reads context through the library's accessor, so the storage can move to ScopedValue, final in Java 25, for immutable per-request values while frameworks that still read ThreadLocals keep working.
Background to this question
Request context is the quiet backbone of a multi-tenant platform: the tenant id decides whose data we read, the identity decides what we may do, the trace context ties logs together and the locale decides what users see. When each team propagates these differently, every async boundary is a place where context is lost or, worse, leaks to the wrong request. A staff engineer is expected to set one standard that works across blocking, async, reactive and virtual-thread code. In this answer, we will look at what goes wrong today, define the context object, choose the propagation mechanism, plan for scoped values, list the boundaries that must be covered and describe how the library proves itself.
1What Goes Wrong With Per-Team ThreadLocals
Today each service, and often each library, has its own static ThreadLocal for tenant or locale, its own MDC keys and its own way of copying them to background threads. Some teams wrote TaskDecorators, some pass values as method parameters, some rely on InheritableThreadLocal, which copies values only when a thread is created and therefore leaks them across pooled tasks. Reactive code does not run on a single thread at all, so ThreadLocals there are simply wrong unless bridged into Reactor's Context.
The result is that correctness depends on every developer remembering every boundary. Add virtual threads, which make it tempting to spawn new threads freely, and the number of boundaries grows. So the standard must remove the need to remember.
2One Context Object, One Accessor
We define a single immutable RequestContext type in a shared platform library, holding tenant id, user identity, locale and a reference to trace context, and one accessor class through which all application code reads it. Nobody declares their own ThreadLocal for these values.
The accessor hides where the context is stored. Today Storage is a ThreadLocal registered with the propagation library; later it can be a ScopedValue, and application code does not change. The bind method always unbinds in a finally, so leaks to the next task are impossible by construction. Trace context stays with the tracing library, which already propagates it, and the library only exposes it.
3A Propagation Library That Bridges Every Model
Micrometer's context-propagation library is the mechanism. Each kind of context registers a ThreadLocalAccessor, and the library can take a ContextSnapshot of all of them on one thread and restore it on another. It also bridges between ThreadLocals and Reactor's Context, so a Reactor pipeline can carry the same values and restore them as ThreadLocals when it calls blocking or logging code. Spring Framework and Spring Boot integrate with it, for example through ContextPropagatingTaskDecorator and Reactor's automatic context propagation (check your versions for the exact property and class names).
- Application codereads only
Ctx.current() - Platform context library
RequestContext, bind and unbind, fail closed - Micrometer context-propagationsnapshots and restores all registered contexts
- Storage
ThreadLocaltoday,ScopedValuewhere possible - Boundariesexecutors,
@Async, Reactor, listeners, schedulers
5 layers, from the top down. Step through them one at a time.
Teams depend on the top two layers; the platform can change the lower ones.
4Planning for ScopedValue
ScopedValue, final in Java 25, binds a value for the duration of a call: ScopedValue.where(KEY, value).run(work). Inside work the value is readable, after it returns the binding is gone, and the value cannot be changed while bound. That matches request context well: it is immutable for the request, it cannot leak past its scope, and it is cheap to share with many virtual threads. Structured concurrency, still a preview in Java 25, would make child tasks inherit scoped values automatically.
But there is a catch: our frameworks do not read our scoped values. Logback's MDC, Spring's transaction synchronisation and Spring Security's context holder are ThreadLocal-based, and will be for some time. So the standard is to use ScopedValue as the storage for new, platform-owned context where the library controls both binding and reading, and to keep a ThreadLocal bridge for framework-facing values. The accessor makes this mixed state invisible to teams.
5Capture and Restore at Every Async Boundary
The platform provides every place where work changes threads, and each one must capture context at submission and restore it around the work: the auto-configured task executors and @Async (through a context-propagating TaskDecorator), any ExecutorService created through the platform's factory (wrapped with ContextExecutorService), CompletableFuture usage through those executors, Reactor through automatic context propagation, scheduled jobs (which bind a system context explicitly, since there is no request), and message listeners for Kafka or other brokers, which restore context from message headers. Outgoing HTTP calls and messages write the context into headers, so the next service can restore it.
| Boundary | Mechanism |
|---|---|
@Async and task executors | Context-propagating TaskDecorator |
Custom ExecutorService | Created by platform factory, wrapped for context |
| Reactor pipelines | Automatic context propagation into Reactor Context |
| Kafka and other listeners | Restore from message headers, clear after |
| Scheduled jobs | Bind an explicit system context |
| Outgoing HTTP and messages | Write tenant and trace headers |
6Proving It in the Shared Library
The library ships tests for every boundary: context set on one thread is visible inside an @Async method, a CompletableFuture stage, a Reactor operator and a listener; after each task the worker thread has no context left; and a missing context fails closed. A test with a single-threaded executor running two tasks from two different requests proves clearing, which is the failure that leaks data between tenants. ArchUnit or a similar check can forbid new ThreadLocal fields for these concepts outside the library.
7Follow-up questions
Why not pass the context as method parameters everywhere?
Explicit parameters are the clearest option for small codebases and for domain values that business logic uses. But tenant, identity and trace context are needed by cross-cutting code such as logging, data access filters and clients, deep in call stacks we do not own. Implicit context through one controlled accessor is the practical compromise, as long as it is propagated and cleared reliably.
How do we roll this out without a big-bang migration?
Ship the library in the platform starter, migrate shared libraries first, then let services adopt the accessor as they touch code. Old ThreadLocals can be bridged into the new context so both work during the transition, and a lint rule stops new ones.
What changes when services move to virtual threads?
Pooled-thread leaks become rarer, because threads are not reused, but missing context at boundaries remains, and ThreadLocal caches become expensive. That makes the standard more valuable, and it is the moment when ScopedValue becomes attractive for platform-owned context.
8Conclusion
In this answer, we set a platform standard for request context: one immutable context object behind one accessor, propagated by Micrometer context-propagation across executors, Reactor and listeners, with capture and restore at every async boundary the platform provides. We planned ScopedValue, final in Java 25, as the storage for new platform-owned context while keeping ThreadLocal bridges for frameworks, and required the shared library to test propagation and clearing at each boundary.
Open with
I'd standardise on one context and one mechanism, owned by the platform. First, a single immutable RequestContext with tenant, identity and locale, plus access to the trace context, read only through one accessor in a shared library, and that accessor fails closed if nothing is bound.
Then, if they want more
Teams stop declaring their own ThreadLocals. Second, propagation goes through Micrometer's context-propagation library, which can snapshot every registered ThreadLocal and restore it on another thread, and bridges to Reactor's Context, so blocking, async and reactive code behave the same.
Third, the platform covers every async boundary it provides: the task executors and @Async through a context-propagating TaskDecorator, any executor created through our factory, Reactor's automatic propagation, message listeners that restore from headers, scheduled jobs with an explicit system context, and outgoing calls that write headers.
Every restore is paired with a clear in a finally, so nothing leaks to the next task. Fourth, I'd plan for ScopedValue, which is final in Java 25.
It fits request context well because it's immutable and can't outlive its scope, but logging, transactions and security still read ThreadLocals, so for now it's the storage for platform-owned values behind the accessor, with bridges for the frameworks. Finally, the library ships tests for propagation and clearing at every boundary, and a lint rule blocks new ad-hoc ThreadLocals.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 25
- 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
- java.util.concurrent, package 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 "Tenant id, user identity, trace context and locale are propagated today through...": https://scrivio-interview-library.netlify.app/java/threads/28c40aafab/