Menu
Cloudflare Blog·October 9, 2026

On-demand CPU and Memory Profiling for Cloudflare Workers

Cloudflare introduces on-demand CPU and memory profiling with flamegraphs for Workers and Durable Objects, enabling developers to analyze resource usage directly in production. This feature is crucial for identifying performance bottlenecks and memory leaks in serverless functions, offering deep insights beyond aggregate metrics. The article highlights how this tool aids in optimizing performance and fixing memory issues in a distributed edge environment.

Read original on Cloudflare Blog

Understanding and optimizing resource utilization in distributed, serverless environments like Cloudflare Workers is critical for performance and cost efficiency. Traditional logging and metrics often fall short in pinpointing the exact cause of high CPU or memory consumption. This article introduces a powerful observability feature: on-demand CPU and memory profiling directly in production, visualized through interactive flamegraphs.

The Challenge of Profiling in Edge Environments

Profiling in a highly distributed, ephemeral environment like Cloudflare Workers presents unique challenges. Workers are replicated across numerous data centers and physical servers, dynamically routed to the nearest client, and can be instantiated multiple times on a single machine. Durable Objects add further complexity due to their stateful nature and dynamic placement. Local profiling is insufficient because production traffic patterns and loads are unique.

  • Geographic Distribution: Workers are globally distributed, meaning a profiling request needs to target a specific instance in a specific data center.
  • Ephemeral Nature: Isolates can be loaded and unloaded, requiring the profiler to find an active instance.
  • Durable Objects: These are stateful and named, requiring routing to the exact primary actor regardless of its physical location.
  • Non-Disruptive Profiling: Profiling must occur without stopping or significantly impacting the live Worker's execution.

Architectural Considerations for On-Demand Profiling

Cloudflare's implementation addresses these challenges by enabling profiling requests to specify the Worker script and version. The runtime then intelligently routes the request to an active isolate in a data center handling traffic for that version. For CPU profiling, the V8 CPU profiler is started by acquiring and releasing the isolate lock only for setup and teardown, allowing JavaScript execution to continue uninterrupted during sampling. This non-blocking approach is crucial for production stability.

text
1. Acquire isolate lock.
2. Create V8 CPU profiler, begin sampling (e.g., 1ms interval).
3. Release lock; normal requests run.
4. Wait for specified duration.
5. Reacquire lock; stop profiling.
6. Release lock; serialize results outside critical section.
💡

Optimizing Performance with Profiling

The article demonstrates practical examples, such as identifying recursive calls in a JSON replacer and duplicate metric collection in an R2 binding Worker, leading to significant CPU time savings. For memory issues, a heap profile revealed partially disabled Prometheus code consuming excessive memory, highlighting the importance of deep code analysis beyond assumptions.

Future: Continuous Profiling

While on-demand profiling is powerful, it has limitations (e.g., missing startup allocations, requiring explicit initiation). Cloudflare is working on continuous profiling, which will automatically capture samples, making it easier to detect intermittent issues and analyze historical performance trends without manual intervention.

profilingobservabilityCloudflare WorkersDurable Objectsserverlessperformance optimizationmemory managementflamegraphs

Comments

Loading comments...