Cloudflare Workers Resource Limits & OpenNext Configuration¶
Overview¶
Cloudflare Workers don't have a "reboot" mechanism when hitting resource limits. When a Worker exceeds CPU or memory limits, Cloudflare terminates execution and returns Error 1102. Each new request starts a fresh Worker instance automatically.
Solutions¶
1. Increase CPU Limits¶
Add the limits configuration to wrangler files. Paid Workers can use up to 5 minutes (300,000ms) of CPU time.
For apps/frontend/wrangler.jsonc:
For apps/producer/wrangler.jsonc:
{
"name": "piquetickets-producer",
"main": ".open-next/worker.js",
"limits": {
"cpu_ms": 60000
},
// ... rest of config
}
Start with 60 seconds (60000ms) and adjust based on actual needs.
2. Update Compatibility Date¶
Update compatibility_date to 2025-05-05 or later for better API support:
3. Multi-Worker Setup (Advanced)¶
For better resource isolation, OpenNext supports splitting your app into separate middleware and server workers. This reduces the memory footprint of each worker.
Benefits: - Reduced memory footprint per worker - Improved cold start performance - Better handling of ISR/SSG requests
Limitations:
- Cannot be used with preview URLs
- Cannot use standard @opennextjs/cloudflare deploy command
- Requires manual deployment with Wrangler CLI
See: https://opennext.js.org/cloudflare/howtos/multi-worker
4. Analyze and Optimize Bundle Size¶
cd apps/frontend && npx opennextjs-cloudflare build
# Then analyze .open-next/server-functions/default/handler.mjs.meta.json
Use the ESBuild Bundle Analyzer to identify large dependencies.
Size Limits: - Free Plan: 3 MiB compressed - Paid Plan: 10 MiB compressed
5. Monitoring¶
Use wrangler tail to debug in real-time:
Sentry is already configured for error capture.
Common Causes of Error 1102¶
- Heavy computation or long-running tasks
- Tight loops or unbounded recursive calls
- Hanging Promises that stall execution
- Large bundle sizes causing slow cold starts
Best Practices¶
- Break down lengthy tasks into smaller, asynchronous chunks
- Avoid blocking patterns
- Stream data processing where possible
- Ensure all Promises resolve or reject appropriately
- Move heavy computation to backend services or scheduled jobs