Scrivio Interview library

Java performance engineering

24 questions in 7 subtopics.

JMH microbenchmarks

CodingFoundations6 min read
What is wrong with this benchmark?
long start = System.nanoTime();
for (int i = 0; i < 1_000_000; i++) {
parse("2024-01-01");
}
System.out.println((System.nanoTime() - start) / 1_000_000 + " ns per call");
CodingIntermediate7 min read
Write a JMH benchmark comparing StringBuilder with String.join for joining ten strings, and explain how you avoid the common pitfalls.
ScenarioAdvanced6 min read
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?
LeadershipPrincipal6 min read
How would you build a lasting performance engineering culture in a large Java organisation? What roles do microbenchmarks, load tests and production profiling play, and how do you invest in them over years?

Allocation pressure

ConceptFoundations8 min read
Allocation in a TLAB is just a pointer bump, so why does allocation rate still matter for Java performance?
InternalsIntermediate7 min read
Explain thread-local allocation buffers (TLABs) in HotSpot, and what happens when an object is too large for the current TLAB.
DebuggingIntermediate6 min read
JFR shows a service allocating about 2 GB per second, and most of it comes from debug logging: log.debug(String.format("order %s items %s", order.id(), toJson(order.items()))). DEBUG is disabled in production. Explain and fix.
LeadershipPrincipal6 min read
Java's performance model has changed over time: escape analysis, compact strings, better collectors, indy-based string concatenation and virtual threads. Which old performance rules of thumb should your organisation retire, and how do you keep guidance current?

False sharing

ConceptFoundations6 min read
What is a CPU cache line, and what is false sharing?
CodingIntermediate7 min read
Two threads each increment their own counter in
class Counters {
volatile long a;
volatile long b;
}
as fast as possible: thread one updates a, thread two updates b. It runs much slower than with one thread. Explain and fix.

Boxing costs

CodingFoundations6 min read
Why is this loop slow, and what else could go wrong with boxed types?
Long sum = 0L;
for (long i = 0; i < n; i++) {
sum += i;
}
DebuggingFoundations6 min read
A code review finds if (order.getQuantity() == other.getQuantity()) where both getters return Integer. Tests pass with small quantities. What is the bug?
ScenarioIntermediate7 min read
A service keeps a Map<Long, Long> with 50 million entries and it uses roughly 4 GB of heap. Estimate where the memory goes and propose alternatives.

Collection choice and performance

ScenarioFoundations6 min read
A worker queue holds up to one million pending tasks; producers add at the end and consumers remove from the front. A developer used ArrayList with remove(0). What is the problem, and which collection should be used?
DebuggingIntermediate6 min read
A profile of a batch job shows significant time in HashMap.resize. The code creates new HashMap<>(expectedSize) and then inserts exactly expectedSize entries. Why does it still resize, and how do you fix it?
DesignIntermediate6 min read
You need an in-memory structure of price updates keyed by timestamp that supports range queries (all updates between two times) and is read and written by many threads. Which Java collection would you choose and why?

Latency vs throughput

ConceptIntermediate7 min read
Explain latency versus throughput, why average latency is misleading, and how you should measure and report latency for a service.
ScenarioAdvanced7 min read
One deployment of a service handles both interactive API traffic with a 100 ms p99 target and large nightly batch imports. The batch team wants Parallel GC and large thread pools, the API team wants low pauses. How do you resolve this?
DesignAdvanced6 min read
A checkout flow crosses 12 services and has an end-to-end p99 target of 800 ms. Set up performance budgets: how are they split, who owns them, and how are they enforced?
LeadershipPrincipal7 min read
At company level, how do you decide when to invest engineering time in performance work versus buying more hardware, and how do you keep that decision sound over years?

Mechanical sympathy

ConceptFoundations6 min read
What does mechanical sympathy mean for a Java developer? Give two examples where knowing the hardware changes how you write code.
ScenarioIntermediate7 min read
Summing an int[4096][4096] row by row takes 20 ms, but summing it column by column takes over 200 ms. Explain why.
DesignAdvanced8 min read
You are designing a low-latency order matching component in Java with a target of tens of microseconds per order. Which mechanical sympathy principles would guide the design?
LeadershipPrincipal7 min read
Server hardware keeps changing: more cores per socket, ARM-based instances with different price and performance, large caches, and memory bandwidth limits. How should these trends shape your company's Java performance strategy over the next five years?