Video in progress
You want to keep JFR on permanently in prod and build continuous profiling for the whole fleet. How do you design it?
On every JVM a low-overhead JFR recording runs in circular mode, and an agent takes it periodically or on an event and sends it to central storage. The main questions are overhead, storage size, personal data and how easy it is to search.
📖 Answer
JFR's default profile adds about 1% overhead, so you can leave it on in prod all the time. The profile settings are more detailed, but more expensive.
On every JVM:
-XX:StartFlightRecording=name=bg,settings=default,disk=true,maxage=2h,maxsize=500m
This works like a "black box": the last 2 hours rotate on disk. When there is a problem, you grab it with jcmd <pid> JFR.dump name=bg filename=incident.jfr.
At the fleet level there are two approaches:
- Periodic collection. A sidecar or agent takes a chunk every 1-5 minutes, picks out the events you need (CPU samples, allocation, locks, GC) and sends them to central storage. Systems like Grafana Pyroscope accept the JFR format.
- JFR Event Streaming (JDK 14+). Read events in real time inside the application with
RecordingStreamand turn them into metrics, for example a long GC pause or a lock wait longer than 50 ms.
try (var rs = new RecordingStream()) {
rs.enable("jdk.JavaMonitorEnter").withThreshold(Duration.ofMillis(50));
rs.onEvent("jdk.JavaMonitorEnter", e -> lockWaits.increment());
rs.startAsync();
}
What has to be settled up front: the cost of storage (GBs per day from every service), privacy (JFR can contain system properties, environment variables and arguments, so filter them), and linking to the deploy version: it has to answer "which commit is this flame graph from". The biggest benefit is that after an incident there is data to answer "what was going on at that moment".
Sign up to mark studied questions and save your progress.