title: Use Queues for Concurrent Workflows impact: HIGH impactDescription: Queues provide managed concurrency and flow control tags: queue, concurrency, enqueue, registerQueue, flow-control
Use Queues for Concurrent Workflows
Queues run many workflows with managed flow control. Register a queue with dbos.registerQueue(name, options)
after launch — its configuration is stored in the system database and visible to every process on that database —
then enqueue workflows with StartWorkflowOptions.withQueue(...).
Incorrect (unbounded concurrent starts):
// Starts every task immediately: no flow control, no protection for downstream services
for (String task : tasks) {
dbos.startWorkflow(() -> proxy.processTask(task));
}
Incorrect (deprecated in-memory Queue registered before launch):
// Deprecated for removal: in-memory only, not visible to other processes or clients
dbos.registerQueue(new Queue("task-queue").withWorkerConcurrency(5));
dbos.launch();
Correct (database-backed queue, enqueue with flow control):
import dev.dbos.transact.StartWorkflowOptions;
import dev.dbos.transact.workflow.QueueOptions;
dbos.launch();
dbos.registerQueue("task-queue", new QueueOptions().withWorkerConcurrency(5));
@Workflow
public List<String> processAll(List<String> tasks) throws Exception {
var options = new StartWorkflowOptions().withQueue("task-queue");
List<WorkflowHandle<String, Exception>> handles = new ArrayList<>();
for (String task : tasks) {
handles.add(dbos.startWorkflow(() -> self.processTask(task), options));
}
List<String> results = new ArrayList<>();
for (var handle : handles) {
results.add(handle.getResult());
}
return results;
}
Key points:
- Enqueued workflows are dequeued in priority order, then FIFO, by any process listening to the queue. Since 1.2, creation, dequeue and rate-limit times come from the database clock, so clock skew between hosts does not affect FIFO order or rate limits
- Enqueueing is durable: once
startWorkflowreturns, the workflow will run even if this process dies - Build
QueueOptionslike every other options type: start fromnew QueueOptions()(every property unset) and chainwithConcurrency,withWorkerConcurrency,withRateLimit,withPartitionConcurrency,withPartitionWorkerConcurrency,withPartitionRateLimit,withPollingInterval. Since 1.2,QueueOptions.empty(), the staticsetXfactories, theandXmethods and thewithXoverloads taking aFieldorOptionalare deprecated for removal — do not generate them.setPriorityEnabledis deprecated and ignored: every queue dequeues in priority order - Enqueue from outside the application with
DBOSClient(client-enqueue.md) - Existing code using the in-memory
Queuestill works, but per-partition limits are supported only on database-backed queues (queue-partitioning.md) registerQueue(name, options, onConflict)controls how registration treats a queue already in the system database:UPDATE_IF_LATEST_VERSION(default),ALWAYS_UPDATEorNEVER_UPDATE(use this if you reconfigured the queue at runtime withupdateQueue) (queue-management.md)- Each queue is owned by the application that registers it, and only that application dequeues workflows from it — this matters when multiple applications share a system database (advanced-shared-database.md)
Incorrect (a bare string is a workflow ID, not a queue):
// The single-String constructor sets the WORKFLOW ID: this starts the workflow directly,
// with ID "task-queue", on no queue at all
var options = new StartWorkflowOptions("task-queue");
Correct (name the queue explicitly):
import dev.dbos.transact.workflow.QueueName;
var options = new StartWorkflowOptions(QueueName.of("task-queue"));
var same = new StartWorkflowOptions().withQueue("task-queue");
QueueName wraps a queue name so it cannot be mistaken for a workflow ID. StartWorkflowOptions, ForkOptions,
DBOSConfig.withListenQueue(s) and Debouncer/DebouncerClient.withQueue accept either a String or a
QueueName.
Deprecated for removal since 1.1 — do not use in new code:
dbos.getQueue(name)(reads only the pre-launch registry); usedbos.findQueue(name)dbos.registerQueue(Queue)/dbos.registerQueues(Queue...); usedbos.registerQueue(String, QueueOptions)after launch- The
Queueconstructors and itswith*builders; aQueueis whatfindQueue/listQueuesreturn, not something to build - Every overload that takes a
Queuevalue:new StartWorkflowOptions(Queue),StartWorkflowOptions.withQueue(Queue),ForkOptions.withQueue(Queue),DBOSConfig.withListenQueue(Queue)/withListenQueues(Queue...),Debouncer.withQueue(Queue)/DebouncerClient.withQueue(Queue); pass the name or aQueueNameinstead
To receive results as each task finishes instead of waiting in order, have each child workflow send a message to
the parent and recv them as they arrive (comm-messages.md).
Reference: DBOS Queues