Skip to content

Serverless Sandboxes

A serverless sandbox auto-stops when it goes idle and is transparently restarted on the next inbound HTTP, TCP, or TLS-SNI connection to one of its exposed ports. Long-lived sandboxes that only serve occasional traffic - preview deployments, per-user code environments, model endpoints - stop consuming CPU and memory between requests without losing their disk state or public URL.

Because of scale-to-zero, serverless sandboxes enable you to build:

  • Massive density hosting: Run thousands of isolated workspaces or container instances on a single physical node by reclaiming CPU and memory from idle environments.
  • Vercel-style deployment platforms: Spin up instant-on web applications or frontend preview deployments that automatically sleep and wake up dynamically upon client requests.
  • Neon-style serverless databases: Provision dedicated database instances (e.g. Postgres, Redis) per tenant that scale to zero when inactive and resume instantly when queries arrive.
  • On-demand interactive environments: Deploy coding interview platforms, tutorials, web playgrounds, or secure burner browsers that only consume active resources.

Check the usecase sections to learn more.

Set serverless: true on the sandbox's lifecycle alongside stopIfIdleFor. When the idle timer fires, the sandbox stops, but its exposed-port routes stay alive in a wake-aware shape and wake_armed is set in the store. The next inbound HTTP request or L4 connection through its public endpoint drives a cold start, then traffic is proxied to the started container.

AspectBehavior
Idle triggerstopIfIdleFor (required) - sandboxes stopped by lifecycle / age / involuntary exit re-arm for wake.
Manual stopCalling stop explicitly clears wake_armed. The sandbox stays down until you start it again.
Cold-start latencyHundreds of milliseconds to a few seconds, dominated by container start.
Cold-start timeout15s. After that, the ingress handler returns 503 Retry-After: 2.
Request body cap8 MiB. Larger requests during cold start are rejected with 413.
Protocols wokenHTTP, TCP, and TLS-SNI exposures. HTTP requests are buffered during cold start up to the body cap; TCP/TLS connections wait for wake and then stream bytes.

The sandbox keeps its ID, public URL, environment, mounts, and disk across stop / wake cycles. Memory and in-process state do not survive - a serverless sandbox is more like a fast scale-to-zero container than a snapshot resume.

stopIfIdleFor is required when serverless: true. The server rejects the request otherwise.

const sandbox = await client.create({
image: 'ghcr.io/me/my-http-app:latest',
lifecycle: {
stopIfIdleFor: 5 * 60 * 1_000_000_000, // 5 minutes
serverless: true,
},
})
await sandbox.exposePort(8080)

The lifecycle update endpoint accepts the same field. Toggle serverless on or off on a running sandbox; the new policy takes effect at the next idle-window evaluation.

await sandbox.updateLifecycle({
stopIfIdleFor: 5 * 60 * 1_000_000_000,
serverless: true,
})

Stopping the sandbox through the API is treated as an operator decision - wake_armed is cleared and exposed endpoints fail until the sandbox is explicitly started again. Only lifecycle-driven stops (idle timer, age, involuntary container exit) re-arm wake. Use this to take a serverless sandbox out of rotation without destroying it.