Quick revision
The short answer
Micrometer's context-propagation library defines ThreadLocalAccessors, small adapters that know how to read, set and clear a given ThreadLocal, and uses them to copy values between ThreadLocals and Reactor Context. With Hooks.enableAutomaticContextPropagation(), available since Reactor 3.5.3, Reactor uses those accessors to restore the ThreadLocals around operator callbacks, so a log line inside map runs with the right MDC. Spring Boot enables it with spring.reactor.context-propagation=auto (check your version), and Micrometer Tracing registers accessors so the current span and the MDC trace id are restored; the cost is extra work per callback, which we should measure on hot paths.
What to remember
In this answer, we saw how automatic context propagation bridges ThreadLocals and Reactor Context. The Micrometer context-propagation library defines ThreadLocalAccessors registered in a ContextRegistry; Hooks.enableAutomaticContextPropagation(), from Reactor 3.5.3, captures ThreadLocal values into the Context at subscription and restores and resets them around every operator callback. Spring Boot turns it on with spring.reactor.context-propagation=auto, depending on version, and Micrometer Tracing registers the accessors that bring back the current span and the MDC trace id. We also saw its overhead and the cases it does not cover.
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.
Micrometer's context-propagation library defines ThreadLocalAccessors, small adapters that know how to read, set and clear a given ThreadLocal, and uses them to copy values between ThreadLocals and Reactor Context. With Hooks.enableAutomaticContextPropagation(), available since Reactor 3.5.3, Reactor uses those accessors to restore the ThreadLocals around operator callbacks, so a log line inside map runs with the right MDC. Spring Boot enables it with spring.reactor.context-propagation=auto (check your version), and Micrometer Tracing registers accessors so the current span and the MDC trace id are restored; the cost is extra work per callback, which we should measure on hot paths.
Why this question is asked
Reactor Context solved the problem of carrying request data across threads, but it left another one: most libraries, including logging frameworks and tracing, read their data from ThreadLocals. Asking every developer to copy values by hand inside each operator never worked well. Spring Boot 3 and Micrometer solved this with automatic context propagation, and interviewers ask how it works because it is a common source of confusion when trace ids still go missing. In this answer, we will recap the two worlds that need bridging, see the accessors that bridge them, follow what the Reactor hook does around each operator, turn it on in Spring Boot, and look at its cost and limits.
1A Quick Recap: Two Places to Keep Request Data
Thread-bound code keeps per-request data in ThreadLocals: SLF4J's MDC holds the trace id for log lines, Micrometer's observation registry holds the current observation, and a tracer holds the current span. Reactive code keeps per-request data in the Reactor Context, which is attached to the subscription and survives thread switches.
Neither side knows about the other. A log statement inside map reads MDC, a ThreadLocal, but the trace id lives in the Context. Automatic context propagation is the bridge: it takes the values from the Context and places them into the ThreadLocals for exactly as long as our callback runs on that thread.
2ThreadLocalAccessors: How the Library Knows What to Copy
The io.micrometer:context-propagation library is independent of Reactor. Its central idea is the ThreadLocalAccessor, an interface with four jobs: give the key the value is stored under, read the current ThreadLocal value, set it, and reset it. Accessors are registered in a global ContextRegistry. A library that owns a ThreadLocal registers an accessor for it, and from then on the propagation machinery can capture that value into a snapshot and restore it elsewhere.
Micrometer Tracing registers the accessors we need for tracing: one for the current observation, from which the current span is opened in scope, and that span scope is what puts traceId and spanId into the MDC. So once those accessors run, the logging pattern %X{traceId} sees the right value. This is why we usually do not register anything ourselves for tracing; we only add accessors for our own ThreadLocals, such as a tenant id.
3What the Reactor Hook Does Around Each Operator
Hooks.enableAutomaticContextPropagation(), added in Reactor 3.5.3, switches on the mode. Two things then happen. When thread-bound code consumes a pipeline with block(), or calls contextCapture() explicitly, the current ThreadLocal values are captured into the Reactor Context, so a trace started by an MVC filter or a test enters the reactive world (in WebFlux, Spring's request observation writes the Context for us). And around each operator callback, such as the lambda inside map, filter or doOnNext, Reactor restores the ThreadLocals from the Context before calling our code and resets them afterwards.
6 messages in order. Play them, or step through them one at a time.
Values are set only for the duration of the callback, then reset.
Notice that the map callback runs on a Netty thread that serves many requests, yet the log line shows the correct trace id. Just before the callback, Reactor copied the Context values into the MDC through the accessors; just after it, it reset them, so the next request handled on that thread does not inherit them. The reset step is as important as the set step: it is what prevents the leaked trace ids that hand-written MDC copying often caused.
4Turning It On in Spring Boot
Spring Boot exposes the hook as a property: spring.reactor.context-propagation=auto calls Hooks.enableAutomaticContextPropagation() at startup. In some Boot versions it has been the default when Micrometer Tracing is present, and the property's options have changed across releases, so check your version's documentation. With it enabled, Boot's WebFlux observation support creates an observation per request and stores it in the Context, WebClient built from the injected WebClient.Builder propagates trace headers, and log lines inside our operators carry the trace id.
Before 3.5.3, Reactor offered only explicit restoration through the handle and tap operators with contextCapture(), which still exist and are useful when we want propagation in one place without the global hook.
- Our code
log.infoinside map,flatMap,doOnNext - Reactor automatic propagationrestores and resets
ThreadLocalsper callback - context-propagation library
ContextRegistry,ThreadLocalAccessors, snapshots - Micrometer Tracingregisters accessors, opens span scope, fills MDC
- Spring Boot
spring.reactor.context-propagation, observations,WebClient.Builder
5 layers, from the top down. Step through them one at a time.
5Cost and Limits
Restoring and resetting ThreadLocals around every callback is not free. Each operator call now does a few map lookups and ThreadLocal writes, which is negligible for a typical request with a few dozen operator calls but measurable for a pipeline that processes millions of small items per second. So for hot paths, we measure throughput with propagation on and off in a load test, and if it matters we can keep the global hook off and use contextCapture with handle or tap in the places we need it.
There are also limits. Values only propagate for keys with registered accessors; a custom ThreadLocal without one stays empty. Code that leaves Reactor entirely, such as a task submitted to a plain ExecutorService, needs ContextSnapshot wrapping or a context-aware executor. And a value set into a ThreadLocal inside a callback does not flow into the Context; the Context remains the source of truth, written with contextWrite.
6Follow-up questions
Why do some log lines still lack a trace id after enabling it?
Usually because the code is outside a reactive callback or outside an observation. Examples are logs from a plain thread pool, a WebClient created with WebClient.create() instead of the injected builder (so no observation is created for the call), or a pipeline subscribed with no trace in its Context, such as a scheduled job without an observation of its own.
Does automatic propagation also work with virtual threads or executors?
Not by itself. It is a Reactor feature. For executors, the context-propagation library provides ContextSnapshot to capture values and wrap a Runnable, and Spring offers ContextPropagatingTaskDecorator for task executors, which applies the same accessors when work moves to another thread.
Is it safe to put security data in propagated ThreadLocals?
It is safe in the sense that values are reset after each callback, so they do not leak between requests. But security decisions should still read from the authoritative source, which in WebFlux is ReactiveSecurityContextHolder, rather than from a ThreadLocal restored for logging convenience.
7Conclusion
In this answer, we saw how automatic context propagation bridges ThreadLocals and Reactor Context. The Micrometer context-propagation library defines ThreadLocalAccessors registered in a ContextRegistry; Hooks.enableAutomaticContextPropagation(), from Reactor 3.5.3, captures ThreadLocal values into the Context at subscription and restores and resets them around every operator callback. Spring Boot turns it on with spring.reactor.context-propagation=auto, depending on version, and Micrometer Tracing registers the accessors that bring back the current span and the MDC trace id. We also saw its overhead and the cases it does not cover.
Open with
The mechanism has two parts. The first is Micrometer's context-propagation library, which defines ThreadLocalAccessors: small adapters registered in a global ContextRegistry that know a key and how to read, set and reset a particular ThreadLocal.
Then, if they want more
Micrometer Tracing registers accessors for the current observation, and when the span is put in scope it fills the MDC with the trace id and span id. We can register our own, for example for a tenant id. The second part is Reactor's Hooks.enableAutomaticContextPropagation, available since 3.5.3.
When it is on, blocking calls and contextCapture copy the current ThreadLocal values into the Reactor Context, and around every operator callback, like the lambda in map or doOnNext, Reactor restores the ThreadLocals from the Context before our code runs and resets them afterwards.
So a log line inside map, running on a Netty thread that serves many requests, sees the right trace id and nothing leaks to the next request. In Spring Boot 3 I enable it with spring.reactor.context-propagation set to auto, though defaults have varied, so I check the version.
It costs a little per callback, which I would measure on hot paths, and it only covers registered ThreadLocals inside Reactor callbacks; plain executors need ContextSnapshot or a context-propagating task decorator.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- 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
- Spring Framework reference, Spring WebFlux, Project Reactor reference guide
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 "How does automatic context propagation work in Spring Boot 3 with Reactor and...": https://scrivio-interview-library.netlify.app/spring-boot/webflux/af7af008c4/