Skip to main content

Advanced: Back Pressure for High Availability

View as Markdown

When a parent may reject requests because it is overloaded, submit each request through a short SubmitRequestFlow.

The client starts SubmitRequestFlow instead of retrying an RPC in memory. SubmitStep picks a parent for the request and calls its SendRequest RPC. It completes only after the parent accepts the request. If the parent returns false, the Step fails, and the normal Step retry policy tries again after a backoff. The same retry handles a race to start the parent; see Multiple parents with partitioning. Dex keeps retrying even if the submitting process or the Worker restarts.

Parent Flows stay bounded because they reject requests they cannot take. Each submission keeps retrying on its own until a parent accepts it.

Core implementation

submit_request_flow.pyPython
class SubmitStep(Step[SubmitRequestInput]):
def __init__(
self,
client_provider: Callable[[], AsyncClient],
parent_flow: AdvancedShortLiveParentFlow,
) -> None:
self.client_provider = client_provider
self.parent_flow = parent_flow

async def execute( # type: ignore[override]
self, context: AsyncContext, input: SubmitRequestInput
) -> StepDecision:
if not input.parent_ids:
raise ValueError("at least one parent Flow ID is required")
parent_id = input.parent_ids[partition(input.request, len(input.parent_ids))]
accepted = await enqueue_request(
self.client_provider(), self.parent_flow, parent_id, input.request
)
if not accepted:
raise RuntimeError(f"parent {parent_id} rejected the request")
return graceful_complete(parent_id)


async def enqueue_request(
client: AsyncClient,
parent_flow: AdvancedShortLiveParentFlow,
parent_id: str,
request: str,
) -> bool:
try:

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

Next