Scrivio Interview library
LeadershipPrincipal7 min read · updated 12 November 2026

StampedLock offers fast optimistic reads but is not reentrant and is easy to misuse. How do you decide whether performance-oriented APIs with sharp edges should be allowed in a codebase that will live for ten years or more?

I would allow such APIs only when a measured gain matters at the level of the service, not just in a microbenchmark, and only when that gain outweighs the cost of every future engineer having to understand the sharp edges. When they are allowed, they live inside small, well-tested, encapsulated components with named owners and a documented fallback to a simpler construct, and the decision is reviewed again when the hardware, the JDK or the traffic changes.

Why this question is asked

Every mature platform has APIs that are fast and dangerous: StampedLock, VarHandle access modes, off-heap memory, lock-free structures. In a codebase that will live for a decade, the people who change the code most will not be the people who chose the API. Interviewers ask this leadership question to see whether we can make a principled call that neither blocks real engineering nor lets cleverness spread. In this answer, we will look at what makes StampedLock a good example, then build the decision step by step: measuring benefit properly, counting maintenance cost, containing the API, keeping a way back, and revisiting the decision over time.

1Why StampedLock Is a Good Example

StampedLock's optimistic read takes no lock and writes no shared memory, so read-mostly code can scale across cores far better than with a read-write lock. That benefit is real. But its sharp edges are just as real: it is not reentrant, so a method that holds the write lock and calls a helper that takes it again hangs forever; it has no conditions; it does not track owner threads; and optimistic reads may see inconsistent values, so the code must copy, validate, then act.

None of those edges shows up in a quick test, and most of them appear only when someone later changes the call graph. That is the general shape of a sharp-edged API: the cost is deferred and lands on someone else.

2Question 1: Does the Gain Matter at the Service Level?

A microbenchmark may show that an optimistic read is five times faster than a read lock. That is a fact about the lock, not about the service. If the lock accounts for 0.5 percent of request time, making it five times faster saves 0.4 percent, which nobody will ever notice. If the service spends 30 percent of its CPU contending on that lock, as a pricing or routing hot path might, the same change could cut hardware or latency meaningfully.

So the first question is always: what does a profile of the real service show, and what improvement in p99 latency, throughput or cost would this change bring? The evidence should come from a load test or production profiling, with JFR or async-profiler showing lock contention, not from a benchmark of the lock alone.

Same microbenchmark gain, different service value
Share of request time in the lockLock made 5x fasterWorth the sharp edges?
0.5%about 0.4% faster requestsno
5%about 4% faster requestsrarely
30% (hot shared lock)about 24% faster requestspossibly, with safeguards
Numbers are illustrative; the point is to measure the share first.

3Question 2: What Will It Cost to Maintain?

The cost side is mostly people's time and risk. Every engineer who changes the component must understand the API's rules, and over ten years that could be dozens of people, many of them not concurrency specialists. Each misunderstanding risks a bug that is rare, hard to reproduce and often found in production, like a hang that appears only on one call path. Reviews take longer, onboarding takes longer, and refactoring becomes risky.

A useful way to weigh it is to ask: if the original author left tomorrow, could the team safely change this code next year? If the honest answer is no, the gain has to be large to justify it.

4Containing the API in a Small, Owned Component

When the gain is worth it, the second half of the decision is containment. The sharp-edged API should sit inside one small class with a simple, safe public interface, so the rest of the codebase never touches it. For StampedLock, that might be a PriceBook class with get() and update() methods; callers do not know or care which lock is inside.

The component needs a named owner, a team or person accountable for it, and tests that cover its concurrency: every public method under contention with a timeout, so a reentrancy hang fails the build, and ideally a stress test with a tool such as jcstress for the optimistic path. A short comment at the top should explain why the API was chosen and what rules any change must follow.

Java
/**
* Read-mostly price table. Uses StampedLock optimistic reads: profiling showed 28% of
* request CPU in the previous ReadWriteLock (load test LT-2026-03). Owner: pricing-core.
* Rules: StampedLock is NOT reentrant - public methods lock once, private *Locked methods never lock.
* Fallback: SimplePriceBook (ReentrantReadWriteLock), selectable with pricing.book=simple.
*/
public final class PriceBook {
private final StampedLock sl = new StampedLock();
// ...
}

Notice that the comment carries the evidence, the rules and the way back. Most of the cost of a sharp-edged API comes from people not knowing those three things.

5Keeping a Documented Fallback

A fallback is a simpler implementation of the same interface, using a standard construct such as ReentrantReadWriteLock, a plain lock or an immutable snapshot, that can be switched on by configuration. It has three uses. It is the safe choice when a bug in the fast version appears in production, so we can switch without an emergency rewrite. It is the baseline for benchmarks when we revisit the decision. And it is the obvious path if the component's owner leaves and nobody else is comfortable maintaining the fast version.

Keeping the fallback tested costs a little, but it turns the decision from a one-way door into a two-way door, which is exactly what we want for a choice with long-term maintenance risk.

Step throughDeciding on a sharp-edged API
  1. Profile shows a meaningful service-level gain?
    YesContinueNoUse the standard construct
  2. Can the team maintain it if the author leaves?
    YesContinueNoGain must be very large, or reject
  3. Encapsulate in one small componentsafe public API, named owner
  4. Add concurrency tests and a documented fallback
  5. Record a review triggerJDK upgrade, hardware change, traffic shift

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

6Reviewing the Decision When Things Change

The balance of cost and benefit is not fixed. Hardware changes: more cores make contention worse and may increase the gain, while a move to different processors can change the numbers entirely. The JDK changes: monitors and locks have been improved many times, and JEP 491 in JDK 24 changed how virtual threads behave inside synchronized, which shifted several locking recommendations. Traffic changes: a hot path can become cold when a feature is retired.

So the decision should carry a review trigger, recorded with the component: re-run the benchmark against the fallback at each LTS upgrade, after a hardware migration or when the service's traffic pattern changes. If the gain has shrunk, we switch to the simpler version and delete the sharp edge. Removing complexity that no longer pays is as much a part of the policy as admitting it in the first place.

7Follow-up questions

Would you ever ban an API like StampedLock outright?

I would not ban it, because some hot paths really need it, but I would make it impossible to use casually: a static analysis rule flags it outside approved components, and adding a new use needs the evidence and safeguards described. That is a ban by default with a reviewed exception.

How do you handle an expert who insists the fast version is obviously better?

I ask for the service-level evidence and for the maintenance plan, not for theory. Experts are usually right about the mechanism; the question is whether the gain matters here and whether others can maintain it. If both answers are good, they get to build it within the safeguards.

What if the original owner leaves?

That is one of the review triggers. The new owner either takes it on, with the comment, tests and fallback making that feasible, or the team switches to the fallback. Without an owner, the sharp-edged version should not stay in production.

8Conclusion

In this answer, we built a policy for performance APIs with sharp edges such as StampedLock. Allow them only when profiling shows a gain that matters at the service level, not only in a microbenchmark, and when that gain outweighs the long-term cost of every future engineer having to understand the API. When allowed, contain them in a small, tested, encapsulated component with a named owner, a comment carrying the evidence and rules, and a documented, switchable fallback to a simpler construct, and revisit the decision whenever the hardware, the JDK or the traffic changes.

What to say in the interviewAbout 90 seconds aloud

Open with

I treat it as a cost-benefit decision with an expiry date. On the benefit side, I want evidence that the gain matters at the service level, not just in a microbenchmark.

Then, if they want more

A StampedLock optimistic read might be five times faster than a read lock, but if the lock is half a percent of request time, nobody will notice. If profiling shows a hot lock taking a third of the CPU, it may be worth it.

On the cost side, I think about the next ten years: most people who change this code will not be concurrency specialists, and StampedLock's edges, no reentrancy, no conditions, no owner, and the copy-validate-act rule for optimistic reads, cause rare bugs like hangs on one call path.

So if I allow it, it goes inside one small component with a safe public API and a named owner, so nobody else touches the sharp edges. It needs tests that exercise every public method under contention with timeouts, and a comment that records the evidence, the rules and the fallback.

The fallback is a simpler implementation behind the same interface, switchable by configuration, so the decision is reversible. Finally I record review triggers: an LTS upgrade, a hardware change or a traffic shift means we re-benchmark against the fallback, and if the gain is gone, we switch to the simple version and delete the complexity.

About this answer

Last updated
12 November 2026
Versions it names
Java 24
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 "StampedLock offers fast optimistic reads but is not reentrant and is easy to misuse. How...": https://scrivio-interview-library.netlify.app/java/locks/6149c75526/