| 1 | # Workers |
| 2 | |
| 3 | A Worker is a stateless request handler that uses the Cloudflare Workers API. |
| 4 | An application uses a Worker for the code that answers an HTTP request, such as |
| 5 | an API endpoint, a router, a webhook receiver, or a front end for a binding. |
| 6 | celld runs the deployment in a V8 isolate on a node of the fleet, so a Worker |
| 7 | keeps the persistent state of an application in a binding instead. Read the |
| 8 | [Cloudflare Workers documentation](https://developers.cloudflare.com/workers/runtime-apis/) |
| 9 | for the standard API behavior. |
| 10 | |
| 11 | ## Example |
| 12 | |
| 13 | The [hello example](../../examples/hello) returns a text response from a |
| 14 | `fetch` handler. |
| 15 | |
| 16 | <!-- celld-example: hello --> |
| 17 | |
| 18 | ## The request path |
| 19 | |
| 20 | An operator puts an ingress proxy or a load balancer in front of the fleet. |
| 21 | The ingress terminates TLS, because celld serves plain HTTP on the public |
| 22 | listener of a node. The load balancer can send a request to any node in its |
| 23 | rotation, and every node embeds V8 and holds the current deployment. The node |
| 24 | that accepts the request therefore runs the handler itself, and it forwards |
| 25 | only a binding call that a different node owns. |
| 26 | |
| 27 | Cloudflare instead routes a request through its own global network and starts |
| 28 | the Worker in a nearby data center. A celld fleet has only the machines that |
| 29 | you run, so you control the placement and the routing. Read |
| 30 | [How Workers works](https://developers.cloudflare.com/workers/reference/how-workers-works/) |
| 31 | for the Cloudflare model. |
| 32 | |
| 33 |  |
| 34 | |
| 35 | ## The handler |
| 36 | |
| 37 | An HTTP request invokes the `fetch(request, env, ctx)` handler that the module |
| 38 | exports as its default export. The handler receives the inbound `Request`, and |
| 39 | it must return a `Response`. A module can export more handlers, such as |
| 40 | `scheduled` for a Cron Trigger or `queue` for a Queues consumer. Each export of |
| 41 | the main module must be a handler object or a class, as in workerd. A Worker |
| 42 | whose main module exports a string or a number therefore fails to start. Read |
| 43 | the |
| 44 | [handlers documentation](https://developers.cloudflare.com/workers/runtime-apis/handlers/) |
| 45 | for the complete list, and the |
| 46 | [fetch handler documentation](https://developers.cloudflare.com/workers/runtime-apis/handlers/fetch/) |
| 47 | for the parameters. |
| 48 | |
| 49 | The `ctx` argument controls the lifetime of the invocation. `ctx.waitUntil()` |
| 50 | keeps the isolate alive for a promise after the response goes to the client. |
| 51 | `ctx.passThroughOnException()` has no effect on celld, because celld has no CDN |
| 52 | that can answer in place of a failed Worker. |
| 53 | |
| 54 | ## State and bindings |
| 55 | |
| 56 | The `env` argument contains the bindings that the Wrangler configuration |
| 57 | declares. A binding gives the Worker a permission and an API together, so the |
| 58 | code needs no credential for the target service. Read the |
| 59 | [bindings documentation](https://developers.cloudflare.com/workers/runtime-apis/bindings/) |
| 60 | for the configuration syntax. |
| 61 | |
| 62 | A binding call leaves the isolate, and the target decides where the call goes. |
| 63 | A KV namespace, a D1 database, a queue, and a Durable Object are each a cell, |
| 64 | and one node owns a cell at a time, so celld routes the call to that owner. An |
| 65 | R2 binding has no cell, and it reads and writes the fleet bucket directly. |
| 66 | |
| 67 | celld runs many requests in one isolate, so a value in the module scope can |
| 68 | still be present for a later request. That value is not a safe place for |
| 69 | application state. The load balancer can send the next request to a different |
| 70 | node, and celld retires an isolate when the request load falls or when a new |
| 71 | deployment supersedes it, so the value can also disappear. A Worker must |
| 72 | therefore not depend on mutable in-memory state, and it must write the state |
| 73 | that it must keep into a binding. The Cloudflare |
| 74 | [best practices](https://developers.cloudflare.com/workers/best-practices/workers-best-practices/) |
| 75 | give the same rule. |
| 76 | |
| 77 | ## Differences from Cloudflare |
| 78 | |
| 79 | - celld does not manage a custom domain or terminate TLS. Terminate TLS in the |
| 80 | ingress proxy. |
| 81 | - celld does not supply a Workers AI binding. Call an AI provider from the |
| 82 | application. The process rejects `CELLD_AI_BINDING` and `CELLD_AI_URL`, and a |
| 83 | deployment rejects an `ai` declaration. |
| 84 | |
| 85 | The [Cloudflare compatibility](../cloudflare-compat.md#services) page lists the |
| 86 | runtime APIs and the unsupported services. |