Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Workers and Workers KV  ›  Lesson

The Workers Runtime — Code at the Edge

Workers Runtime Environment 12 minWorkers run your code in lightweight V8 isolates in every Cloudflare location, using web-standard APIs, with no servers to manage and no durable memory between requests.

The Workers Runtime — Code at the Edge

Throughout this course you will build Hoplink, a tiny link shortener: someone visits hop.example/cat and lands on a long URL about cats. A redirect like that should feel instant from Delhi, Lagos or Lima. If your code lives on one server in one country, half the planet waits for a round trip across an ocean. Cloudflare Workers take the opposite approach: your code runs in whichever Cloudflare data centre is nearest to the visitor.

The rule

Many tiny isolates, near every user, speaking web standards.

Unpack each part:

  • Isolates. A Worker does not get its own virtual machine or container. It runs inside a V8 isolate — the same sandbox a browser tab uses to keep one site's JavaScript away from another's. Thousands of isolates share one process, so starting one takes milliseconds, not seconds. That is why Workers have almost no "cold start".
  • Near every user. When you deploy, your code is copied to Cloudflare's whole network. A request is handled at the location that received it — the edge — rather than being forwarded to a central server.
  • Serverless. You never choose a machine, patch an operating system or decide how many copies to run. You ship a function; the platform runs as many copies as traffic needs and bills by use.
  • Web standards. Inside a Worker you use the APIs you already know from the browser: fetch, Request, Response, URL, crypto, streams. Many Node.js APIs are available too once you turn on the nodejs_compat compatibility flag, but the default world is the web platform.

The smallest Hoplink Worker looks like this:

export default {
  async fetch(request, env, ctx) {
    return new Response("Hoplink is alive");
  },
};

There is no listen(8080) and no server object. The runtime calls your fetch function once per request and sends back whatever Response you return.

The shortcut

When you meet any design question on Workers, run it through three checks: Is it short? Is it stateless? Is it standard? A Worker invocation should do a bounded amount of CPU work, keep nothing important in memory between requests, and use web APIs. Anything that fails a check belongs in a storage product (KV, R2, D1, Durable Objects) or a queue — which is exactly what the rest of this course covers.

Should Hoplink's click counter live in a global variable?Step 1 of 4
  1. Is it short?

    Adding one to a number is tiny CPU work. This check passes.

The classic trap is treating a Worker like a long-running Node server: caching user data in a module-level Map and expecting every request to see it.

Drills

Answer fast — trust the rule.

quick drill

Why do Workers start in milliseconds rather than seconds?

quick drill

A visitor in Mumbai opens a Hoplink URL. Where does the Worker usually run?

quick drill

Which API is available in a Worker without enabling any compatibility flag?

quick drill

A developer stores signed-in users in a module-level Map inside a Worker. What goes wrong?

Remember

  • Many tiny isolates, near every user, speaking web standards.
  • Shortcut: short, stateless, standard — whatever fails goes to storage or a queue.
  • Trap: treating a Worker like a long-lived server with durable global memory.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWhy not a global variable?

A teammate wants Hoplink to keep each link's click count in a module-level JavaScript object inside the Worker. Explain what Workers are made of and why this plan will give wrong numbers.