Scrivio Interview library
DesignAdvanced7 min read · updated 12 November 2026

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.

I would make the evidence collect itself: run JFR continuously in every JVM with a low-overhead profile and a rolling buffer that can be dumped on demand, and have an agent or sidecar take several thread dumps a few seconds apart, plus a JFR dump, whenever an alert fires, uploading them to central storage tagged with service, version, host and alert. Where it is affordable, continuous profiling with lock and wall-clock views fills the gaps between incidents. Engineers then use a tool that groups dumps by stack and highlights blocked and waiting threads, and access is controlled because dumps can contain sensitive data.

Why this question is asked

In a fleet of hundreds of services, the classic way of debugging a hang, someone with production access logs into a host and runs jstack, fails in several ways. It is slow, it needs scarce access, the pod may have been restarted by the time anyone looks, and in a containerised platform there is often no shell at all. Interviewers ask this design question to see whether we can turn a manual expert habit into a platform capability. In this answer, we will set the requirements, then build the design in layers: continuous JFR, alert-triggered capture, continuous profiling, the analysis tooling, and the security and cost controls.

1What the Design Must Achieve

The goal is that when an alert fires at 03:00, the on-call engineer opens a link and sees what the threads and locks were doing during the minute the problem started, without production shell access. That gives us four requirements. Data must be captured at the moment of the problem, including the minutes before the alert, because the cause usually precedes the symptom. It must be captured automatically, since a restart by the orchestrator destroys the evidence. It must be stored centrally with enough tags to find it. And the overhead must be low enough to run everywhere, all the time.

Thread dumps and JFR recordings complement each other. A thread dump shows every thread's stack and locks at one instant. A JFR recording shows events over time: which locks were contended and for how long, which threads parked, which sockets were slow, GC pauses, CPU samples. Our design uses both.

Step throughFleet diagnostics, from collection to use
  1. Engineer viewlink in the alert, grouped stacks, lock and wall-clock flame graphs
  2. Analysis serviceparses dumps and JFR, groups by stack, highlights blocked and waiting
  3. Central storageobject store, tagged by service, version, host, alert id; retention limits
  4. Capture agent or sidecarruns jcmd on alert, uploads results
  5. Every JVMcontinuous JFR with a rolling buffer; optional profiler agent

5 layers, from the top down. Step through them one at a time.

2Layer 1: Continuous JFR in Every JVM

Java Flight Recorder is built into the JDK and designed to run in production. With the default settings profile, overhead is typically around one percent. We turn it on in the standard base image or JVM options so every service gets it without doing anything. The recording keeps a rolling window on local disk, limited by age and size, so the last few minutes are always available.

Text
JAVA_TOOL_OPTIONS="-XX:StartFlightRecording=name=continuous,settings=default,disk=true,maxage=15m,maxsize=250m,dumponexit=true,filename=/diag/exit.jfr"
# On demand, write out what the buffer holds (the last 15 minutes):
jcmd 1 JFR.dump name=continuous filename=/diag/alert-7731.jfr

The default profile already records events such as jdk.JavaMonitorEnter, jdk.ThreadPark, socket reads and GC pauses above thresholds. A custom settings file can lower the lock thresholds for services where contention matters. The reason for a rolling buffer instead of periodic uploads is cost: we only move and store data when something interesting happened.

3Layer 2: Capture Automatically When an Alert Fires

The alerting system already knows when latency or error rates cross a threshold. We connect it to a capture job. When an alert fires for a service, the job finds the affected instances, for example the pods behind the alert, and runs a capture on each: three to five thread dumps about five seconds apart with jcmd Thread.print -l (or Thread.dump_to_file for virtual-thread services), plus a JFR.dump of the rolling buffer. It uploads the files to an object store with tags for service, version, instance, region and alert id, and adds a link to the alert.

In Kubernetes the capture can run as an ephemeral debug container or a node agent that shares the pod's process namespace; for VMs, a small daemon does the same. The important part is that the capture runs within seconds of the alert, before an autoscaler or a liveness probe replaces the instance. A liveness-probe failure is itself a good trigger: capturing just before the kill preserves the evidence of exactly the hang that caused it.

Step throughAn alert turns into a captured dump
AlertingCapture jobJVMObject storeEngineer
p99 latency alert for order-service
jcmd Thread.print -l, three times, 5 s apart
jcmd JFR.dump of the last 15 minutes
upload, tagged service, version, alert id
attach link to the alert
open grouped view from the alert

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

4Layer 3: Continuous Profiling Where It Pays Off

Alert-triggered capture explains incidents; continuous profiling also finds problems that never become alerts, such as a lock that adds 15 ms to every request after a release. A continuous profiler samples CPU, wall-clock time and lock contention all the time, uploads aggregated profiles every minute, and lets us compare one version against the previous one. Wall-clock profiles are especially valuable for latency because they show where threads spend time waiting, not just where they burn CPU.

This costs more than JFR alone: agent overhead of a few percent, storage for profiles, and either a vendor licence or a self-hosted system such as an open-source profiling backend fed by async-profiler or JFR. So I would roll it out first to the services with the strictest latency targets and the highest traffic, and extend it if it pays off.

5Layer 4: Tooling That Makes the Data Readable

Raw dumps of 300 threads, three times over, are hard to read under pressure. The analysis service parses each capture and shows a summary first: threads grouped by identical stack with counts, groups sorted by size, BLOCKED and parked threads highlighted, and for each contended lock the owner and its stack. It compares the three dumps and marks threads that did not move. It also flags known patterns, such as many threads in HikariPool.getConnection or in a synchronized block that contains a socket read, and links the JFR file so the engineer can open lock and latency views over the minutes before the alert.

Because every capture carries the service version, the same tooling can answer 'did this start with release 4.12?' by comparing captures across versions.

6Layer 5: Access Control, Privacy and Cost

Thread dumps and JFR recordings are not harmless. Thread names often include request paths, user ids or tenant names; JFR can record system properties, environment variables and command lines, which may hold secrets; and heap-related events can expose object contents. So the store is access-controlled like production logs, with access granted per service team and logged. We configure JFR to omit sensitive events where possible, scrub obvious secrets on upload, and keep data for a limited time, for example 14 days for routine captures and longer only for captures linked to an incident review.

Cost controls matter too: captures are rate-limited per service so a flapping alert does not upload hundreds of files, and the rolling JFR buffer stays on local disk until needed.

7Follow-up questions

Why not use the Spring Boot actuator threaddump endpoint instead of jcmd?

The actuator endpoint is useful and can be called by the capture job over HTTP, which avoids needing process access. But it only works when the application's HTTP stack is still responsive, and a hung service often cannot serve the request. jcmd talks to the JVM through its attach mechanism, which works even when every request thread is stuck, so I would prefer jcmd and use the endpoint as a fallback. Either way, the endpoint must not be exposed publicly.

How do you keep JFR overhead acceptable across the fleet?

Start with the built-in default settings profile, which is designed for continuous use, and measure overhead in a load test for representative services. Raise thresholds or disable chatty events for the few services where it shows up. The profile settings with more detail are for short, targeted recordings, not for always-on use.

What would you measure to know the design works?

Time from alert to first useful evidence, the share of incidents where a capture was available, and time to diagnosis in incident reviews before and after. If engineers still log into hosts, the tooling is missing something, and that is worth investigating.

8Conclusion

In this answer, we designed fleet-wide thread and lock diagnostics in layers: continuous JFR with a rolling buffer in every JVM, alert-triggered capture of several thread dumps and a JFR dump that are uploaded with service and version tags and linked from the alert, continuous profiling with lock and wall-clock views for the services where it pays off, analysis tooling that groups stacks and highlights blocked and waiting threads, and access control, scrubbing and retention because dumps can contain sensitive data.

What to say in the interviewAbout 90 seconds aloud

Open with

I would design it so the evidence is captured automatically and is one click away from the alert. First, every JVM runs Java Flight Recorder continuously with the default low-overhead profile, keeping a rolling buffer of the last fifteen minutes on local disk; that goes into our base image so nobody has to opt in.

Then, if they want more

Second, the alerting system triggers a capture job: within seconds it runs jcmd on the affected instances to take three to five thread dumps a few seconds apart, plus a JFR dump of the buffer, and uploads them to an object store tagged with service, version, instance and alert id, adding a link to the alert.

Running it before the orchestrator restarts the pod is key, so a failing liveness probe is a trigger too. Third, for our most latency-sensitive services I would add continuous profiling with lock and wall-clock views, which also catches regressions that never page anyone, and widen it if it pays off.

Fourth, engineers need tooling that groups threads by stack, highlights blocked and parked threads with their lock owners, and compares dumps to show stuck threads.

Finally, I would treat the data like production logs: dumps can contain user ids in thread names and secrets in JFR system properties, so access is per team and audited, sensitive events are scrubbed, retention is limited, and captures are rate-limited to control cost.

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.
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 "Design a fleet-wide approach so that, when a latency or error alert fires, engineers...": https://scrivio-interview-library.netlify.app/java/concurrency-problems/2975528a7c/