Skip to main content

Advanced: Multiple Parents with Partitioning

View as Markdown

Spread requests across several parent Flow IDs, so the total number of running SubFlows can exceed one parent's concurrency limit.

Use a stable partition key, such as a request ID hash modulo the number of parents. Every request with the same key reaches the same parent. Each parent applies its own Channel buffer limit and SubFlow concurrency limit. Adding parents adds total capacity, and no single Flow grows without limit.

Changing the number or order of parents sends some keys to a different parent. If each key must keep reaching the same parent while you scale, use a versioned parent set or consistent hashing.

Core implementation

SubmitStep first invokes the RPC of the selected parent. If that parent does not exist or is no longer running, SubmitStep starts it with the request as its initial input. The parent's Init publishes that input to RequestChannel, so the request is not lost. If another submitter starts the parent first, the start fails with FlowAlreadyStarted and SubmitStep retries. Its next execution invokes the RPC on the running parent.

submit_request_flow.pyPython
async def enqueue_request(client, parent_flow, parent_id: str, request: str) -> bool:
try:
return await client.invoke_rpc(parent_flow.send_request, parent_id, request)
except FlowNotActiveOrNotFoundError:
await client.start_flow(
parent_flow,
parent_id,
ParentInput([request], DEFAULT_CONCURRENCY),
StartFlowOptions(id_reuse_policy=IdReusePolicy.ALLOW_IF_NOT_RUNNING),
)
return True


def partition(request: str, partitions: int) -> int:
hash_value = 2_166_136_261
for byte in request.encode():
hash_value ^= byte
hash_value = hash_value * 16_777_619 & 0xFFFFFFFF
return hash_value % partitions

Example: examples/python/dex_examples/patterns/parallel-subflows/submit_request_flow.py

Next