Quick revision
The short answer
We start every service with a continuous JFR recording that keeps a bounded ring buffer on disk, limited by maxage and maxsize, so the last few hours of JVM events are always available. When something goes wrong we dump that buffer with jcmd JFR.dump, either by hand, automatically when an alert fires, or when the JVM exits, and we stream a few selected events into metrics. Custom business events add context, dumps go to access-controlled storage because they contain sensitive data, and we measure the overhead under load before turning it on everywhere.
What to remember
In this answer, we designed an always-on JFR setup. Every service starts a continuous recording on disk with maxage and maxsize, so the repository works as a ring buffer of recent history. Dumps are taken with jcmd JFR.dump on demand, automatically when an alert fires, and on exit, and they go to encrypted, access-controlled storage because they contain sensitive data. Event streaming turns a few events into metrics, custom events add business context, and the overhead is measured with load tests and a canary before the setting goes fleet-wide.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We start every service with a continuous JFR recording that keeps a bounded ring buffer on disk, limited by maxage and maxsize, so the last few hours of JVM events are always available. When something goes wrong we dump that buffer with jcmd JFR.dump, either by hand, automatically when an alert fires, or when the JVM exits, and we stream a few selected events into metrics. Custom business events add context, dumps go to access-controlled storage because they contain sensitive data, and we measure the overhead under load before turning it on everywhere.
Why this question is asked
Most production incidents are over before anyone opens a laptop. The pod restarted, the latency spike passed, and the evidence went with it. Reproducing the problem later is slow and often impossible. An always-on recorder changes this: the JVM keeps writing what it is doing into a bounded buffer, and after the incident we take the last hours out and read them. Interviewers ask it to see whether we design diagnosis in advance, with its costs, storage and security, rather than improvise it during an outage. In this answer, we will recap how a continuous recording works, configure it, design how dumps are taken and stored, add streaming and custom events, deal with security, and plan a safe rollout.
1How a Continuous JFR Recording Works
JFR, the Java Flight Recorder built into the JVM, records events such as GC pauses, lock waits, socket reads, CPU samples of Java methods and allocation samples. Threads write events into small thread-local buffers, which are moved to global buffers and then written to disk in files called chunks, inside a directory called the repository.
A continuous recording has no fixed end. Instead it has limits: maxage says how old the oldest data may be, for example 6 hours, and maxsize says how much disk the recording may use, for example 1 GB. When either limit is reached, JFR deletes the oldest chunks. So the repository behaves like a ring buffer, always holding the most recent window of history, and its disk use never grows without bound.
Nothing leaves the process until we ask for it. A dump copies the chunks of the window we want into one .jfr file.
2Configuring the Recording at Startup
We add the recording to the standard JVM options of every service, ideally in a shared base image or deployment template so no team has to remember it.
The option is shown on several lines for reading; in practice it is one argument without spaces. Notice the repository location. In a container, the default temporary directory disappears with the pod, so we point the repository at a volume that survives a restart, for example an emptyDir that a sidecar can read or a node-local path. Otherwise dumponexit writes a file that is deleted a second later.
The default settings file is designed for production and the JDK documentation describes its overhead as around 1 percent; profile settings record more, such as allocation samples with stack traces, at a higher cost. We start with default plus a few chosen extras, and we confirm the numbers ourselves in the rollout step.
3Getting the Data Out: Dumps on Demand, on Alert and on Exit
There are three moments when we want a dump. The first is on demand: an engineer investigating a slow hour runs jcmd against the process. The second is automatic, when an alert fires, so the evidence is captured within seconds instead of when someone wakes up. The third is on exit, so a crash or a restart does not lose the last window.
6 messages in order. Play them, or step through them one at a time.
The engineer who picks up the alert already has the recording.
Recent JDKs accept begin and end on JFR.dump, so we can take only the window around the incident instead of all six hours; check your JDK version for these options. For the automatic case, a small agent or sidecar that receives the alert and runs jcmd keeps the logic out of the application.
4Streaming Selected Events Into Metrics
Dumps are for detailed investigation, but some JFR data is useful continuously. JFR event streaming, added in JDK 14, lets code inside the JVM subscribe to events while they are being recorded. We can turn a handful of events into metrics, for example long GC pauses, lock waits over 20 ms, or our own business events, and see them on dashboards without waiting for a dump.
Streaming reads chunks as they are flushed, so the delay is around a second, which is fine for metrics. We keep the list of streamed events short, because each one is work the service does all the time. In a real service the stream is opened once at startup and closed on shutdown.
5Adding Business Context With Custom Events
JVM events tell us that a thread waited 800 ms on a socket. They do not tell us that it was pricing a subscription order for a large customer. Custom events, classes that extend jdk.jfr.Event, fix that. We add one for each important business operation, such as pricing, payment authorisation or inventory reservation, with a few fields such as the operation type and a correlation id that also appears in our logs and traces. Then the recording shows the slow business call, and right under it the GC pause or lock wait that caused it.
We keep these events cheap: no stack traces by default, a threshold so only slow calls are kept, and field values computed only after shouldCommit() returns true.
6Treating Dumps as Sensitive Data
A JFR file is not just numbers. It can contain the JVM command line and system properties, which sometimes hold passwords, environment variables, thread names, class names and the string fields of events, including our own custom events.
So the design stores dumps in a dedicated bucket with encryption, short retention such as 30 days, and access limited to on-call engineers with logging of who downloaded what. We keep secrets out of command lines and custom event fields, and recent JDKs offer jfr scrub to remove chosen events or fields from a file before sharing it outside the team; check your version.
7Rolling It Out Safely
Before turning it on for every service, we measure. We pick two or three representative services, run a load test with and without the recording, and compare throughput, p99 latency, CPU and disk writes. Then a canary in production for a few days, and only then the base image, with a documented way to opt out.
Finally, we write a short runbook on opening a dump and check after each incident whether the recording had what we needed.
- Choose settings
default.jfcplus custom events - Load test 3 serviceswith and without recording
- Overhead under budget?for example under 2 percent CPUYesCanary in productionNoTrim events, test again
- Canary for a weekwatch p99, CPU, disk
- Add to base imageopt-out documented
- Review after incidentsdid the recording have what we needed?
6 steps in order. Play them, or step through them one at a time.
8Follow-up questions
What happens if the JVM is killed before it can dump?
dumponexit only runs on an orderly shutdown, and an in-memory recording (disk=false) is simply lost. For a SIGKILL, the repository directory still contains the chunks written before the kill, and they can be read or combined with the jfr tool afterwards, as long as the repository is on a volume that outlives the process. That is the main reason for putting the repository on its own volume.
9Conclusion
In this answer, we designed an always-on JFR setup. Every service starts a continuous recording on disk with maxage and maxsize, so the repository works as a ring buffer of recent history. Dumps are taken with jcmd JFR.dump on demand, automatically when an alert fires, and on exit, and they go to encrypted, access-controlled storage because they contain sensitive data. Event streaming turns a few events into metrics, custom events add business context, and the overhead is measured with load tests and a canary before the setting goes fleet-wide.
Open with
I would make a continuous JFR recording part of the base runtime of every service: StartFlightRecording with the default settings, disk on, maxage around six hours and maxsize around a gigabyte, with the repository on a volume that survives a container restart, and dumponexit so an orderly shutdown writes the window out.
Then, if they want more
Then the question is how data leaves the JVM. On demand, an engineer runs jcmd JFR.dump, in recent JDKs with a begin time so they only take the last half hour.
Automatically, an alert hook calls a small agent in the pod that dumps and uploads the file with the service, version and alert id, so the evidence is captured within seconds.
For things we want all the time, like long GC pauses or lock waits, I would use JFR event streaming from JDK 14 to turn a few events into metrics. I would add custom events for key business operations so slow calls carry domain context.
Dumps contain command lines, properties and string fields, so they go to encrypted storage with short retention and access logging. And before a fleet-wide rollout I would load test a few services with and without the recording, then canary it, and only then put it in the base image.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 14
- 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 an always-on JFR setup for production services so that engineers can investigate...": https://scrivio-interview-library.netlify.app/java/jvm-diagnostics/dd65d96440/