Scrivio Interview library
ScenarioAdvanced6 min read · updated 12 November 2026

A team shows a JMH result where their rewritten hashing routine is 30 percent faster, and wants to replace the routine in a shared library used by 80 services. How do you evaluate the proposal?

A 30 percent faster microbenchmark is a hypothesis, not a decision. First we check with production profiles how much time services actually spend in the routine, because 30 percent of 1 percent of CPU is only 0.3 percent. Then we reproduce the benchmark with several forks, realistic inputs and the JDK versions we run, verify that outputs are identical including edge cases and across platforms, and prove the gain with a load test or canary in one service. Only then do we weigh the measured benefit against maintenance cost, readability and the risk of changing a library used by 80 services.

Why this question is asked

A shared library is the most leveraged code in an organisation: one change reaches every service that depends on it, which is why performance improvements there are tempting and why mistakes there are expensive. A hashing routine is especially sensitive, because a changed output can silently break caches, partitioning or stored data. Interviewers ask this scenario to see whether a senior engineer can evaluate a performance claim with the right scepticism, without killing a good idea. In this answer, we will measure the real-world value of the gain, check the benchmark itself, verify correctness, test at system level, and then make the decision.

1How Much Time Do Services Spend Here?

A microbenchmark tells us how fast the routine is in isolation. It says nothing about how much that matters. The answer comes from production profiles: continuous profiling or JFR recordings of the services that use the library, showing what share of their CPU is spent inside the hashing routine.

Suppose the profiles show that the routine accounts for 1 percent of CPU in a typical service. Making it 30 percent faster saves 0.3 percent of CPU, which is within normal noise and unlikely to change latency or cost. If instead one or two services spend 15 percent of their CPU there, the same change saves about 4.5 percent for them, which may be worth a lot. This is Amdahl's law in practice: the overall gain is limited by the share of time the improved part takes.

What a 30 percent faster routine is worth
Share of service CPU in the routineOverall CPU savedWorth the risk?
0.5%0.15%almost certainly not
1%0.3%unlikely, within noise
5%1.5%maybe, for large fleets
15%4.5%probably, for those services
Overall saving = share of CPU in the routine x 30 percent.

2Reproduce the Benchmark Properly

Before believing the 30 percent, we check how it was measured. Microbenchmarks are easy to get wrong, and a hashing routine is especially sensitive to its inputs. We rerun the JMH benchmark with several forks, typically three to five, because JIT decisions vary between JVM launches, and we look at the error margins rather than just the scores.

We also use realistic inputs. If production hashes keys of 20 to 200 bytes, a benchmark that hashes a single 8-byte value, or the same value every time, measures a different case; the new routine may be faster for short keys and slower for long ones. We add a @Param for input sizes taken from production. And we run on the JDK versions the 80 services actually use, for example JDK 21 and JDK 25, because intrinsics and JIT behaviour change between versions, and an optimisation that wins on one version can disappear on another. Running on the CPU architectures in production, such as x86 and ARM, matters for the same reason.

3Check Correctness and Compatibility

For a hashing routine, correctness means producing exactly the same output as the old one for every input, unless the change is deliberately a new hash function, which is a much bigger decision. If any service stores hashes, uses them to partition data, route requests, name cache keys or compare with values computed by other systems, a different output silently breaks things: cache misses everywhere, data routed to the wrong partition, or records that can no longer be found.

So we test identical outputs over a large set of inputs: random data, production-like keys, empty input, very long input, inputs with every byte value, unusual Unicode strings and boundary lengths around the internal block size. Property-based tests that compare the old and new routines on millions of generated inputs are ideal here. We also check behaviour across platforms: rewritten routines sometimes depend on byte order, on unsafe memory access, or on vectorised operations, and must give the same result on every architecture and JDK in use.

Java
@Property
void newHashMatchesOldHash(@ForAll byte[] input) {
assertEquals(OldHasher.hash(input), NewHasher.hash(input)); // must hold for every input
}
// known edge cases, checked explicitly
assertEquals(OldHasher.hash(new byte[0]), NewHasher.hash(new byte[0])); // empty
assertEquals(OldHasher.hash(new byte[1_000_000]), NewHasher.hash(new byte[1_000_000])); // long

If the outputs differ even once, the proposal changes from a performance improvement to a migration, with versioning of stored hashes and a coordinated rollout, which is a very different cost.

4Prove It at System Level

A microbenchmark measures the routine with a hot cache and nothing else running. In a real service, the routine competes with everything else for CPU caches, and the JIT might inline it differently inside a larger call chain. So the next step is to try the change in one service that spends a meaningful share of time in the routine.

We run a load test of that service with the old and new library versions, at the same traffic, and compare CPU per request, latency percentiles and allocation. Or we deploy a canary, a small share of instances running the new version, and compare them with the rest under real traffic. If the profiles predicted a 1.5 percent CPU saving and the canary shows about that, the claim is confirmed; if it shows nothing, the benchmark did not represent reality.

Step throughEvaluating the proposal
  1. Production profilesshare of CPU in the routine
  2. Is the share large enough to matter?
    YesContinueNoThank the team, keep the old code
  3. Rerun JMH: forks, real inputs, our JDKs
  4. Identical outputs on all inputs and platforms
  5. Load test or canary in one serviceCPU per request, latency
  6. System-level gain confirmed?
    YesWeigh against cost and riskNoStop, the benchmark misled

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

5Weigh the Gain Against Cost and Risk

With measured numbers in hand, we make the decision. On the benefit side is the confirmed saving across the services where it matters, perhaps translated into money: 1.5 percent of CPU across a fleet costing 2 million a year in compute is 30,000 a year. On the cost side is maintenance: a rewritten routine using low-level tricks is harder to read and to fix, and someone must own it. There is also the risk of a subtle bug reaching 80 services at once.

If we go ahead, we roll out gradually: a new library version, adopted first by the services that benefit most, then by others through normal upgrades, with the old implementation available behind a flag for a release or two. If the gain is small, the right answer is often to keep the simpler code and thank the team, explaining the measurements, so they know where their effort would matter more, for example in the code that production profiles show is really hot.

6Follow-up questions

What if only one service benefits significantly?

Then that service can adopt the faster routine itself, or the library can offer it as an opt-in alternative, without changing the default for everyone. This gives the benefit where it exists and avoids risk for the 79 services where it does not matter.

How do you keep the conversation constructive with the team?

By treating their result as a promising hypothesis and evaluating it together with clear criteria agreed in advance: share of CPU, reproduced benchmark, identical outputs, system-level gain. If the change does not go in, the team still learns how to evaluate performance work, which is valuable in itself.

7Conclusion

In this answer, we evaluated a proposal to replace a hashing routine in a shared library because a JMH benchmark showed it 30 percent faster. We checked with production profiles how much CPU the routine actually uses, reproduced the benchmark with forks, realistic inputs and our JDK versions, verified identical outputs across edge cases and platforms, confirmed the gain with a load test or canary in one service, and weighed the measured benefit against maintenance cost, readability and the risk of a fleet-wide change.

What to say in the interviewAbout 90 seconds aloud

Open with

I would treat the 30 percent as a hypothesis. First, I would look at production profiles for the services using the library to see how much CPU is really spent in this routine.

Then, if they want more

If it is 1 percent, a 30 percent improvement saves 0.3 percent, which is noise and not worth touching code used by 80 services. If some services spend 10 or 15 percent there, it becomes interesting for them.

Second, I would rerun the JMH benchmark with several forks, realistic input sizes taken from production, and the JDK versions and CPU architectures we actually run, because hashing performance depends on input size and on JIT intrinsics that change between versions.

Third, correctness: the new routine must produce exactly the same output for every input, because hashes may be stored, used for partitioning or for cache keys. I would run property-based tests against the old implementation on millions of inputs plus edge cases like empty and very long input, on every platform.

Fourth, I would prove it at system level with a load test or a canary in one service that spends real time there, comparing CPU per request. Only then would I weigh the measured gain against readability, maintenance and the risk of a fleet-wide change, and if we go ahead, roll it out gradually or as an opt-in.

About this answer

Last updated
12 November 2026
Versions it names
Java 21, 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 "A team shows a JMH result where their rewritten hashing routine is 30 percent faster...": https://scrivio-interview-library.netlify.app/java/java-performance/11921baa83/