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);
},
};
fetch(request, env, ctx)is the Worker's handler: the function called once per request.requestis a standard webRequestobject, the same one the browser has. What you return must be aResponse.envholds the bindings: connections to Cloudflare resources such as KV, D1 or secrets.ctx.waitUntil(promise)lets small work (sending a log, for example) finish after the response has gone out.request.cfholds what Cloudflare knows about the request:colo(the data centre's code, such asSINfor Singapore),countryandcity.return fetch(request)uses the ordinaryfetch, as in the browser (not the handler), to pass the request on to the origin. Without this line, other paths such as/aboutor/notes/popularnever reachweb-1.
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:
- KV (key-value): string storage per key, copied to many locations so it is fast to read anywhere.
await env.LINKS.get("k7x2")returns a string, ornullwhen the key doesn't exist. To write:await env.LINKS.put("k7x2", url, { expirationTtl: 3600 }), and that key disappears by itself after 1 hour. - D1: Cloudflare's SQLite database, for relational data (data in tables that link to each other) with SQL queries:
await env.DB.prepare("SELECT * FROM notes WHERE id = ?").bind(42).first(). - Durable Objects: an object with its own state (data it remembers) and storage, which lives in one location. Every request for the same object is served in one place, one at a time, so its count is always consistent. A fit for a per-user rate limiter, a counter or a WebSocket chat room.
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
env.LINKS (...) KV Namespace: the bindingLINKSfrom the config is now available asenv.LINKS.notes.example.com/* (zone name: example.com): the route the Worker sits on.workers_dev = falsemeans the Worker is not published at a*.workers.devaddress (the address Cloudflare gives every Worker by default).- To try it on your laptop first,
npx wrangler devruns the same Worker locally.
⚠️ The edge is not an ordinary server
- Keeping state in a global variable. Every isolate has its own copy of the variable, and an isolate can be dropped at any time. A counter in a global variable counts only some of the requests. Use Durable Objects.
- Treating KV as instantly consistent. A freshly written link can still be
nullin other locations for about a minute. - Forgetting
return fetch(request)at the end. The routenotes.example.com/*catches every path, so without that line/notes/popularnever reachesweb-1. - Heavy CPU work in a Worker. The Free plan gives only 10 ms of CPU. Waiting on a
fetchmay take long, but computing may not.
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.