Skip to main content
Prisma Documentation Docs

Search documentation

Type to search this documentation.

On this pageOverview

Keeping instances awake (/docs/compute/keeping-instances-awake)

For the complete Prisma documentation index, see llms.txt. A markdown version of any docs page is available by appending .md to its URL.

Keep a Prisma Compute instance awake so it does not scale to zero while bounded background work completes.

Location: Compute > Keeping instances awake

By default, Prisma Compute scales idle instances to zero to save cost: idle instances sleep with a memory snapshot, then resume with memory intact in milliseconds. Some bounded background work needs the instance to keep running after a request ends or while no request is in flight.

@prisma/compute provides two primitives for these cases:

  • waitUntil: keeps the current instance awake until a background promise settles.
  • KeepAwakeGuard: keeps the current instance awake until you release the guard.

[!WARNING] These primitives are for best-effort background work. They only prevent the current instance from scaling to zero; they do not make work durable, retry failures, guarantee that work continues through restarts or deployments, or change connection-lifetime limits. Persist progress and design work so it can safely restart.

WebSocket servers are not currently supported.

Reach for waitUntil or KeepAwakeGuard for bounded, best-effort work between requests, for example:

  • A bounded job that outlives the request that started it.
  • Background processing you kick off after returning a response.

This also covers work that would exceed the 60-second request timeout: return the response first, then let the work continue under waitUntil.

If your work always finishes inside a single request, you do not need either primitive. Let the instance sleep as usual.

Use waitUntil when you have a promise for background work that should keep the instance awake. This is the most direct API for work you start during a request but do not await before returning a response:

src/index.ts
import { waitUntil } from "@prisma/compute";

export default {
  fetch(req: Request) {
    waitUntil(processInBackground(req), {
      // Optional. Prevents a hanging promise from keeping the instance awake indefinitely.
      signal: AbortSignal.timeout(30_000),
    });

    return new Response("Accepted");
  },
};

waitUntil follows the same pattern as Cloudflare Workers and the Service Worker API: the instance stays awake until the promise settles. Prisma Compute adds an optional caller-owned signal value so you can stop the keep-awake effect if a promise hangs or never settles. The signal does not cancel the promise or the work it started.

Use KeepAwakeGuard for block-scoped or manual lifetimes

Section titled “Use KeepAwakeGuard for block-scoped or manual lifetimes”

Use KeepAwakeGuard when the keep-awake lifetime does not map cleanly to one promise, or when a block scope should control when the instance can resume normal scaling.

KeepAwakeGuard implements the JavaScript Disposable interface, so you can use the using keyword to release it automatically at the end of the scope:

src/index.ts
import { KeepAwakeGuard } from "@prisma/compute";

async function processBatch() {
  using guard = new KeepAwakeGuard();

  await fetchAllRecords();
  await transformData();
  await writeResults();
}

While the guard is active, the instance does not sleep. When the scope exits, using disposes the guard and normal scaling resumes.

If using is not available in your runtime or build setup, or if you need more granular control, release the guard in a finally block. This prevents a thrown error from leaving the guard active longer than intended:

src/index.ts
import { KeepAwakeGuard } from "@prisma/compute";

async function processBatch() {
  const guard = new KeepAwakeGuard();
  try {
    await fetchAllRecords();
    await transformData();
    await writeResults();
  } finally {
    guard.release();
  }
}

Calling guard.release() signals that the work is done and the instance can resume normal scaling behavior. Always release manually created guards, preferably from finally, unless using owns the lifetime for you.

KeepAwakeGuard accepts a caller-owned signal option. Use it as a safety fallback to release the guard if your code does not reach release(). The signal releases the keep-awake guard; it does not cancel the work protected by the guard.

src/index.ts
import { KeepAwakeGuard } from "@prisma/compute";

const guard = new KeepAwakeGuard({
  // Optional. Prevents a leaked guard from keeping the instance awake indefinitely.
  signal: AbortSignal.timeout(30_000),
});

[!WARNING] The signal option is a fallback, not the primary way to control your work. Prefer using or try/finally for normal guard lifetimes. Use signal to cap the maximum keep-awake lifetime and avoid dangling guards.

For example, combine AbortSignal.timeout() with try/finally when a job should keep the instance awake, but never for longer than a fixed duration:

src/index.ts
import { KeepAwakeGuard } from "@prisma/compute";

async function processBatch() {
  const guard = new KeepAwakeGuard({
    // Optional. Prevents a leaked guard from keeping the instance awake indefinitely.
    signal: AbortSignal.timeout(30_000),
  });

  try {
    await fetchAllRecords();
    await transformData();
    await writeResults();
  } finally {
    guard.release();
  }
}

The signal follows the standard AbortSignal pattern, so you can pass any signal you control.

@prisma/compute
waitUntil(promise: PromiseLike<unknown>, options?: { signal?: AbortSignal }): void;

Keeps the instance awake until promise settles. Pass options.signal as a caller-owned fallback to stop keeping the instance awake if the promise hangs or never settles. The signal does not cancel the promise.

@prisma/compute
new KeepAwakeGuard(options?: { signal?: AbortSignal });

Creates a guard that keeps the instance awake until the guard is disposed, released, or its optional caller-owned signal aborts. The signal releases the guard; it does not cancel the work protected by the guard.

Member Type Description
release() () => void Releases the guard, allowing the instance to resume normal scaling behavior.
Disposable Symbol.dispose Enables automatic release with using.
  • Alchemy: Provision Prisma Postgres and deploy applications to Prisma Compute in one TypeScript stack.
  • Branching: Branches are isolated environments that map to your Git branches, so preview work never touches production.
  • Deploy Button: Add a Deploy with Prisma button that copies a public Composer repository and starts a Composer-managed deployment.
  • Deploy on push: Graduate a Composer app from manual deploys to a Git workflow, with production deploys on push and an isolated preview environment per branch.
  • Deployments: How deploys create service versions on Prisma Compute, and how to inspect, promote, roll back, start, and stop them.
Suggest an edit

Propose a replacement for this page. The site team reviews it before applying any changes.

Export
Documentation menu