Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 36 ยท Workers

Cloudflare Workers: What They Are and Why They Matter

Learn what Cloudflare Workers are, how isolates differ from containers and servers, what the fetch handler looks like, and when serverless edge code is the right tool.

A Cloudflare Worker is a small program that runs on Cloudflare's network, in a data center close to the person making the request. You write ordinary JavaScript or TypeScript, hand it to Cloudflare, and it answers HTTP requests without any server of yours. There is no machine to patch, no region to choose and no idle capacity to pay for.

Until now in this course your traffic went through Cloudflare's proxy, which applies settings you configured. A Worker is different: it is code you wrote, and it can decide anything about a request. Redirect it, rewrite it, answer it from scratch, call an API, read a database and shape the response.

The Workers platform cannot run on this site. Everything in this chapter that talks to Cloudflare (commands, dashboards, bindings) is described precisely but shown read-only. The runnable examples are small stand-ins written in plain JavaScript that use the standard Request, Response and URL objects, which are the same objects a real Worker uses.

How Workers run: isolates, not containers

A traditional serverless function starts a container or a virtual machine for your code. That takes hundreds of milliseconds (a "cold start") and reserves memory for each copy.

Workers use V8 isolates instead. V8 is the JavaScript engine inside Chrome. An isolate is a lightweight sandbox inside one running V8 process, so thousands of isolates can share a process, and starting one takes a few milliseconds. Cloudflare typically hides even that cost by starting your isolate while the TLS handshake of the visitor is still in progress.

The trade-off is a restricted environment. You get Web-standard APIs (fetch, Request, Response, URL, crypto, streams, TextEncoder) and a large, growing slice of Node.js compatibility, but not a full operating system. There is no local disk, and CPU time and memory per request are limited.

flowchart LR
  V["Visitor"] --> E["Nearest Cloudflare data center"]
  E --> I["Your Worker (V8 isolate)"]
  I -->|"fetch()"| O["Origin or any API"]
  I -->|"binding"| S["KV, R2, D1, Durable Objects"]
  I --> V

A request is handled by your code at the edge; the origin and storage are optional.

The smallest Worker

A Worker is an ES module that exports a default object with a fetch handler. Cloudflare calls it for every request.

JavaScript
export default {
  async fetch(request, env, ctx) {
    return new Response("Hello from the edge!");
  },
};

The three parameters matter:

Parameter What it is
request A standard Request: URL, method, headers, body.
env Your bindings: secrets, variables, KV namespaces, R2 buckets, D1 databases.
ctx The execution context. ctx.waitUntil(promise) keeps work running after the response is sent.

The handler returns a Response (or a promise of one). That is the whole contract.

Try the idea

The runtime itself is not available here, but the handler contract is just a function from Request to Response. This stand-in calls a Worker-shaped object the same way Cloudflare's runtime does.

JS A Worker-shaped handler

Press Run (or Ctrl + Enter) to see the console output here.

No output.

What Workers are good for

  • Routing and rewriting. Send /api/* to one backend and everything else to another, add headers, redirect old URLs.
  • APIs and backends. Small JSON APIs with storage from KV, R2 and D1.
  • Personalization at the edge. Pick a variant, translate a header into a cookie, block by country.
  • Glue. Webhooks, signature checks, forwarding to Slack or a queue.
  • Full sites. Static assets plus server code deploy together as one Worker.

They are a weaker fit for long CPU-heavy jobs (video encoding, big data crunching) and for software that needs a specific operating system or native libraries. For those, run a container elsewhere and let a Worker sit in front of it.

Request limits in one glance

Every request runs under limits. They change over time, so always check the current "Limits" page in the Cloudflare docs before you design around a number. The categories are stable:

  • CPU time per request (time your code actually computes; waiting for fetch does not count).
  • Memory per isolate (on the order of 128 MB).
  • Subrequests: how many fetch() calls one request may make.
  • Bundle size: how big your uploaded code may be.
  • Requests per day on the free plan, with no cap other than billing on the paid plan.

If you stay inside those limits, Workers scale from zero to millions of requests without configuration.

A tiny billing and sizing estimator

Because limits and pricing change, this stand-in uses made-up numbers that you can edit. It shows the shape of the reasoning: free quota first, then a per-million price.

JS Request cost sketch (example numbers)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why do Workers start much faster than typical container-based functions?

    An isolate is a lightweight sandbox inside an existing V8 process, so starting one takes milliseconds instead of booting a container.
  2. 2. What does a Worker's fetch handler return?

    The contract is Request in, Response out. The body can be text, JSON, a stream or nothing.
  3. 3. Where do secrets, KV namespaces and D1 databases appear inside your code?

    Bindings are passed in through env, which keeps credentials out of your source and makes local testing easy.
  4. 4. Which job is the WEAKEST fit for a Worker?

    CPU time per request is limited. Heavy compute belongs on a container or batch service, with a Worker in front if needed.

Summary

  • A Worker is JavaScript/TypeScript that runs at Cloudflare's edge in a V8 isolate, with millisecond starts and no servers to manage.
  • The contract is export default { fetch(request, env, ctx) } returning a Response.
  • env carries bindings (secrets, KV, R2, D1); ctx.waitUntil runs work after the response.
  • Workers excel at routing, APIs, personalization and glue code, and are limited by CPU time, memory, subrequests and bundle size.
  • Always read the current limits page; the numbers change.

Next: Requests and Responses in Workers