diff --git a/apps/docs/content/guides/self-hosting/docker.mdx b/apps/docs/content/guides/self-hosting/docker.mdx index 16a38eb5d44..60844a1faf4 100644 --- a/apps/docs/content/guides/self-hosting/docker.mdx +++ b/apps/docs/content/guides/self-hosting/docker.mdx @@ -339,7 +339,7 @@ Add new functions at `volumes/functions//index.ts`, then restart sh run.sh restart functions ``` -The main worker loads each function from disk per request, so a restart is enough to pick up new or changed function code. Use `sh run.sh recreate functions` instead when you change environment variables or secrets (for example `.env.functions` or the service's `environment:` block), since those are only applied when the container is recreated. +A restart stops the running function workers, so the next request to each function starts a worker from the new or changed code. Use `sh run.sh recreate functions` instead when you change environment variables or secrets (for example `.env.functions` or the service's `environment:` block), since those are only applied when the container is recreated. See the [Self-hosted Edge Functions](/docs/guides/self-hosting/self-hosted-functions) guide for more details. diff --git a/apps/docs/content/guides/self-hosting/self-hosted-functions.mdx b/apps/docs/content/guides/self-hosting/self-hosted-functions.mdx index 958587c54b1..a47881f3ecf 100644 --- a/apps/docs/content/guides/self-hosting/self-hosted-functions.mdx +++ b/apps/docs/content/guides/self-hosting/self-hosted-functions.mdx @@ -212,6 +212,22 @@ For more details, see: In your functions, the [supported synchronous Deno file APIs](/docs/guides/functions/ephemeral-storage#using-synchronous-file-apis), such as `Deno.readFileSync`, work only during initial script evaluation. A call inside a request handler or a timer such as `setTimeout` throws a `PermissionDenied` error: `Deno. is blocklisted on the current context`. The functions service runs each of your functions in its own user worker, where this rule applies. The main worker, which runs the router in `volumes/functions/main/index.ts`, isn't restricted. +## Worker lifecycle + +Each function runs in a worker that serves many requests. The `functions` service uses Edge Runtime's default `per_worker` policy, which sends a request to a running worker for its function. A new worker starts only when the function has no running worker. + +After a function has a running worker, later and concurrent requests go to it. Concurrent requests that arrive before any worker is running might each start a worker, up to the number of CPUs available to the container. Later requests rotate across those workers. Code at the top level of the function's `index.ts` runs once per worker, not once per request. Put heavy setup, such as creating clients, loading schemas, or filling caches, at the top level, outside the request handler. State created at the top level belongs to one worker and lasts only as long as that worker. + +Each of these events stops a worker from taking new requests. When the function has no running worker left, the next request starts a new one, with a cold start and a new run of top-level code: + +- The worker reaches half of its maximum lifetime, set by `workerTimeoutMs`, and stops taking new requests. See [Timeout errors](#timeout-errors). +- The worker gets no requests for the time set by `requestAbsentTimeoutMs` and has no pending tasks. See [Timeout errors](#timeout-errors). +- [Idle-worker cleanup](#idle-worker-cleanup) retires the worker under memory pressure. +- The worker exits on an error or a resource limit, such as the [per-worker memory limit](#per-worker-memory-limit). +- The functions service restarts or is recreated. + +To keep workers alive longer, raise `requestAbsentTimeoutMs` and `workerTimeoutMs` in `volumes/functions/main/index.ts`, then restart the functions service. Idle workers then hold memory from the [container memory budget](#container-memory-budget) for longer. + ## Memory management The functions service has two memory limits: a shared container budget and a per-worker limit. Idle-worker cleanup responds to usage of the container budget.