Cloudflare Workers

Cloudflare Workers run JavaScript/TypeScript at the edge — on Cloudflare's global network, physically close to users. That makes them ideal for request routing, caching, lightweight APIs, auth gateways, and content transformation, all with very low latency. The mental model: an edge gateway that shapes requests before they ever reach your origin.

Workers trade a restricted runtime and tight CPU/time limits for that proximity and scale. They're not the place for heavy compute or long-running background jobs — they're where you put fast, request-scoped logic in front of everything else.

TL;DR

Quick Example

A Worker is just a fetch handler — intercept, decide, respond or forward:

What Workers Are

They're commonly used as an edge layer in front of an origin API to normalize requests, enforce auth, add caching, and cut origin latency and load.

Common Use Cases

See API Security.

Data Options

Cloudflare offers several storage primitives — pick by consistency and latency needs:

💡 Quick guide: read-heavy global config → KV; per-entity coordination needing strong consistency → Durable Objects; object-storage semantics → R2.

Caching

Edge caching dramatically cuts latency and origin load. The rules of thumb:

See Caching.

Security Considerations

Operational Gotchas

Best Practices

Common Mistakes

Treating KV as strongly consistent

Doing heavy compute in a Worker

FAQ

How are Workers different from AWS Lambda?

Lambda runs in a full runtime in a specific region with cold starts and broad capabilities. Workers run a lightweight V8-isolate runtime at hundreds of edge locations with near-zero cold start, but with stricter CPU/time limits and a smaller API surface. Use Workers for fast, global, request-scoped logic; use Lambda for heavier, region-bound workloads. See Serverless Patterns.

When should I use KV vs Durable Objects vs D1?

KV for read-heavy, globally-replicated config where eventual consistency is fine. Durable Objects when you need a single authoritative, strongly-consistent point of coordination per key (counters, chat rooms, locks). D1 when you need relational SQL queries. R2 for object storage. Match the primitive to your consistency and query shape.

Can Workers replace my backend?

For some apps, yes — a Worker plus D1/KV/R2 can serve a complete lightweight API. But for heavy compute, long-running jobs, persistent connections, or complex relational workloads, Workers are better as an edge layer in front of a backend than a full replacement.

How do I handle secrets in a Worker?

Use Cloudflare's encrypted secrets/bindings (via wrangler secret put or the dashboard), accessed through the env object — never hardcode keys in the Worker source or log them. See Secrets Management.

Related Topics

References