Monitoring Edge Function resource usage
Learn how to track your Edge Function’s performance and identify potential resource issues.
Accessing metrics
Track your function’s performance through the dashboard:
- Navigate to Edge Functions > [Your Function] > Metrics
- Review CPU, memory, and execution time charts
- Identify potential problems in resource consumption
Key metrics to monitor
CPU usage
High CPU usage indicates your function is performing intensive computations. Consider:
- Optimizing algorithms
- Caching computed results
- Moving heavy processing to background jobs
Memory usage
Memory spikes can cause function failures. Watch for:
- Large datasets loaded into memory
- Accumulated objects that aren’t garbage collected
- Large dependency bundles
Execution time
Long execution times can lead to timeouts. Monitor for:
- Slow external API calls
- Inefficient database queries
- Complex computational operations
Resource limits
Edge Functions have limited resources compared to traditional servers. Optimize for:
- Memory efficiency: Avoid loading large datasets into memory
- CPU optimization: Minimize complex computations
- Execution time: Send a response before the 150-second request idle timeout
Best practices
Use streaming for large data
Instead of loading entire files into memory, stream data:
// Stream responses instead of buffering
return new Response(readableStream, {
headers: { 'Content-Type': 'application/json' },
})
Process data in chunks
Break large operations into smaller batches:
for (const batch of dataBatches) {
await processBatch(batch)
}
Cache expensive operations
Store results of expensive computations to avoid recalculating:
const cachedResult = await cache.get(key)
if (cachedResult) {
return cachedResult
}