Skip to content
Get started
Get started
Cross-Queue FlowProducer Graph
View Markdown
examples · flow producer

Fan out. Transform. Converge.

Admit one atomic 5-node job graph on broker A, execute leaves on B, transform and publish on C, then inspect the completed tree on B and final result on A.

Multi-Broker DAG Flows

Run child jobs across different broker pods and unlock the parent atomically

Child 1 (Pod A) Child 2 (Pod B) Child 3 (Pod B) parallel across pods atomic counter = 0 Flow Push (Pod A) 1 atomic PG tx Parent Unlocked getChildrenValues() Zero orphaned parents even if a broker restarts between child completions
Distributed DAGs: PUSHF commits parent and child edges in one PostgreSQL transaction; whichever pod finishes the last child unlocks the parent.
  • Steps across 3 brokers: Broker A admits the 5-node graph atomically → Broker B runs extract-sales (2), extract-costs (3), and extract-forecast (5) → Broker C runs transform-ledger ((2 + 3) * 2 = 10) → Broker C publishes final report (10 + 5 = 15).
examples/postgres-multibroker/flow.ts
import { FlowProducer, Queue, Worker } from 'bunqueue/client';
import { connection, invariant, settleCleanup, uniqueQueue } from './shared';
type FlowData = { stage?: string; value?: number };
export async function runFlowExample(): Promise<void> {
const extractQueue = uniqueQueue('extract');
const transformQueue = uniqueQueue('transform');
const reportQueue = uniqueQueue('report');
const effects: string[] = [];
const executions = new Map<string, number>();
const mark = (id: string): void => {
effects.push(id);
executions.set(id, (executions.get(id) ?? 0) + 1);
};
const extractWorker = new Worker<FlowData, number>(
extractQueue,
(job) => {
mark(job.id);
return Number(job.data.value);
},
{ concurrency: 4, connection: connection('b') }
);
const transformWorker = new Worker<FlowData, number>(
transformQueue,
async (job) => {
mark(job.id);
const values = await job.getChildrenValues<number>();
return Object.values(values).reduce((sum, value) => sum + value, 0) * 2;
},
{ concurrency: 2, connection: connection('c') }
);
const reportWorker = new Worker<FlowData, number>(
reportQueue,
async (job) => {
mark(job.id);
const values = await job.getChildrenValues<number>();
return Object.values(values).reduce((sum, value) => sum + value, 0);
},
{ concurrency: 1, connection: connection('c') }
);
const flow = new FlowProducer({ connection: connection('a') });
const observer = new Queue<FlowData>(reportQueue, { connection: connection('b') });
try {
await Promise.all([
extractWorker.waitUntilReady(),
transformWorker.waitUntilReady(),
reportWorker.waitUntilReady(),
flow.waitUntilReady(),
observer.waitUntilReady(),
]);
const suffix = crypto.randomUUID();
const root = await flow.add<FlowData>({
children: [
{
children: [
{
data: { value: 2 },
name: 'extract-sales',
opts: { durable: true, jobId: `extract-sales-${suffix}` },
queueName: extractQueue,
},
{
data: { value: 3 },
name: 'extract-costs',
opts: { durable: true, jobId: `extract-costs-${suffix}` },
queueName: extractQueue,
},
],
data: { stage: 'transform' },
name: 'transform-ledger',
opts: { durable: true, jobId: `transform-${suffix}` },
queueName: transformQueue,
},
{
data: { value: 5 },
name: 'extract-forecast',
opts: { durable: true, jobId: `extract-forecast-${suffix}` },
queueName: extractQueue,
},
],
data: { stage: 'report' },
name: 'publish-report',
opts: { durable: true, jobId: `report-${suffix}` },
queueName: reportQueue,
});
invariant((await root.job.waitUntilFinished(null, 20_000)) === 15, 'wrong flow result');
invariant(executions.size === 5, 'not every flow node executed');
invariant(
[...executions.values()].every((count) => count === 1),
'a flow node executed more than once'
);
const transformNode = root.children?.[0];
invariant(transformNode, 'the transform node is missing');
const transformId = transformNode.job.id;
const reportId = root.job.id;
for (const leaf of transformNode.children ?? []) {
invariant(effects.indexOf(leaf.job.id) < effects.indexOf(transformId), 'transform ran early');
}
invariant(effects.indexOf(transformId) < effects.indexOf(reportId), 'report ran early');
invariant((await observer.getJobState(reportId)) === 'completed', 'broker B missed the report');
const tree = await flow.getFlow({ depth: 3, id: reportId, queueName: reportQueue });
invariant(tree?.children?.length === 2, 'the persisted flow tree is incomplete');
invariant((await flow.getParentResult<number>(reportId)) === 15, 'the result is not durable');
} finally {
const extract = new Queue(extractQueue, { connection: connection('a') });
const transform = new Queue(transformQueue, { connection: connection('a') });
const report = new Queue(reportQueue, { connection: connection('a') });
await settleCleanup(
[
() => extractWorker.close(true),
() => transformWorker.close(true),
() => reportWorker.close(true),
() => flow.close(),
],
[
() => extract.obliterateAsync(),
() => transform.obliterateAsync(),
() => report.obliterateAsync(),
],
[() => extract.close(), () => transform.close(), () => report.close(), () => observer.close()]
);
}
}
  • FlowProducer.add() commits all five nodes in one PostgreSQL transaction and runs each once in strict dependency order (extract-* -> transform-ledger -> publish-report).
  • job.getChildrenValues() returns authoritative child results across queues/brokers; broker B reads the depth-3 tree via getFlow() and broker A reads the durable root result 15 via getParentResult().

Keep node payloads small (store large artifacts in object storage). Unlike Queue.add(), duplicate custom IDs in FlowProducer.add() throw Flow job <id> already exists (and custom IDs cannot contain :); catch conflicts and inspect with getFlow(). For loops, branching, human approvals, or saga rollbacks, use the Workflow Engine.

Next: operate and scale the fleet.