Skip to content

Lesson 2 of 9 · 24m

Workers: Isolates, Not Containers

The V8 isolate execution model explains Workers' cold-start speed, its constraints, and the mistakes people porting Node apps make.

A Worker is not a tiny Linux container and it is not "Lambda but faster" in any way that helps you design with it. Workers run inside V8 isolates: many tenants share the same process, separated by the JavaScript engine rather than by kernels and namespaces. Once that clicks, the platform's behavior becomes predictable.

The request lifecycle is brutally simple

export default {
  async fetch(request, env, ctx) {
    const url = new URL(request.url);
    if (url.pathname === "/health") {
      return new Response("ok", { status: 200 });
    }
    return fetch(request);
  },
};

request comes in, a Response goes out. env carries bindings — KV namespaces, R2 buckets, service bindings, secrets. ctx carries lifecycle controls such as ctx.waitUntil() for work that may continue after the response is sent.

There is no process you control, no local disk you can rely on, and no guarantee the same isolate handles the next request. Design for statelessness. Local memory is an optimization, never a source of truth.

Isolates explain both the strengths and the limits

Cold starts are milliseconds because the runtime is already hot — Cloudflare starts your JavaScript in an existing V8 process instead of booting a VM. CPU time is limited and measured separately from wall-clock time. You can wait on subrequests, but you cannot burn unlimited CPU in a single request.

Node compatibility exists through nodejs_compat, but it is not a blanket promise that arbitrary npm packages work. Anything expecting raw sockets, native modules, or a writable filesystem will eventually hurt you.

# wrangler.toml
name = "checkout-edge"
main = "src/index.ts"
compatibility_date = "2024-09-25"
compatibility_flags = ["nodejs_compat"]

[[kv_namespaces]]
binding = "SESSIONS"
id = "7f4b6d..."

Use ctx.waitUntil for consequence-free work

Logging, analytics, cache warming, and non-critical cleanup can finish after the response:

ctx.waitUntil(env.ANALYTICS.writeDataPoint({
  blobs: [url.pathname],
  doubles: [Date.now() - start],
}));

Do not use it for work the user expects to be true before they act on the response. "Fire and forget" is also "sometimes absent" during incidents.

Field notes

  • The biggest porting failure is assuming background threads exist. They do not; scheduled handlers, queues, and Durable Objects are the replacements.
  • Global scope may be reused between requests. Cache immutable data there deliberately; never store per-user state there unless you enjoy cross-user bugs.
  • Keep dependency graphs small. Startup work happens per isolate and you pay for it in tail latency.
  • Workers are excellent at request shaping, auth verification, routing, and glue. They are a poor place to hide a monolith.

Was this lesson helpful?

Sign in to rate