# Cloudflare Workers

Cloudflare Workers discipline for edge compute and storage.

## Rules

1. **Workers are stateless.** No in-memory state between requests. Use KV, Durable Objects, R2, or D1 for persistence — pick the right storage for the access pattern.

2. **Respect CPU time limits.** Workers have execution bounds. Heavy computation, large payloads, and synchronous blocking don't belong at the edge.

3. **Bindings over hardcoded URLs.** KV namespaces, R2 buckets, D1 databases, and service bindings are configured in `wrangler.toml` — not string-interpolated endpoints.

4. **Environment-specific config in wrangler.** `wrangler.toml` environments for staging and production. Secrets via `wrangler secret put`, not in source.

5. **Durable Objects for coordination.** When you need consistent state, leader election, or WebSocket rooms — Durable Objects, not global variables.

6. **Test locally with Wrangler.** `wrangler dev` before deploy. Miniflare simulates bindings locally. Don't discover binding misconfigurations in production.

## What This Replaces

Treating Workers like long-running servers, storing state in global variables, and deploying without local testing against bindings.

## Official Source

Distilled from Cloudflare's official agent skills.
Full upstream: https://officialskills.sh/cloudflare/skills/workers-best-practices
