Cloudflare Workers & Edge Computing — DevOps

Cloudflare Workers runs your handler fetch(request, env, ctx) (the function called for each request) in the data centre closest to the visitor, in more than…

The short link notes.example.com/s/k7x2 has a trivial job: answer "go to this address". Yet every time the link is opened, the request crosses all the way to web-1, Express answers, and the answer travels back. For a visitor in Tokyo, that trip alone takes tens of milliseconds.

Cloudflare Workers moves small answers like that close to the visitor. It runs your JavaScript on Cloudflare's network, in more than 300 cities, so the code runs in the data centre closest to the visitor. Those places are called the edge, and running code there is called edge computing. Picture a postal service with a small branch office in every town and one head office. The staff at a branch can handle small things themselves: sell stamps, weigh a parcel, tell you the postage. Big matters are passed on to the head office. In this picture, each branch is a data centre at the edge, and the branch staff is a Worker (the code you run on Workers). The head office is your origin server, web-1: the server where the notes-api app itself runs. The short link is one of the small things: the Worker can answer it at the edge without asking web-1. Cloudflare has stood in front of notes-api since the Caching Strategies lesson, so requests already pass through Cloudflare, and the Worker only has to be added there.

A first Worker: fetch(request, env, ctx)

The Worker notes-edge sits on the route (an address pattern) notes.example.com/*, so every request to that domain passes through the Worker first. This is its code:

// src/index.js
export default {
  async fetch(request, env, ctx) {
    const url = new URL(request.url);

    // 1. Answered at the edge itself
    if (url.pathname === "/api/hello") {
      return Response.json({ message: "Hello from edge!", colo: request.cf?.colo });
    }

    // 2. Short link: read from KV at the edge
    if (url.pathname.startsWith("/s/")) {
      const code = url.pathname.slice(3);
      const target = await env.LINKS.get(code);
      if (target === null) {
        return new Response("Link not found", { status: 404 });
      }
      return Response.redirect(target, 302);
    }

    // 3. Any other path: passed on as it is to the origin (web-1)
    return fetch(request);
  },
};

Running near the user

A visitor in Singapore goes to data centre SIN, one in Kuala Lumpur to KUL, and one in Tokyo to NRT. The same Worker runs in all three. So the /api/hello answer holds "colo":"SIN" for the visitor in Singapore, and "colo":"NRT" for the visitor in Tokyo. These are example numbers from the Lab, for a visitor in Singapore:

Request Who answers Time (example)
/api/hello the Worker in SIN 4 ms to the edge + about 1 ms of work = 5 ms
/s/k7x2 the Worker in SIN, reading KV 4 + 1 + 1 = 6 ms
/notes/popular web-1 through fetch(request) (cache MISS) 4 + 30 to the origin + 40 of Express work = 74 ms

A request answered at the edge never travels to the origin. The farther a visitor is from web-1, the bigger the gap: from Tokyo, the trip from the edge to the origin becomes 78 ms.

V8 isolates, not containers

Each Lambda environment is a separate sandbox, like a container (AWS uses small virtual machines), with its own Node.js runtime. Preparing one makes a Lambda cold start take 100 ms to about 1 second. Workers uses V8 isolates: isolated spaces inside one process of the V8 JavaScript engine, the same engine Chrome uses. One process can hold thousands of isolates belonging to many customers. Each isolate has its own memory and can't look into the others. Creating a new isolate takes only a few milliseconds (Cloudflare says under 5 ms), so a cold start is barely noticeable.

The price you pay for that short cold start: Workers is not full Node.js. What you get is the Web APIs (the standard APIs that browsers also have: fetch, Request, Response, URL, crypto.subtle), plus some Node APIs through the nodejs_compat flag (a setting). Each isolate has 128 MB of memory.

Bindings: KV, D1 and Durable Objects

Bindings are written in wrangler.toml (the Worker's config file), and then appear in env:

So when a visitor in Singapore opens the short link https://notes.example.com/s/k7x2, the Worker in SIN answers /s/k7x2 with a 302 redirect to https://notes.example.com/notes/popular, from data in KV. The short-link request itself never reaches web-1. After that, the browser follows the redirect to /notes/popular, and that second request is the one passed on to the origin.

KV is eventually consistent: newly written data can take about 60 seconds or more before it can be read in other locations. That suits data that is read often and written rarely, like short links. For counts that must be exact, such as a rate limiter, use Durable Objects.

The CPU time limit

Workers limits CPU time, the time the processor actually works on one request. Time spent waiting on a fetch to the origin or to KV doesn't count. As of 2025, the Free plan gets 10 ms of CPU per request. Paid plans default to 30 seconds, and can be raised to 5 minutes (check the Limits page in Cloudflare's docs, because the numbers have changed before). The Worker notes-edge uses only about 1 ms, far below the limit. Heavy work such as resizing big images is better done somewhere else.

Deploying with wrangler

wrangler is the official Workers CLI (command line program). This is the config of the Worker notes-edge:

# wrangler.toml
name = "notes-edge"
main = "src/index.js"
compatibility_date = "2025-09-01"
workers_dev = false
routes = [
  { pattern = "notes.example.com/*", zone_name = "example.com" }
]

[[kv_namespaces]]
binding = "LINKS"
id = "06779da6940b431db6e566b4846d64db"

The same content can be written as wrangler.jsonc (JSON with comments). For new projects, Cloudflare now recommends wrangler.jsonc, because some newer Wrangler features are available only with a JSON config.

This is the output of npx wrangler deploy (wrangler 4.80; title lines trimmed, the version id is an example):

Total Upload: 0.63 KiB / gzip: 0.36 KiB
Your Worker has access to the following bindings:
Binding                                           Resource
env.LINKS (06779da6940b431db6e566b4846d64db)      KV Namespace

Uploaded notes-edge (2.14 sec)
Deployed notes-edge triggers (0.83 sec)
  notes.example.com/* (zone name: example.com)
Current Version ID: 5b0c7e1a-3f2d-4c8b-9a6e-1d4f7b2c9e30

⚠️ The edge is not an ordinary server

TL;DR: Cloudflare Workers runs your handler fetch(request, env, ctx) (the function called for each request) in the data centre closest to the visitor, in more than 300 cities. Those places are called the edge. That handler takes a standard web Request and returns a Response. request.cf.colo tells you which data centre answered, and return fetch(request) uses the ordinary fetch to pass other paths as they are to the origin (the app's own server, here web-1). Workers uses V8 isolates (isolated spaces inside the V8 JavaScript engine), not containers, so its cold start is under 5 ms, while Lambda's is 100 ms to 1 second. But Workers is not full Node.js. Bindings (connections to Cloudflare resources) are in env, for example KV (key-value storage), D1 (SQLite) and Durable Objects (state in one location, a fit for a rate limiter). KV is fast to read anywhere, but eventually consistent: newly written data can take about 60 seconds or more to be readable in other locations. What's limited is CPU time, the time the processor actually works (Free: 10 ms per request), and time spent waiting on fetch doesn't count. Deploy with npx wrangler deploy, which prints the bindings, the route (the address pattern the Worker sits on) and the version id.

⚡ Answered at the edge, the rest to the origin

Write:

if (url.pathname.startsWith("/s/")) {
  const code = url.pathname.slice(3);
  const target = await env.LINKS.get(code);
  if (target === null) {
    return new Response("Link not found", { status: 404 });
  }
  return Response.redirect(target, 302);
}

return fetch(request);

Meaning: This snippet is part of a Worker, a program that Cloudflare Workers runs in the data centre closest to the visitor (the edge). For a short link /s/..., the variable code (the part of the path after /s/) is looked up in KV (Cloudflare's key-value storage) through env.LINKS. If it is found, the Worker answers with a 302 redirect to target, without touching web-1. If not, the Worker answers 404. Without return fetch(request) at the end, other paths never reach the origin (the app's own server, web-1).

⚡ V8 isolates, not containers

Guess first: Lambda prepares a new sandbox, like a container, for each new environment, while Workers uses V8 isolates, isolated spaces inside one process of the V8 JavaScript engine. Which cold start is shorter, and why?

Workers. A new isolate is created inside a V8 process that is already running, without preparing a new sandbox and runtime, so it takes only a few milliseconds (under 5 ms). Lambda prepares a new sandbox: 100 ms to 1 second.

Meaning: The price you pay for the short cold start: Workers is not full Node.js. What you get is Web APIs (the standard APIs that browsers also have) such as fetch, Request and Response.

⚡ Bindings and the CPU limit

Meaning: The bindings in env connect a Worker to Cloudflare's storage. KV is fast to read anywhere, but eventually consistent: newly written data can take about 60 seconds or more to be readable in other locations. A count that must be exact uses Durable Objects (objects with state in one location), not a global variable. What's limited is CPU time, the time the processor actually works (Free: 10 ms per request), not time spent waiting on a fetch.

What you will learn