docs(self-hosting): explain how Edge Functions workers are reused across requests (#53996)

## Problem

The self-hosted Functions guide lists worker timeouts and idle cleanup,
but never says that a running worker serves later requests. The Docker
guide says the main worker loads each function from disk per request,
which suggests top-level setup runs on every request.

Related to
[FDBKIN-22732](https://linear.app/supabase/issue/FDBKIN-22732/clarify-edge-functions-worker-lifecycle-and-reuse-strategy-for-self)
Closes
[DOCS-1799](https://linear.app/supabase/issue/DOCS-1799/explain-how-self-hosted-edge-functions-reuse-workers-across-requests)

## Solution

A new "Worker lifecycle" section on the self-hosted Functions guide
explains that the default `per_worker` policy sends requests to a
function's running workers. Concurrent cold requests can start up to one
worker per CPU, and later requests rotate across them. It says that
top-level code runs once per worker, so heavy setup belongs there. It
also lists the events that stop a worker from taking new requests,
including retirement at half of `workerTimeoutMs`, links to the existing
timeout and cleanup sections, and names the settings that keep workers
alive longer. The Docker guide's sentence now says that a restart stops
running workers, so the next request picks up changed code.

## How to verify

1. Open
https://docs-git-tedd-docs-1799-supabase.vercel.app/docs/guides/self-hosting/self-hosted-functions#worker-lifecycle
and
https://docs-git-tedd-docs-1799-supabase.vercel.app/docs/guides/self-hosting/docker#accessing-edge-functions.
2. The Worker lifecycle section says that requests go to a running
worker for the function, that concurrent cold requests might each start
a worker up to the number of CPUs, and that later requests rotate across
those workers.
3. It says that top-level code runs once per worker, that heavy setup
goes at the top level, and that top-level state belongs to one worker.
4. It lists reaching half of `workerTimeoutMs`, no requests, idle-worker
cleanup, an error or resource limit, and a service restart as events
that stop a worker from taking new requests, and says a new worker
starts when the function has none left. The links to Timeout errors,
Idle-worker cleanup, Per-worker memory limit, and Container memory
budget go to those sections.
5. It names `requestAbsentTimeoutMs` and `workerTimeoutMs` as the
settings to raise to keep workers alive longer.
6. The Docker guide no longer says functions load from disk per request,
and still says a restart picks up new or changed function code.

## Checks

Ran `pnpm turbo run lint typecheck` on the changed packages and `pnpm
exec oxfmt --check` on the changed files. Checked the facts against
`docker/volumes/functions/main/index.ts` and `docker/docker-compose.yml`
in this repo, and against `supabase/edge-runtime` at `v1.79.2` (the
compose image tag): `cli/src/flags.rs` (`--policy` default),
`crates/base/src/worker/pool.rs` (worker reuse, rotation, concurrent
creation, and retirement),
`crates/base/src/worker/supervisor/strategy_per_worker.rs` (early
retirement at half of the wall-clock limit), and
`ext/workers/user_workers.js` (`forceCreate` default). Docs tests need
Docker and were skipped. Reuse wasn't tested against a running stack.

## Checklist

- [x] I have read the [Contributing
Guidelines](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [x] I have checked that the change follows the docs style guide
- [ ] I have checked that the change works in the preview deployment

## Feedback for Tedd

To ask Tedd for a change to this PR, reply in Tedd's session on
[DOCS-1799](https://linear.app/supabase/issue/DOCS-1799/explain-how-self-hosted-edge-functions-reuse-workers-across-requests).
Tedd doesn't read comments on this PR.

---------

Co-authored-by: supabase-vercel-tedd[bot] <336548405+supabase-vercel-tedd[bot]@users.noreply.github.com>
This commit is contained in:
supabase-vercel-tedd[bot] authored and GitHub committed 2026-11-24 20:02:44 -07:00
1 parent 74cb5aa97a
commit 528e2d0a98
2 files changed
+17 -1

No files matched your search

@@ -339,7 +339,7 @@ Add new functions at `volumes/functions/<FUNCTION_NAME>/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.
@@ -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.<name> 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.