Scrivio Interview library

Concurrency problems and diagnosis

26 questions in 7 subtopics.

Deadlock

ConceptFoundations6 min read
What are the four conditions that must all hold for a deadlock, and which one is usually easiest to break in application code?
CodingFoundations7 min read
Thread 1 runs
synchronized (a) {
synchronized (b) { ... }
}
and thread 2 runs
synchronized (b) {
synchronized (a) { ... }
}
Explain why this can deadlock, how you would see it in a thread dump, and how to fix it.
DebuggingIntermediate7 min read
A service hangs under load. The thread dump shows no Java-level deadlock, but all 16 threads of the report-pool are WAITING in FutureTask.get, and the pool's queue contains subtasks submitted by those same threads. Explain.
ScenarioIntermediate7 min read
Each request in a service opens a transaction with one connection and then calls a helper that requests a second connection from the same Hikari pool of size 10. Under load, everything hangs until connection timeouts. Explain and fix.
ScenarioAdvanced6 min read
Service A calls service B synchronously, and B, while handling some requests, calls back to A. Both use bounded thread pools. During a traffic spike, both services stop responding until timeouts fire. Explain and propose architectural fixes.
ConceptPrincipal7 min read
As systems moved from one large JVM to many services, deadlocks shifted from lock cycles inside a process to waits across services: call cycles, exhausted pools and retries. What long-term architecture principles would you set to prevent these?

Livelock and starvation

ConceptFoundations6 min read
Define livelock and starvation with a Java example of each, and how to fix them.
ScenarioIntermediate6 min read
A service has one shared pool for request work and a periodic cleanup job. Under steady high load, the cleanup task submitted to the pool runs hours late and memory grows. Diagnose and fix.
LeadershipPrincipal7 min read
Retry storms, where clients retry failed calls and keep an overloaded service from recovering, are a form of livelock at system scale. What long-term standards would you set for retries across your organisation?

Race conditions

CodingFoundations7 min read
This Spring singleton bean is called by many request threads. Find the race and fix it.
@Service
class RateCache {
private final Map<String, Rate> cache = new HashMap<>();
Rate get(String c) {
Rate r = cache.get(c);
if (r == null) {
r = load(c);
cache.put(c, r);
}
return r;
}
}
DebuggingFoundations8 min read
Some reports show wrong dates or throw NumberFormatException intermittently. The code uses
private static final SimpleDateFormat FMT = new SimpleDateFormat("yyyy-MM-dd");
shared by all threads. Explain and fix.
ScenarioIntermediate7 min read
A payments service occasionally sends duplicate confirmation emails. The code checks
if (!order.isConfirmed()) {
sendEmail();
order.setConfirmed(true);
save(order);
}
and two instances process the same event. What would you do?
LeadershipAdvanced7 min read
A race condition in an order service caused duplicate shipments for several hours before detection, affecting other services that consumed its events. You lead the incident. Describe the response and the changes afterwards.

Thread dumps

ConceptFoundations8 min read
How do you take a thread dump of a running JVM, and what do BLOCKED, WAITING (parking) and locked lines tell you?
ConceptIntermediate7 min read
How do thread dumps work for applications using virtual threads, and what changes in how you read them?
DebuggingIntermediate7 min read
During a latency incident a thread dump shows 180 of 200 Tomcat threads like this: "http-nio-8080-exec-57" WAITING (parking) at jdk.internal.misc.Unsafe.park ... at java.util.concurrent.SynchronousQueue.poll ... at com.zaxxer.hikari.pool.HikariPool.getConnection. What is happening and what do you check next?
DesignAdvanced7 min read
Design a fleet-wide approach so that, when a latency or error alert fires, engineers have useful thread and lock information without logging into hosts.

ThreadLocal leaks

DebuggingFoundations7 min read
A service sets a large per-request object into a ThreadLocal in an interceptor, and memory slowly grows even though requests are short. A heap dump shows those objects referenced from Thread objects of the request pool. What is wrong?
DebuggingIntermediate7 min read
After several hot redeploys of a web application in a shared Tomcat, the server fails with OutOfMemoryError: Metaspace. Tomcat logs warnings that the web application created a ThreadLocal and failed to remove it. Explain the leak.

Contention profiling

ConceptIntermediate7 min read
What tools and JFR events would you use to profile contention in a JVM, and what does each tell you?
ScenarioIntermediate7 min read
A service shows p99 latency spikes while CPU usage stays at 30%. You suspect lock contention. Describe how you would confirm and locate it.
LeadershipAdvanced7 min read
Contention problems in your organisation are usually found only during incidents. What would you put in place so they are found earlier?
LeadershipPrincipal7 min read
Leadership asks whether to invest in fleet-wide continuous profiling, including lock and wall-clock profiles, or to rely on engineers taking thread dumps and profiles during incidents. How would you weigh this decision over several years?

Testing concurrent code

CodingFoundations7 min read
Write a test that tries to detect a lost-update bug in a Counter.increment() method by running many threads at once. Explain why a passing test does not prove the code is correct.
ConceptIntermediate7 min read
What tools and techniques would you use to test concurrent Java code beyond plain unit tests, and what is each good for?
LeadershipPrincipal7 min read
Testing cannot prove the absence of concurrency bugs. As a senior technical leader, how would you set an organisation-wide strategy for concurrency correctness?