---
title: "Advanced: Back Pressure for High Availability"
description: "When a parent may reject requests because it is overloaded, submit each request through a short SubmitRequestFlow."
canonical_url: "https://docs.superdurable.io/design-patterns/subflows/back-pressure-for-high-availability/"
source: "docs/content/design-patterns/subflows/back-pressure.mdx"
locale: "en"
language: "python"
---

> Index of every page for AI agents: https://docs.superdurable.io/llms.txt

# Advanced: Back Pressure for High Availability

Code on this page is in Python. Other languages: [Go](https://docs.superdurable.io/design-patterns/subflows/back-pressure-for-high-availability.go.md) · [Java](https://docs.superdurable.io/design-patterns/subflows/back-pressure-for-high-availability.java.md) · [TypeScript](https://docs.superdurable.io/design-patterns/subflows/back-pressure-for-high-availability.typescript.md) · [Rust](https://docs.superdurable.io/design-patterns/subflows/back-pressure-for-high-availability.rust.md)

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

**Flow graph: SubmitRequestFlow**

**Steps**

1. `SubmitStep` (start Step): Pick a parent Flow by partition and queue the request on it. Next: the Flow completes.

Example: [examples/python/dex_examples/patterns/parallel-subflows/submit_request_flow.py](https://github.com/superdurable/dex/tree/main/examples/python/dex_examples/patterns/parallel-subflows/submit_request_flow.py)

## How it works

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. `SendRequest` returns false when the parent's request buffer is full. Then the Step fails, and the Step retry policy tries again after a backoff. Each retry sends a new RPC request ID, the SDK default, so the parent decides admission again. A lost response can then admit the same request twice, so have the parent deduplicate accepted requests by a stable business request ID; the example parent does not. The same retry handles a race to start the parent; see [Multiple parents with partitioning](https://docs.superdurable.io/design-patterns/subflows/multiple-parents-with-partitioning.md). 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 retries on its own until a parent accepts it or the Step retry policy runs out. Then the submit Flow fails. The retry limits are in [StepOptions defaults](https://docs.superdurable.io/reference/python/step-options.md#defaults).

## Core implementation

**Python**

```python
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:
        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


class SubmitRequestFlow(Flow[SubmitRequestInput]):
    def __init__(
        self,
        client_provider: Callable[[], AsyncClient],
        parent_flow: AdvancedShortLiveParentFlow,
    ) -> None:
        self.submit = SubmitStep(client_provider, parent_flow)

    def get_steps(self) -> StepList[SubmitRequestInput]:
        return StepList.start_step(self.submit)
```

Example: [examples/python/dex_examples/patterns/parallel-subflows/submit_request_flow.py](https://github.com/superdurable/dex/tree/main/examples/python/dex_examples/patterns/parallel-subflows/submit_request_flow.py)

## Next

- [Cancel a Case with Its Child Flows](https://docs.superdurable.io/design-patterns/subflows/cancel-case-with-its-child-flows.md)
- [Multiple parents with partitioning](https://docs.superdurable.io/design-patterns/subflows/multiple-parents-with-partitioning.md)
- [Failure Handling](https://docs.superdurable.io/design-patterns.md#failure-handling)
