How-To Guide

How to Continuously Profile Services in Production

Sample CPU and memory profiles across a fleet so you can find the code path behind a latency regression without reproducing it.

On this page

  1. Introduction
  2. What Continuous Profiling Is
  3. Sampling, Not Instrumentation
  4. Aggregating Across a Fleet
  5. Reading a Flame Graph
  6. Cost Control
  7. Acting on a Finding
  8. Conclusion

Introduction

A profile shows where a program spends its time, but traditional profiling runs in a development environment on a workload that is not the production one. Continuous profiling runs at low sampling rates in production across the whole fleet, storing profiles so you can look back at the moment a regression happened and see which function changed. It answers questions that metrics cannot: not just that latency rose, but which code path is responsible. This article covers sampling versus instrumentation, fleet-wide aggregation, reading a flame graph, and controlling the cost.

What Continuous Profiling Is

A profiler samples the call stack periodically and aggregates the samples into a picture of where time is spent. Traditional profiling instruments every function, which is accurate but slow, so it runs in development where the workload differs from production. Continuous profiling samples at a low, fixed rate in production, which is cheap enough to leave on, and aggregates across all instances so a rare slow path becomes visible.

The value is that the production workload is the only one that matters. A regression that appears under real traffic, real data, and real concurrency may not appear in a benchmark. Continuous profiling captures the profile of that exact workload, so the diagnosis starts from reality rather than from a reproduction.

Sampling, Not Instrumentation

Sampling interrupts the program at a fixed frequency and records the current stack. The overhead is proportional to the sampling rate, so a hundred samples per second is a fraction of a percent of CPU, which is why it can run in production. The trade-off is resolution: rare, very short functions may be missed, but the functions that dominate time are sampled reliably.

For CPU, the samples come from a timer; for memory allocation, from a hook on the allocator; for lock contention, from the scheduler. Each profile type answers a different question, so collect CPU and allocation profiles at least, and lock profiles where contention is suspected.

Aggregating Across a Fleet

A single instance profile describes one process. Aggregating across instances, labelled by service and version, lets you compare the profile before and after a deploy, which is how you find the code that caused a regression. The labels matter: without the version, you cannot tell which build a profile came from, and without the service, you cannot separate the consumers of a shared library.

# Continuous profiling agent (Pyroscope/Parca style) started in the service
# so profiles are labelled by service and version automatically.
pyroscope exec --server-address http://profiles.internal:4040 \
  --application-name orders-api \
  --tags version=1.4.2,region=eu-west-1 \
  -- node dist/server.js

# Query profiles for a service and a time window, optionally diffing two versions
curl -G http://profiles.internal:4040/api/v1/query \
  --data-urlencode 'query=orders-api{version="1.4.2"}' \
  --data-urlencode 'from=now-1h' --data-urlencode 'until=now' \
  -o profile.json

# Local pprof analysis when the profile is a Go/pprof format
go tool pprof -top -nodecount=20 profile.json    # hottest functions by self time

Reading a Flame Graph

A flame graph lays out the call stack vertically and the sample count horizontally, so a wide box is a function that consumed many samples and a wide stack is a path that dominates. Read it top-down to find the caller, and look for the widest boxes at the deepest level, because those are where the time actually goes rather than where it is merely called.

The most useful view is a diff between two profiles: the functions that grew are the ones that caused a regression. A diff flame graph shows the growth in one colour and the shrinkage in another, which turns “latency rose after the deploy” into “this function now consumes four times the samples”. That is a direct pointer to the change to investigate.

Cost Control

Profiles are data and storing all of them for a long time is expensive. Sample less frequently, keep raw profiles for a short window and aggregated summaries for longer, and downsample the long-term data. The goal is to have the profile when a regression happens, which means retaining enough history to cover the period between a deploy and its discovery.

Tune the sampling rate to the service: a hot service with a large fleet can afford a lower per-instance rate because the fleet provides the aggregation, while a single-instance service needs a higher rate to get enough samples. The rate is a trade-between overhead and resolution, and it should be a configured value rather than a hard-coded default.

Acting on a Finding

A profile points at a function, and the next step is to read that function and understand why it grew. Common findings are an accidental O(n squared) loop introduced by a change, an allocation in a hot path that triggers garbage collection, and a lock that serializes work that was previously parallel. Each is fixable once the profile names it, and the fix is verifiable by profiling again after the change.

Link the profile to the deploy so the timeline is complete: the regression started at a specific version, and the profile of that version versus the previous one shows the cause. This turns an investigation into a comparison, and it is the reason to label profiles by version.

# Compare the production profile of the previous release against the new one
# so the function behind a latency regression is named directly.
pyroscope compare \
  --profile-type cpu \
  "orders-api{env=\"production\",version=\"1.4.1\"}" \
  "orders-api{env=\"production\",version=\"1.4.2\"}" \
  --from "now-24h" --until "now"

# Or diff two pprof profiles from the pre-deploy and post-deploy builds
go tool pprof -diff_base before.prof after.prof
#   top output ranks functions by the growth in samples: the largest growth
#   is the code path the deploy changed.

Conclusion

Continuous profiling samples the production workload at low overhead and aggregates across the fleet, so you can look back at a regression and see the code path behind it. Run it at a low sampling rate, label profiles by service and version, compare profiles across versions to localise the change, and retain enough history to cover the deploy-to-discovery gap. Then a latency regression becomes a specific function to fix rather than a mystery to reproduce.

Sources: Google: continuous profiling, Pyroscope, Go pprof.

FAQ

Is continuous profiling safe to run in production?
At a low sampling rate, yes: the overhead is a fraction of a percent of CPU, because sampling interrupts rather than instruments every call. The trade-off is resolution for very short functions, which matters less than the functions that dominate time.
What is the most useful continuous-profiling view?
A diff between two profiles, typically the version before and after a deploy. The functions that grew are the ones behind the regression, which turns a vague latency increase into a specific function to investigate.

Related Examples