How to capture CPU and memory profiles of a Worker or Durable Object in production, read the flamegraph, and avoid common interpretation errors.
When a Worker consumes too much CPU or approaches the memory limit, logs and aggregated metrics tell us that something is wrong, but not where. Cloudflare has launched on-demand CPU and memory profiling for Workers and Durable Objects, which generates interactive flamegraphs for code already running in production.
In this guide, we summarize how to approach a profiling session, what to expect from the results, and the limitations of the technique. We build on InfoQ's coverage and Cloudflare's documentation, so it's a good idea to cross-reference interface details with those sources.
What you need
- A Worker or Durable Object already deployed and receiving real traffic during the capture.
- Access to the Workers Observability dashboard or the cf command-line tool (refer to the installation reference).
- Source maps enabled if you're working with TypeScript, to ensure function names are readable.
- Optional: pprof or Chrome DevTools to analyze downloaded profiles offline.

Steps to capture a profile
- Identify the symptom: high latency, elevated CPU, or memory near the 128 MB limit, based on your metrics and logs.
- Choose the profile type. CPU if you suspect redundant computation; memory (heap) if you suspect excessive objects or allocations.
- Select the deployed Worker version. If it's a Durable Object, specify the particular instance by name.
- Launch the capture from the Workers Observability dashboard or the CLI while the target receives sufficient traffic.
- Open the flamegraph, locate the widest blocks, and if necessary, download the profile for analysis with pprof.
How it works internally
Profiling targets an isolate that is already running, not a new one, so you see the code under real traffic. For CPU, the runtime starts the V8 profiler with sampling every millisecond, releases the isolate lock while requests continue, and reacquires it only to stop the capture and collect results.
Since a script can have isolates in multiple data centers, the request must locate an active one of the requested version. For Durable Objects, which are stateful and can change location, the request is routed to the machine hosting that specific object.
# Illustrative outline; exact subcommands are in Cloudflare documentation
# 1. Choose the Worker and its deployed version
# 2. Request a CPU or heap profile
# 3. Download the result for analysis with pprofWe haven't invented specific commands: the CLI syntax should be copied from the official documentation. What's important is the workflow order: symptom, profile type, target, capture, and analysis.

What to expect from the results
CPU and memory answer different questions. A function can consume a lot of CPU without allocating much memory, and another can generate many allocations without dominating execution time. Therefore, it's advisable to capture both profiles before deciding where to optimize.
Cloudflare shares internal cases: a recursive JSON replacer that traversed nested data repeatedly, whose correction made it, according to the company, 2.7 times faster; a duplicated metrics call that accounted for about 1% of CPU; and partially disabled Prometheus code that reduced memory usage at the 99.9th percentile from 133 MB to 118 MB when removed. These figures are declared by the provider.
Keep the limitations in mind. On-demand capture is not continuous monitoring: a short window might miss intermittent problems. Additionally, a heap profile records allocations during capture, not a complete snapshot of retained memory. Cloudflare says it's working towards continuous profiling, but for now, you need to anticipate when to capture.
Original source: infoq.com
Article generated with AI.larebelion



Comentarios
Publicar un comentario