Pulsar
Quickstart

Serverless

Set up Pulsar in stateless, timeout-constrained serverless environments like AWS Lambda, Vercel Functions, or Google Cloud Functions.

Quick Start: Serverless VM Runtime

Learn how to configure Pulsar for stateless Serverless VM execution environments (such as AWS Lambda, Vercel, or Google Cloud Functions) using execution timeouts and createPulsarRequestHandler.


Define Your Queues

Define your queues in a dedicated queues.ts file. In Serverless VM environments, you can configure polling profiles and consumer options matched to your function setup.

src/queues.ts
import { createQueue } from "@pulsar";

export const Queue1 = createQueue({
  queue: "q_1",
  type: "STANDARD",
  concurrency: 10,
  cycleTickLimit: 10,
  maxJobsPerTick: 1000,
  n: 1,
  sleep: 10,
  // String presets: REALTIME | STANDARD | RELAXED | ULTRA_EFFICIENT | BALANCED | CUSTOM
  profile: "REALTIME",
  // Pooler strategy: interval-driven-pooler | event-driven-pooler
  pooler: "interval-driven-pooler",
  consumer: {
    type: "http",
    url: "http://localhost:9999/test-f",
    headers: {
      Authorization: `Bearer ${process.env.SUPABASE_ANON_KEY}`,
    },
  },
});

Configure Storage & Drivers

In setup.ts, initialize the storage drivers and combine them using PulsarUnifiedStorage.

src/setup.ts
import { Queue1, Queue2, Queue3 } from "./queues.ts";
import { Pulsar, PulsarRuntime } from "@pulsar";
import { PostgresDriver, PulsarUnifiedStorage, RedisDriver } from "@pulsar/storage";
import { createPulsarRequestHandler, type GetEngine } from "@pulsar/server";

const cacheStorage = new RedisDriver({
  protocol: "tcp",
  keyPrefix: "pulsar",
  connectionString: "host.docker.internal:6379",
});

const postgresDriver = new PostgresDriver({
  connectionString: "postgresql://postgres:password@host.docker.internal:5432/postgres",
  unloggedTableSchema: "pulsar_engine",
});

const unifiedStorage = new PulsarUnifiedStorage({
  cache: cacheStorage,
  db: postgresDriver,
  strategy: "AUTO",
});

Create Engine Factory with Timeout Controls

Define your getEngine factory using PulsarRuntime.SERVERLESS_VM and explicitly pass timeoutMs to guarantee the engine stops cleanly before the serverless host terminates.

src/setup.ts
const getEngine = ({ instanceId, lockKey }: GetEngine) => {
  const engine = new Pulsar({
    runtime: PulsarRuntime.SERVERLESS_VM,
    timeoutMs: 15000, // 15-second execution boundary
    storage: unifiedStorage,
    queues: [Queue1, Queue2, Queue3],
    swarmConfig: {
      maxGlobalConcurrency: 1,
      globalMaxJobsPerTick: 100_000,
    },
    engine: {
      selfURL: "[http://host.docker.internal:9999/process-job](http://host.docker.internal:9999/process-job)",
      selfInvocationMethod: "auto_using_pgnet",
      selfInvocationIntervalSec: 5,
      healthCheckIntervalSec: 15,
      cycleTickLimit: 1000,
      tickConcurrency: "sequential",
      lockKey,
      instanceId,
    },
  });

  engine.installIntegrations([
    // Add optional plugins/event integrations here
  ]);

  return engine;
};

export const pulsarHandler = createPulsarRequestHandler({
  unifiedStorage,
  getEngine,
});

Execution Timeout Matching

Always set timeoutMs slightly below your cloud platform's function execution limit (e.g., set timeoutMs: 12000 for a 15-second AWS Lambda timeout) to allow grace periods for state flushing.

Export Function Entry Point

Expose pulsarHandler inside your serverless function handler (e.g., Next.js API routes, AWS Lambda handlers, or standard HTTP web request handlers).

src/index.ts
import { pulsarHandler } from "./setup.ts";

export async function POST(req: Request) {
  return await pulsarHandler.handle(req);
}

Next Steps

On this page