Scrivio Interview library
LeadershipAdvanced7 min read · updated 12 November 2026

A senior engineer on your team wrote a lock-free cache that relies on careful reasoning about happens-before, and claims it is 20% faster than using ConcurrentHashMap. It will be used by many services. How do you decide whether to accept it?

I would accept the lock-free cache only if three things are true: the author can write down the happens-before argument for every shared field and code path, jcstress tests on the critical interactions pass on both x86 and ARM, and the 20% gain shows up at the service level rather than only in a microbenchmark. Then I would weigh who can maintain and review it after the author moves on. If the gain matters and the risk is contained behind a small, well-tested interface, it can go in; otherwise ConcurrentHashMap stays.

Why this question is asked

A shared library used by many services is a multiplier: a 20% speedup helps everyone, and a subtle race corrupts data everywhere at once. Lock-free code, code that coordinates threads with atomic operations and memory ordering instead of locks, is where such races hide, because it is correct only if a chain of reasoning about the Java Memory Model holds for every interleaving. Interviewers ask this question to see whether a senior engineer can evaluate a clever contribution fairly, respecting the author's work while protecting the organisation from risk it cannot see. In this answer, we will go through how I would check correctness, how I would check the performance claim, how I would weigh maintenance, and how the final decision comes together.

1What Makes This Decision Hard

ConcurrentHashMap is part of the JDK. It has been tuned and tested for many years by people who specialise in exactly this problem, it is specified in detail, and it behaves the same on every platform Java supports. A custom lock-free cache starts with none of that. Its correctness depends on reasoning about happens-before, the rule that decides when one thread's writes are visible to another, and a single missing edge means a reader can see a half-built entry or a stale value.

So the question is not whether the author is skilled. It is whether we, as an organisation, can verify the claim now and keep it true as the code changes. I would make the evaluation explicit and share the criteria with the author upfront, so it feels like a review process and not a verdict.

2The Written Happens-Before Argument

I would ask the author for a short document that lists every field that more than one thread touches, and for each one: who writes it, who reads it, what access mode is used (plain, volatile, VarHandle acquire/release, compare-and-set), and which happens-before edge makes each write visible to each read. For each public method, it should state what a caller is guaranteed to see, for example that a get() returning a value sees the value fully constructed.

Writing it down does two things. It often reveals gaps the author had not noticed, because a reasoning that feels complete in one's head has holes on paper. And it makes review possible: a second expert can check the argument line by line instead of trying to reconstruct it from the code. If the author cannot produce this argument, that alone is a strong signal to say no.

3Stress Tests With jcstress on x86 and ARM

jcstress is the OpenJDK tool for testing concurrent code. A test runs two or more small actors against shared state millions of times and records every outcome; we declare which outcomes are acceptable and which are forbidden. It is the best available way to catch reorderings that ordinary tests never trigger.

Java
@JCStressTest
@Outcome(id = "42", expect = Expect.ACCEPTABLE, desc = "entry fully visible")
@Outcome(id = "-1", expect = Expect.ACCEPTABLE, desc = "entry not yet published")
@Outcome(id = "0", expect = Expect.FORBIDDEN, desc = "half-built entry seen")
@State
public class PutGetVisibility {
final LockFreeCache<String, Box> cache = new LockFreeCache<>();
@Actor public void writer() { cache.put("k", new Box(42)); }
@Actor public void reader(I_Result r) {
Box b = cache.get("k");
r.r1 = (b == null) ? -1 : b.value; // 0 would mean we saw the box before its field
}
}

The important detail is running the tests on ARM as well as x86. x86 has a strong hardware memory model and hides many reorderings; ARM allows far more of them. A test that passes only on x86 has proved much less than it seems. I would ask for tests on the critical interactions: put racing with get, two puts on the same key, eviction racing with get, and resizing if the structure resizes. The tests should run in CI on both architectures.

4Does 20% Matter for the Services?

A 20% gain usually comes from a microbenchmark, a small JMH test that calls the cache in a tight loop. That number is real, but it measures the cache alone. What matters is the effect on a service. If a request spends 50 microseconds in cache calls out of a 20 millisecond total, a 20% faster cache saves 10 microseconds, which is 0.05% of the request: invisible.

So I would ask for a measurement in at least one real service, under production-like load, with p50 and p99 latency, throughput and CPU compared between the two implementations. I would also check that the benchmark matches real use: realistic key distribution, read and write ratio, number of threads and cache size. ConcurrentHashMap is very fast for read-heavy loads, and custom structures often win only in one specific pattern.

How a microbenchmark gain translates
MeasureConcurrentHashMapLock-free cache
JMH get(), ns per op2520 (20% faster)
Cache time per request50 us40 us
Total request time p5020.00 ms19.99 ms
Service-level gain-0.05%
Illustrative numbers: a real gain in the benchmark can disappear at the service level.

5Who Maintains It Later

Even correct lock-free code has a cost after it is merged. Every future change needs a reviewer who can redo the happens-before reasoning, and in most organisations there are only a handful of such people. If the author leaves, the code becomes something everyone is afraid to touch, and bug reports against it are hard to diagnose. I would ask who the second owner is, whether the code has a narrow interface that hides the clever parts, and whether we can switch back to ConcurrentHashMap behind that interface with a configuration flag if a problem appears.

6Making the Decision

Putting it together, the decision has a clear shape. If the service-level gain is negligible, I would thank the author, keep ConcurrentHashMap, and perhaps suggest publishing the work as an internal experiment. If the gain matters, for example a hot path in a service where cache operations are a measurable share of CPU, then the correctness argument, the jcstress tests on both architectures and a second owner become the conditions for acceptance. With all of them, I would accept it behind an interface with a fallback, roll it out to one service first, and expand gradually.

Step throughShould we accept the custom cache?
  1. Gain visible at service level?
    YesCheck correctnessNoKeep ConcurrentHashMap
  2. Written happens-before argument holds?
    YesRun stress testsNoAsk for fixes or reject
  3. jcstress passes on x86 and ARM?
    YesCheck ownershipNoAsk for fixes or reject
  4. Second owner and fallback flag?
    YesAccept, roll out to one serviceNoWait until there is one

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

This way the author's effort is respected and evaluated on evidence, and the organisation takes on risk only where there is a real return for it.

7Follow-up questions

Isn't asking for all this discouraging to strong engineers?

It can be if it feels like distrust, which is why I share the criteria early and apply them to everyone, including myself. Most strong engineers welcome a clear bar, and the written argument and stress tests often improve their own confidence in the code. The message is that shared libraries carry other teams' risk, so they need evidence that can be checked by someone other than the author.

What if jcstress passes but you still have doubts?

Passing jcstress shows that the tested interleavings did not produce forbidden results on those machines; it does not prove correctness. So I would rely on the written argument plus an independent review as the main evidence, and treat the tests as a check that the argument matches the code. Limiting the rollout and keeping a fallback flag contain the remaining risk.

8Conclusion

In this answer, we treated the lock-free cache as a trade between a measured gain and a risk that is hard to see. We asked for the happens-before argument in writing for every shared field, jcstress tests on the critical interactions on both x86 and ARM, proof that the 20% matters at the service level, and a plan for ownership and fallback. Only when the gain is real and the risk is contained would we accept it; otherwise the standard library remains the better choice for code that many services share.

What to say in the interviewAbout 100 seconds aloud

Open with

I would judge it on evidence rather than on the author's reputation, and I would share the criteria with the author upfront. First, correctness: I would ask for the happens-before argument in writing, listing every field shared between threads, how it is accessed, and which edge makes each write visible to each read, plus what each public method guarantees to callers.

Then, if they want more

Then I would ask for jcstress tests on the critical interactions, like put racing with get, eviction racing with get and resizing, with forbidden outcomes declared, run in CI on both x86 and ARM, because x86 hides many reorderings that ARM exposes.

Second, value: a 20% gain in a microbenchmark often disappears at the service level, so I would want measurements in a real service under realistic load, comparing p50, p99, throughput and CPU. If cache calls are a tiny share of request time, the gain is not worth the risk.

Third, maintenance: lock-free code needs reviewers who can redo the reasoning for every change, and we have few of them, so I would want a second owner, a narrow interface, and a flag to fall back to ConcurrentHashMap.

If the gain matters and all of that is in place, I would accept it and roll it out to one service first. If not, I would keep the standard library, and maybe look at Caffeine or at how we use ConcurrentHashMap, since usage fixes often give much of the speedup with no new concurrency code.

About this answer

Last updated
12 November 2026
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 "A senior engineer on your team wrote a lock-free cache that relies on careful reasoning...": https://scrivio-interview-library.netlify.app/java/java-memory-model/2c459322ee/