Skip to content

Lesson 2 of 9 · 25m

The Cloudflare Workers Execution Model

Learn how V8 isolates, fetch handlers, bindings, limits, and subrequests shape what Workers can safely and efficiently do.

The first thing experienced server engineers get wrong about Cloudflare Workers is assuming each request has a tiny Linux VM behind it. Workers use V8 isolates: lightweight JavaScript execution contexts that can start quickly and share infrastructure efficiently. That design is why Workers are useful, and it is also why naive server code often misbehaves there.

A Worker is an event handler, not a daemon

A typical Worker exposes a fetch handler:

export default {
  async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
    const url = new URL(request.url)

    if (url.pathname === "/health") {
      return Response.json({ ok: true })
    }

    return new Response("not found", { status: 404 })
  },
}

There is no listening socket you configure. The platform invokes your code for an incoming request. You return a Response. Background continuation, when needed, is explicit through ctx.waitUntil() or queues — not an unobserved detached thread.

Bindings are the configuration surface

Workers become useful through bindings declared in wrangler.toml:

name = "checkout-edge"
main = "src/index.ts"
compatibility_date = "2025-01-01"

[[kv_namespaces]]
binding = "CONFIG"
id = "9f4d31d0a8e34455a25d8d39f7140a10"

[[r2_buckets]]
binding = "ASSETS"
bucket_name = "checkout-public-assets"

[vars]
ENVIRONMENT = "production"

A binding is not merely an environment variable. It is a capability handle injected into the execution context. Treat bindings as privileged interfaces: database connectors, KV namespaces, object storage, queues, service bindings, and secrets deserve the same least-privilege review as IAM policies.

Isolates change startup economics

Traditional cold starts often involve process creation, runtime initialization, dependency loading, and framework boot. Isolates avoid much of that cost, which makes globally distributed invocation practical. But “no cold start” is not a license to deploy a 12 MB bundle. Parse time, compile time, memory use, and module graph complexity still matter.

npx wrangler deploy --dry-run --outdir dist
ls -lh dist

If the Worker imports a huge validation library to check two strings, you have moved framework waste to thousands of locations.

Subrequests are real dependencies

Workers can call origins and APIs, but every subrequest adds latency and failure modes. Chain as little as possible and run independent work concurrently:

const [user, featureFlags] = await Promise.all([
  fetch(userUrl, { headers }).then((r) => r.json()),
  env.CONFIG.get("flags", "json"),
])

Also understand retry behavior. A retry after a partially successful mutating subrequest can duplicate side effects unless the downstream operation is idempotent.

CPU, duration, and execution constraints

Workers are designed for bounded request-time work. Exact limits depend on plan and product features, but the design rule is stable: do synchronous work measured in milliseconds, push heavy work to queues, and never assume memory persists between requests. Isolate reuse may exist operationally, but it is not a correctness guarantee.

Field notes

  • ctx.waitUntil() extends lifetime for completion work, but clients can still disconnect and observability can still be weak; log what matters before relying on it.
  • Secrets belong in Workers secrets or bindings, never in source, bundled constants, or query parameters.
  • Date.now() is not monotonic proof of ordering across PoPs. Do not use timestamps alone to resolve distributed writes.
  • Test with realistic bundles. A toy hello world hides dependency cost that production users pay.

Was this lesson helpful?

Sign in to rate