Scrivio Interview library
LeadershipPrincipal6 min read · updated 12 November 2026

Your organisation has used ThreadLocal for request context for about 15 years: security, tenant, transactions, logging MDC. Virtual threads and scoped values change the trade-offs. What multi-year plan would you set for request context?

The plan starts with an inventory of where ThreadLocals are used, in frameworks, shared libraries and application code, and removes the ones used as per-thread object caches first, because they scale badly with virtual threads. Next, all request context goes behind accessor APIs so its storage can move from ThreadLocal to ScopedValue, final in Java 25, gradually. We accept that frameworks such as logging and transaction management will keep ThreadLocal-based context for years, and we sequence each step with planned JDK and framework upgrades instead of running a separate rewrite.

Why this question is asked

After fifteen years, ThreadLocal is not a design choice in our organisation, it is the ground everything stands on: security, tenant isolation, transactions and logs all depend on it. Changing ground like that is a leadership problem more than a coding problem, because it spans many teams, competes with feature work and carries real risk of data leaking between tenants. Interviewers ask this to see how a senior leader turns a platform shift into a plan that is safe, incremental and funded. In this answer, we will look at why the trade-offs changed, then walk through the plan phase by phase: inventory, removing caches, introducing accessors, coexisting with frameworks and sequencing with upgrades, and finish with how we would measure progress.

1Why the Trade-Offs Changed

ThreadLocal worked well for fifteen years because our threads were few and long-lived: a Tomcat pool of 200 threads, each handling one request at a time. Context set at the start of a request and cleared at the end was cheap and reliable, as long as everyone cleared it.

Virtual threads change the shape of threads: one per request or even per sub-task, possibly hundreds of thousands at once, each short-lived. ThreadLocal still works for small context values, but ThreadLocals used as per-thread caches now allocate one copy per task, and mutable context copied into many child threads becomes expensive and confusing. ScopedValue, final in Java 25, offers a model designed for this world: an immutable value bound for a scope, readable by everything called inside it, and automatically gone when the scope ends. The opportunity is real, but nothing forces us to switch everything, which is why this calls for a plan rather than a mandate.

2Phase 1: Inventory Where ThreadLocals Live

We cannot plan what we have not counted. A short platform-team effort scans all repositories and dependencies for ThreadLocal and InheritableThreadLocal fields and classifies each one: framework context (Spring Security, transaction synchronisation, Logback MDC, Hibernate session binding), shared-library context (our tenant and locale holders), application context, and object caches (formatters, buffers, encoders). For each, we record the owner, whether it is mutable, whether it is cleared reliably and whether it crosses async boundaries.

This inventory is also a risk register. ThreadLocals that are not cleared in finally blocks are potential tenant leaks today, independent of any migration, and they go to the top of the list.

Classifying the inventory
CategoryExamplePlan
Object cachesPer-thread buffers, formattersRemove first
Our request contextTenant, user, locale holdersMove behind accessor, then ScopedValue
Framework contextSecurity, transactions, MDCKeep; follow framework releases
Unsafe clearingset without finally removeFix immediately: leak risk

3Phase 2: Remove ThreadLocal Caches First

Caches are the cheapest and most valuable place to start. They are usually local to one class, they do not change behaviour when removed, and they are exactly what breaks with virtual threads: a 1 MB buffer cached per thread becomes a 1 MB allocation per request. Replacing them with plain allocation, an immutable shared instance (such as DateTimeFormatter instead of SimpleDateFormat) or a bounded pool is a small change per occurrence. Doing this first also removes the main blocker for teams that want to enable virtual threads early, which gives the program visible wins in the first quarter.

4Phase 3: Hide Context Behind Accessor APIs

The key architectural move is to stop letting code touch the storage. The platform library offers one accessor, for example Ctx.current().tenantId(), and one way to bind context for a unit of work, and application code uses only those. Behind the accessor, the storage starts as the existing ThreadLocal, propagated with a library such as Micrometer context-propagation. Once all reads go through the accessor, the platform team can move the storage of its own values to ScopedValue one service at a time, without application teams changing code.

Java
// Before: every team reads storage directly
String tenant = TenantHolder.TENANT.get(); // static ThreadLocal, cleared by convention
// After: one accessor; storage is a platform detail
String tenant = Ctx.current().tenantId(); // ThreadLocal today, ScopedValue later

This phase is the longest, because it touches many codebases. We make it cheap by providing automated refactoring recipes, for example with OpenRewrite, and a lint rule that blocks new direct ThreadLocal context.

5Phase 4: Coexist With Frameworks for Years

We should say plainly in the plan that some ThreadLocal context is not ours to migrate. Spring's transaction management, Spring Security's context holder and logging MDC are ThreadLocal-based, and they will move only when their maintainers decide, if ever. Fighting that would mean forking frameworks. Instead, our accessor bridges into them: when we bind a request context, the platform also sets the MDC keys and the security context, and clears them together. Coexistence is a stable end state, not a failure of the plan.

6Phase 5: Sequence With Upgrades, Not a Rewrite

A dedicated rewrite program would compete with feature work and lose. So we attach each step to work teams already do. Removing caches rides along with enabling virtual threads. Adopting the accessor rides along with the move to the next Spring Boot major version, which already requires touching configuration and dependencies. Moving storage to ScopedValue rides along with the JDK 25 upgrade, the first LTS where it is final. Each step is small, has its own rollback, and is measured.

Step throughThe multi-year plan
  1. Year 1, quarter 1inventory, fix unsafe clearing
  2. Year 1remove ThreadLocal caches, enable virtual threads where ready
  3. Year 1 to 2accessor API in platform library, migrate with Boot upgrade
  4. JDK 25 adoptionScopedValue storage for platform-owned context
  5. Ongoingframework ThreadLocals bridged, follow their releases

5 steps in order. Play them, or step through them one at a time.

7Measuring Progress and Making the Case

Leaders need to show progress in terms others care about. We track the number of unsafe ThreadLocal usages (target zero within a quarter), the number of services on the accessor, the share of services running on virtual threads, and incidents involving context, such as wrong tenant or missing trace ids. The business case is risk and capacity: tenant leaks are a security exposure, and virtual threads let services handle more concurrency on the same hardware once caches are gone. Publishing this dashboard keeps the program funded without a big launch.

8Follow-up questions

What would make you accelerate the plan?

A tenant-leak incident, a security audit finding, or a capacity problem that virtual threads would solve. Each would justify moving the accessor migration earlier for the affected services. The phases stay the same; only their order across services changes.

Should every team adopt ScopedValue directly?

No. Direct use spreads the storage decision across hundreds of codebases again, which is what we are trying to undo. Teams use the accessor, and the platform decides the storage. Teams may use ScopedValue directly for local, self-contained needs inside one service.

How do you get teams to do the work?

Make the right thing the easy thing: the platform starter brings the accessor and propagation by default, refactoring recipes do most of the change, and the work is attached to upgrades teams must do anyway. A short, visible list of services remaining helps more than a mandate.

9Conclusion

In this answer, we built a multi-year plan for request context. We start with an inventory that also finds unsafe clearing, remove ThreadLocal caches first because they scale badly with virtual threads, then hide all request context behind a platform accessor so storage can move to ScopedValue gradually. We accept that frameworks will keep ThreadLocal context for years and bridge to them, and we sequence every step with JDK and framework upgrades instead of a separate rewrite, measuring progress with a few clear numbers.

What to say in the interviewAbout 90 seconds aloud

Open with

I'd treat this as a gradual platform migration, not a rewrite. The trade-offs changed because virtual threads make threads plentiful and short-lived, which breaks ThreadLocal caches, and ScopedValue, final in Java 25, gives us an immutable, scope-bound alternative for context.

Then, if they want more

Step one is an inventory across frameworks, shared libraries and services, classifying each ThreadLocal as a cache, our own request context, framework context, or unsafe because it isn't cleared in a finally. The unsafe ones get fixed immediately because they're tenant-leak risks today.

Step two is removing the caches, like per-thread buffers and formatters, since they're cheap to replace and they're what blocks virtual threads. Step three, the longest, is hiding all our request context behind one accessor in the platform library, so application code never touches storage.

Then the platform team can switch our own values from ThreadLocal to ScopedValue service by service without teams changing code, using OpenRewrite recipes and a lint rule to make migration cheap. I'd also be explicit that Spring transactions, Spring Security and logging MDC will stay ThreadLocal-based for years, so our accessor bridges into them, and that's a fine end state.

Finally, I'd sequence each step with upgrades we're doing anyway: caches with virtual threads, the accessor with the next Spring Boot major, ScopedValue with the JDK 25 move, and track unsafe usages, adoption and context incidents on a dashboard.

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.
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 organisation has used ThreadLocal for request context for about 15 years: security...": https://scrivio-interview-library.netlify.app/java/threads/f8fe55c00f/