// Stack

Cloudflare Workers

Last updated ·

Cloudflare Workers is a serverless runtime that executes JavaScript, TypeScript and WebAssembly in V8 isolates, by default in the Cloudflare data center that receives each request. EOXLABS uses Workers as the request layer of the systems it builds: authentication, routing, validation and rendering run in the Worker, while state lives behind bindings to storage and coordination services.

How does EOXLABS structure a Worker?

EOXLABS writes each Worker as a stateless function of the request and its bindings. Cloudflare's explanation of how Workers works gives the reason: one isolate can serve many concurrent requests, an isolate can be evicted at any time, and nothing guarantees that two requests reach the same instance. A mutable global variable is therefore a bug, not a cache. Database clients are created inside the handler, and anything that must outlive the request goes to a binding.

Four patterns follow from that rule:

  • Bindings instead of REST calls. Storage, queues and other Workers are reached through bindings, which Cloudflare documents as faster and less restricted than its REST APIs. Secrets arrive in env, never in the bundle.
  • Small Workers joined by RPC. A system is split into a public API, an authentication Worker and an administration Worker that call each other through service bindings with RPC, by extending WorkerEntrypoint. Internal calls never travel over a public URL.
  • Pinned compatibility dates. Every Worker sets a compatibility_date and moves it forward deliberately, with tests, because runtime changes are opted into by date.
  • Awaited or queued, never fire-and-forget. ctx.waitUntil() keeps work alive for up to 30 seconds after the response and then cancels it. EOXLABS uses it for cache writes and metrics only. Audit events are awaited, because an audit trail that can be cancelled silently is not an audit trail.
export default {
  async fetch(request, env, ctx): Promise<Response> {
    // Must happen: Queues confirms the message is on disk before send() resolves.
    await env.AUDIT_QUEUE.send({ path: new URL(request.url).pathname, at: Date.now() });

    const response = await route(request, env); // the application's router

    // May happen: cancelled if still running 30 seconds after the response.
    if (request.method === "GET") ctx.waitUntil(caches.default.put(request, response.clone()));
    return response;
  },
} satisfies ExportedHandler<Env>;

The comment on send() is not a guess: the Queues JavaScript API states that when the promise resolves, the message is confirmed written to disk.

Which Workers limits shape the design?

Isolates start inside an already running process, which Cloudflare says removes the cold starts of the virtual-machine model. They still have hard limits, and EOXLABS designs to each of them on the Workers Paid plan.

LimitValueWhat EOXLABS does about it
CPU time per request30 seconds by default, configurable to 5 minutes; waiting on I/O does not countKeeps request paths I/O-bound and moves heavy computation to queues or Durable Objects
Memory128 MB per isolate, shared by every request that isolate servesStreams request and response bodies instead of buffering them
Connections waiting for headers6 per invocation; a seventh waits in a queueBatches storage calls and avoids wide fan-out inside one request
Startup timeGlobal scope must finish within 1 secondNo schema compilation or heavy setup at the top level of a module
Request body sizeSet by the zone plan: 100 MB on Free and Pro, 200 MB on BusinessSends large files to R2 by multipart upload instead of through one request body

When should a Worker run near the database instead of near the user?

By default a Worker runs in the data center closest to where the request arrived. That is the right default for authentication, caching and rendering. It is the wrong default for a request that makes five sequential queries to a database in one region, because every round trip pays the full distance.

Cloudflare's placement options move the Worker instead. Smart Placement measures request duration in candidate locations and forwards requests when another location is significantly faster. An explicit hint such as region = "aws:us-east-1" runs the Worker in the data center with the lowest latency to a single-homed database. Cloudflare puts the effect at 20 to 30 ms per query from a distant region against 1 to 3 ms when placed nearby. Smart Placement applies to fetch handlers only, not to RPC methods, so EOXLABS keeps a database-heavy path behind a fetch entry point and pairs it with Hyperdrive connection pooling.

How does a Worker compare with the alternatives?

Cloudflare WorkerDurable ObjectRegional container or VM origin
Where code runsThe data center that received the request, or a placement targetOne location, chosen when the object is createdOne region chosen in advance
State between requestsNone; state lives behind bindingsPrivate, strongly consistent storage per objectProcess memory and local disk
ConcurrencyMany isolates across the networkSingle-threaded per objectThreads and processes you manage
StartupIsolate inside a running process; 1-second global-scope limitSame runtime; the constructor runs on wakeProcess or container boot
Best fitStateless request handling at the edgeCoordination around one entityLong CPU-bound jobs, native dependencies

When a request needs one authoritative copy of some state, the answer is a Durable Object, not a global variable in a Worker.

When not to use Cloudflare Workers

  • Long CPU-bound jobs. Media transcoding or numeric batch work that needs more than 5 minutes of CPU per invocation belongs on dedicated compute.
  • Large in-process datasets. Anything that must sit in memory beyond the 128 MB isolate limit does not fit.
  • Software that needs an operating system. Native binaries, a writable local disk or OS-level dependencies stay on a container or VM origin. A Worker can still front that origin.
  • Shared mutable state. Counters, locks and sessions that several requests must agree on need a Durable Object.
  • Processing-location rules without configuration. A Worker runs wherever a request lands. Jurisdictions on D1, R2 and Durable Objects constrain where data is stored, not where code runs. Cloudflare's Regional Services is the documented control for keeping Workers execution inside a region.

Where do Workers fit in an EOXLABS engagement?

New builds start with Workers as the only public entry point, and the rest of the EOXLABS stack sits behind bindings. The design target is under 50 ms p50 response at the edge, a target rather than a measured guarantee; how EOXLABS budgets for sub-50 ms edge latency explains where the milliseconds go. For existing systems, EOXLABS often runs a Worker in front of the current origin and migrates incrementally, a pattern described in legacy-to-edge migration. During that phase ctx.passThroughOnException() can let a Worker fail open to the origin. EOXLABS allows that only for Workers that add non-security behavior; authentication Workers fail closed.

The difference between code written for this runtime and code merely hosted on it is the subject of edge-native versus edge-deployed. The wider practice is described under edge architecture, and Workers KV covers configuration read on every request.

→

Related

+

More stack

Have a system that can’t go dark?

Start a project →