436 Queue Partitioning 03bef7e4

Partition Queues for Per-Entity Limits

tools-only Updated 7 repo stars

File contents

Partition Queues for Per-Entity Limits

Partitioned queues apply flow control limits per partition key instead of the entire queue. Each partition acts as a dynamic "subqueue".

Incorrect (global concurrency for per-user limits):

// Global concurrency=1 blocks ALL users, not per-user
queue := dbos.NewWorkflowQueue(ctx, "tasks",
	dbos.WithGlobalConcurrency(1),
)

Correct (partitioned queue):

queue := dbos.NewWorkflowQueue(ctx, "tasks",
	dbos.WithPartitionQueue(),
	dbos.WithGlobalConcurrency(1),
)

func onUserTask(ctx dbos.DBOSContext, userID, task string) error {
	// Each user gets their own partition - at most 1 task per user
	// but tasks from different users can run concurrently
	_, err := dbos.RunWorkflow(ctx, processTask, task,
		dbos.WithQueue(queue.Name),
		dbos.WithQueuePartitionKey(userID),
	)
	return err
}

When a queue has WithPartitionQueue() enabled, you must provide a WithQueuePartitionKey() when enqueuing. Partition keys and deduplication IDs cannot be used together.

Reference: Partitioning Queues

tools-only/X-Skills/tree/main/automation/workflow/436-queue-partitioning_03bef7e4 commit 93792fdf20

Frequently asked questions

npx skillmds@latest add tools-only/436-queue-partitioning-03bef7e4