Skip to content

Clarify Workflow results are cached given same workflowId, across different applicationVersion #429

@nickwang0808

Description

@nickwang0808

It's not clear reading from the doc, it seems like workflowids are scoped inside appversion as idempotent keys, but in reality workflowId are globally idempotent, this is very useful knowledge because now we can also use DBOS workflow to cache expensive results

import { describe, expect, it } from "vitest";
import { PostgreSqlContainer } from "@testcontainers/postgresql";
import { DBOS } from "@dbos-inc/dbos-sdk";

async function getRandomNumbers(input: number) {
  return Math.floor(Math.random() * 1000000);
}

const workflow = DBOS.registerWorkflow(getRandomNumbers);

describe("Using workflows as cache", () => {
  it("should cache result between different version of workflows", async () => {
    expect(await getRandomNumbers(1)).to.not.equal(await getRandomNumbers(1));

    await using container = await new PostgreSqlContainer(
      "postgres:15-alpine",
    ).start();
    DBOS.setConfig({
      name: "test",
      systemDatabaseUrl: container.getConnectionUri(),
      applicationVersion: "111",
    });
    await DBOS.launch();

    const firstRun = await DBOS.startWorkflow(workflow, {
      workflowID: "1",
    })(1);
    const firstResult = await firstRun.getResult();
    await DBOS.shutdown();

    DBOS.setConfig({
      name: "test",
      systemDatabaseUrl: container.getConnectionUri(),
      applicationVersion: "222",
    });
    await DBOS.launch();

    const secondRun = await DBOS.startWorkflow(workflow, {
      workflowID: "1",
    })(1);
    const secondResult = await secondRun.getResult();
    await DBOS.shutdown();

    expect(firstResult).toEqual(secondResult);
  });
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions