> ## Documentation Index
> Fetch the complete documentation index at: https://docs.calado.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Serverless patterns

> Flush the calado queue before your function freezes.

## Overview

calado batches in memory and flushes every 5 seconds on a timer. Serverless runtimes freeze between invocations, so that timer often never fires and events get stranded in a dead queue.

The fix is to call `calado.flush()` before your handler returns. How you do that depends on the runtime.

## Next.js App Router

Use Next's `after()` helper. It runs after the response is sent, so the user doesn't wait on the flush.

```ts theme={null}
import { after } from "next/server";
import { calado } from "calado";

export async function POST(req: Request) {
  // ...handle the request, make LLM calls...

  after(() => calado.flush());

  return Response.json({ ok: true });
}
```

## Vercel Edge / Next.js Edge runtime

Use `ctx.waitUntil` so the platform keeps the worker alive until the flush resolves.

```ts theme={null}
import { calado } from "calado";

export default async function handler(
  req: Request,
  ctx: { waitUntil: (p: Promise<unknown>) => void }
) {
  // ...handle the request...

  ctx.waitUntil(calado.flush());

  return new Response("ok");
}
```

## AWS Lambda

Flush in a `finally` block so it runs on both success and error paths.

```ts theme={null}
import { calado } from "calado";

export async function handler(event: unknown) {
  try {
    // ...handle the request...
    return { statusCode: 200 };
  } finally {
    await calado.flush();
  }
}
```

## Cloudflare Workers

Same pattern as Vercel Edge. Workers provide `ctx.waitUntil` on the request handler's second argument.

```ts theme={null}
export default {
  async fetch(req: Request, env: Env, ctx: ExecutionContext) {
    // ...handle the request...

    ctx.waitUntil(calado.flush());

    return new Response("ok");
  },
};
```

## Long-running Node processes

If your server stays warm between requests (Express, Fastify, a worker process), the default 5-second flush timer handles everything. You don't need to call `flush()` manually.

On graceful shutdown, call `await calado.shutdown()` to flush and clear timers.

```ts theme={null}
process.on("SIGTERM", async () => {
  await calado.shutdown();
  process.exit(0);
});
```

## Tuning batch size and flush interval

If you're sending many events and want lower latency, lower the batch size or flush interval.

```ts theme={null}
calado.init(process.env.CALADO_API_KEY!, {
  batchSize: 5,
  flushInterval: 1_000,
});
```

Set `flushInterval: 0` to disable the timer entirely. Use this in serverless setups where you always flush manually.

See [Node.js SDK → Init options](/ingestion/node-sdk#init-options) for the full list.
