Skip to content

stack: every Edge Function request starts a new worker, so the same function never reuses one and each call pays a cold start #7274

Open

Description

@palexcast

Affected area

Edge Functions

Supabase CLI version

2.121.0

Operating system

macOS 27.0.1 (Apple silicon)

Installation method

pnpm

Command

SUPABASE_EXPERIMENTAL_STACK=1 supabase start --runtime native
for i in 1 2 3 4 5; do curl -s -w '  %{time_total}s\n' http://127.0.0.1:54321/functions/v1/hello; done

Actual output

{"requests":1,"workerStartedAt":"2026-10-22T04:47:49.507Z","xForwardedFor":null,"xRealIp":null}  1.111719s
{"requests":1,"workerStartedAt":"2026-10-22T04:47:49.553Z","xForwardedFor":null,"xRealIp":null}  0.023354s
{"requests":1,"workerStartedAt":"2026-10-22T04:47:49.586Z","xForwardedFor":null,"xRealIp":null}  0.018719s
{"requests":1,"workerStartedAt":"2026-10-22T04:47:49.613Z","xForwardedFor":null,"xRealIp":null}  0.017223s
{"requests":1,"workerStartedAt":"2026-10-22T04:47:49.645Z","xForwardedFor":null,"xRealIp":null}  0.021565s

# same project on the legacy stack (SUPABASE_EXPERIMENTAL_STACK=0 supabase start)
{"requests":1,"workerStartedAt":"2026-10-22T04:48:40.137Z","xForwardedFor":"192.168.171.1","xRealIp":"192.168.171.1"}  0.056585s
{"requests":2,"workerStartedAt":"2026-10-22T04:48:40.137Z","xForwardedFor":"192.168.171.1","xRealIp":"192.168.171.1"}  0.004061s
{"requests":3,"workerStartedAt":"2026-10-22T04:48:40.137Z","xForwardedFor":"192.168.171.1","xRealIp":"192.168.171.1"}  0.003451s
{"requests":4,"workerStartedAt":"2026-10-22T04:48:40.137Z","xForwardedFor":"192.168.171.1","xRealIp":"192.168.171.1"}  0.003629s
{"requests":5,"workerStartedAt":"2026-10-22T04:48:40.137Z","xForwardedFor":"192.168.171.1","xRealIp":"192.168.171.1"}  0.003386s

On the experimental stack requests stays at 1 and workerStartedAt changes on every call, so each request gets a brand new worker. The legacy stack keeps one worker and counts up.

Expected behavior

Requests to the same function reuse its running worker, as they do on the legacy stack. Only the first call should pay for booting the worker.

Steps to reproduce

  1. mkdir repro && cd repro && supabase init
  2. Create supabase/functions/hello/index.ts:
    let requests = 0;
    const workerStartedAt = new Date().toISOString();
    
    Deno.serve((req) =>
      Response.json({
        requests: ++requests,
        workerStartedAt,
        xForwardedFor: req.headers.get("x-forwarded-for"),
        xRealIp: req.headers.get("x-real-ip"),
      })
    );
  3. Add to supabase/config.toml:
    [functions.hello]
    verify_jwt = false
  4. SUPABASE_EXPERIMENTAL_STACK=1 supabase start --runtime native, then call the function a few times as above. requests is always 1.
  5. Stop it, start the legacy stack with SUPABASE_EXPERIMENTAL_STACK=0 supabase start (I passed -x for everything except the database, Kong and the edge runtime) and make the same calls. requests counts up.

(The two header fields are for #7275 and don't matter here.)

Crash report ID

No response

Docker and service versions

Docker: Client 29.8.2, Server 29.8.2 (linux/arm64)

supabase/postgres       17.11.0.004
supabase/gotrue         v2.197.0
postgrest/postgrest     v16.4
supabase/realtime       v2.143.4
supabase/storage-api    v1.82.1
supabase/edge-runtime   v1.77.4
supabase/studio         2026.10.12-sha-3699278
supabase/postgres-meta  v0.100.0
supabase/logflare       1.53.0
supabase/supavisor      2.9.13

Additional context

In the v2.121.0 tag (97531eb590ccd2c5cf215287ae03e877180810b4), the experimental stack's functions main passes forceCreate: true to EdgeRuntime.userWorkers.create (packages/stack/src/functions/serve.main.ts:466). The legacy main sets const forceCreate = false (apps/cli/src/shared/functions/serve.main.ts:516). develop and next still have forceCreate: true on the same line.

On a hello-world function the cost is small, about 20 ms per call instead of 4 ms. On our real project the functions import a lot more, and every call to the same function took 0.55 to 0.65 s on the native runtime, against 4 ms warm on the legacy stack. That makes the native runtime too slow for our local development and E2E runs, which is the main thing keeping us on Docker.

If forceCreate: true is there so code edits show up on the next request, reusing the worker until a file under the function changes would keep that without a cold start on every call.

I investigated this with help from an AI coding assistant (Claude Code), which also drafted this text. The results above come from real runs.

Activity

  1. self-assigned this
    on Oct 27, 2026
  2. CodelessKris commented on Nov 9, 2026

    @CodelessKris

    Same on 3.1.0 native. Cause is forceCreate: true in the stack’s main service: serve.main.ts#L479. The classic main service uses false.

    With policy = "per_worker" this also leaks: workers are kept but never reused. A trivial function, 200 requests: edge-runtime RSS 190 → 1,332 MiB. Same binary with that flag set to false: one worker reused, 139 → 161 MiB, ~3 ms per call instead of ~40 ms.

    If true is there for hot reload, maybe reuse until the source changes?

  3. CodelessKris commented on Nov 16, 2026

    @CodelessKris

    Still the case on 3.2.0: forceCreate: true, and the same on develop.

    I read in #7347 that it is kept because it is what makes edits show up without a restart. That is a fair reason, so here is what it costs next to it.

    It turns the default policy into a memory leak. supabase init writes policy = "per_worker". On the stack, that policy keeps every worker alive, and forceCreate: true means none of them ever gets a second request. So memory only grows: 190 → 1,332 MiB over 200 requests to a trivial function (my numbers above). This is the failure from supabase/edge-runtime#212 in 2023 (functions serve running out of memory because a worker was created per request), back in stack mode.

    Classic picks up edits without it. The classic main service sets forceCreate = false, and functions serve restarts the runtime when a watched file changes (File change detected: …). The stack backend does not watch files, so today the per-request worker is its only way to pick up an edit.

    Would one of these be acceptable?

    1. A way to turn it off: a config key or environment variable that makes the stack reuse workers, for CI and E2E runs where the code is frozen and hot reload is not needed.
    2. Reuse a worker until the function's source changes. fix(base): make user worker reuse code-aware edge-runtime#736 went in that direction; the stale bot closed it on 12 November without a review.

    The first looks like a small change in serve.main.ts. Without one of them the stack is much slower than classic for any project whose functions import more than a few modules (0.55–0.65 s per call in the report above, against 4 ms warm), which is what keeps us on the classic Docker stack for E2E.

  4. oktayturkdagli commented on Nov 21, 2026

    @oktayturkdagli

    Another data point from CI, on 2.121.0 native, for the toggle requested above.

    Our service E2E suite calls a ~1,800-line Edge Function (plus _shared imports) about 120 times. With forceCreate: true and noModuleCache: true every call boots a worker and recompiles: 0.85 s per request on an M-series Mac, about 1.5 s on a 2-vCPU ubuntu GitHub runner, even for an early 401. That one scenario took a median 186 s; the whole suite 249 s.

    As a stopgap we rewrite the generated main service (~/.supabase/stacks/<id>/data/<instance>/runtime/functions/generation-*/index.ts) right after supabase start to noModuleCache: false, forceCreate: false, before the lazily started runtime loads it. The scenario dropped to 3 s and the suite to 124 s on the same runner, all scenarios passing, and a deliberately broken function still failed. It works, but it patches minified generated code, so a supported switch for runs where functions are frozen (CI, E2E) would let us drop it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions