skip to content

A Cloudflare Worker passed every check under wrangler dev but misbehaves in production; which platform behaviours does local mode not reproduce?

level: seniorimportance: should knowfreq 40%

answer

  1. same runtime, different platform
  2. one store, one location
  3. a cached request.cf snapshot
  4. configured limits are not enforced
  5. crons wait for a request

basics

~20 s

Local mode runs the real runtime but not the network around it: KV has no cross-location propagation delay, request.cf is one cached snapshot, configured limits such as cpu_ms are not enforced, and Cron Triggers never fire on their own.

solid answer

~40 s

Under `wrangler dev` the code runs in `workerd`, but everything around it is a single-machine simulation. Local KV is one on-disk store, so a write is visible at once and production's propagation delay of up to 60 seconds or more between locations never shows. `request.cf` is filled from a snapshot Miniflare fetches once and caches in `.wrangler/cache/cf.json` for up to 30 days, so every request has the same country, colo and bot score, or built-in Austin sample values when the fetch is disabled. Values under `limits`, such as `cpu_ms`, are enforced only once deployed. Cron Triggers do not run on schedule: you trigger the `scheduled` handler through `/cdn-cgi/local/scheduled`. Test what matters with remote bindings, a stubbed `cf` object, or a deployed preview.

code

javascript · 14 lines
javascript
export default {
  async fetch(request) {
    const start = Date.now();
    let x = 0;
    while (Date.now() - start < 300) {
      x += Math.sqrt(x + 1);
    }
    return Response.json({
      burnedMs: Date.now() - start,
      country: request.cf?.country ?? null,
      timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone,
    });
  },
};

go deeper

for a junior

Recall that wrangler dev uses the production runtime but simulates the platform, so some production behaviour never appears locally.

for a middle

Explain the concrete gaps: one KV store, a cached request.cf snapshot, unenforced limits, and Cron Triggers that need a manual request.

for a senior

Diagnose a production-only bug by mapping it to one gap and choosing the cheapest reproduction: a stub, a remote binding, --remote or a deployed preview.

for a principal

Decide which checks must run against deployed previews before release, because local green builds cannot vouch for consistency, limits or edge metadata.

## Same runtime, different platform `wrangler dev` runs Worker code in **`workerd`**, the runtime Cloudflare uses in production, so language features, Web APIs and compatibility flags behave the same. What it cannot reproduce is the **platform around the runtime**: a global network of locations, the edge that fills in request metadata, the accounting that enforces limits, and the scheduler that fires Cron Triggers. A diagnosis starts by asking which of those the failing behaviour depends on. ## Gap 1: Workers KV consistency - **Production**: a write is usually visible at once in the location that made it; other locations may serve the old value for **up to 60 seconds or more**, until their cached copy expires. - **Local**: KV is a single store under `.wrangler/state`. A write followed by a read always sees the new value, from any "user". So a feature that writes a flag in one request and reads it in another, possibly served from elsewhere, passes every local test and then serves stale values. The fix is a design decision (keep such data in a store with stronger guarantees, such as a Durable Object); local mode cannot catch it. ## Gap 2: request.cf is a fixed snapshot In production, `request.cf` describes **each request**: `country`, `colo`, `asn`, TLS details and, on plans with bot management, `botManagement.score`. Locally, Miniflare fetches one example object from `https://workers.cloudflare.com/cf.json`, caches it in `.wrangler/cache/cf.json` for up to **30 days**, and attaches it to **every** request. It reflects the developer's connection at fetch time, not each client. With `CLOUDFLARE_CF_FETCH_ENABLED=false`, or when the fetch fails, a built-in fallback is used, with `country` `"US"`, `city` `"Austin"` and `colo` `"DFW"`. Geo-routing, country blocks and bot-score branches therefore only ever take one path locally. Test them by passing the decision through a function you can call with a stubbed `cf` object, and confirm the real values on a deployed preview. ## Gap 3: limits and scale - The Wrangler configuration's **`limits`** block (`cpu_ms`, `subrequests`) is enforced only on Cloudflare's network, not in local development. A loop that burns 300 ms of CPU succeeds locally even with `"limits": { "cpu_ms": 10 }`; deployed, it fails with a CPU-time error. - There is **one local instance on one machine**. Many isolates across many locations, a Durable Object placed far from its callers, or a hot object at its throughput ceiling only appear under real traffic. - **Timing differs** both ways: no network hop to KV or D1, but laptop CPU and, for remote bindings, internet latency. ## Gap 4: triggers and metadata - **Cron Triggers do not fire locally.** Wrangler warns that "Scheduled Workers are not automatically triggered during local development". Run the handler with `curl "http://localhost:8787/cdn-cgi/local/scheduled?cron=*/5+*+*+*+*"`, or start `wrangler dev --test-scheduled` and request `/__scheduled`, or use Local Explorer's Cron Triggers view. - **Version metadata** describes no deployed version, because there is none. - **Workers AI** is the reverse case: it is always the real service, so cost and output match production even locally. - **Timezone is not a gap**: the local runtime runs with `TZ=UTC`, as production does, so `Date` and `Intl` behave the same on any laptop. ## A checklist that closes the gaps | Behaviour | Local mode shows it? | How to test it | |---|---|---| | Runtime APIs, compatibility flags | Yes | `wrangler dev` | | Data shape against real records | No (empty local stores) | `remote: true` on a staging binding | | KV propagation between locations | No | Design review; staged traffic | | Per-request `request.cf` values | No (one cached snapshot) | Stubbed `cf` in tests; deployed preview | | `limits.cpu_ms` and CPU-time errors | No | DevTools CPU profile locally; deployed preview | | Cron Trigger schedule | Only on request | `/cdn-cgi/local/scheduled`, then a deployed Cron Trigger | | Behaviour tied to Cloudflare's network | No | `wrangler dev --remote` or a preview deployment | ## How a senior answer sounds 1. It separates **runtime fidelity** (high) from **platform fidelity** (low) instead of saying "local is like production". 2. It names the concrete gap the bug falls into rather than blaming "environment differences". 3. It picks the cheapest tool that reproduces that gap: a stubbed object, a remote binding, `--remote`, or a deployed preview.

  • How would you test country-based routing that only ever sees one country locally?
    Move the decision into a function that takes the `cf` object, and unit-test it with stubbed values for each country you care about. For an end-to-end check, deploy a preview version and send requests from different regions. Locally, `CLOUDFLARE_CF_FETCH_PATH` can point at a hand-written `cf.json` (reused while under 30 days old), but it is still one object for every request.
  • Why might a Worker's scheduled handler fail in production after a local run succeeded?
    Locally you invoked it by hand, perhaps with a `cron` value that is not in `triggers.crons`, and with no `limits` enforced. In production it fires on the configured schedule, with the deployed bindings' real data and with CPU limits applied, so a slow cleanup or a branch keyed on a different cron string behaves differently.

  • Says wrangler dev reproduces production exactly because it uses workerd.
  • Expects local Workers KV to show the same propagation delay as production.
  • Trusts a geo or bot-score branch because it worked under wrangler dev.
  • Assumes limits.cpu_ms is enforced locally, so a passing loop is safe.
  • Waits for a Cron Trigger to fire on schedule in a local session.

Assumes Wrangler 4.149 and Miniflare 5: the cf.json snapshot cache, the /cdn-cgi/local/scheduled route and the rule that the limits block is enforced only on Cloudflare's network all describe current tooling.