|
| 1 | +--- |
| 2 | +title: "Autoscaling in Azure Container Apps pattern for the Durable Task SDKs" |
| 3 | +description: Learn how to implement autoscaling with the Durable Task Scheduler using the Durable Task .NET SDK in Azure Container Apps. |
| 4 | +ms.subservice: durable-task-scheduler |
| 5 | +ms.topic: how-to |
| 6 | +ms.date: 05/01/2025 |
| 7 | +--- |
| 8 | + |
| 9 | +# Autoscaling in Azure Container Apps pattern for the Durable Task SDKs |
| 10 | + |
| 11 | +You can implement autoscaling in container apps that use the Durable Task Scheduler. Autoscaling maintains the reliability and scalability of long-running workflows by adapting to changing demands without manual intervention. |
| 12 | + |
| 13 | +Control autoscaling by setting the range of application replicas deployed in response to an orchestration, activity, or entity being triggered. The scaler dynamically adjusts the number of container app replicas within that range, allowing your solution to handle spikes in the workload and prevent resource exhaustion. |
| 14 | + |
| 15 | +## Configure the autoscaler |
| 16 | + |
| 17 | +You can set the autoscaler configuration via the Azure portal, a Bicep template, and the Azure CLI. |
| 18 | + |
| 19 | +# [Azure portal](#tab/portal) |
| 20 | + |
| 21 | +:::image type="content" source="media/durable-task-scheduler-auto-scaling-aca/scaler-configuration.png" alt-text="Screenshot of the scaler min and max replica configuration in the Azure portal."::: |
| 22 | + |
| 23 | +:::image type="content" source="media/durable-task-scheduler-auto-scaling-aca/scaler-configuration-details.png" alt-text="Screenshot of the Durable Task Scheduler-related configuration for the scaler in the Azure portal."::: |
| 24 | + |
| 25 | +| Field | Description | Example | |
| 26 | +| ----- | ----------- | ------- | |
| 27 | +| Min replicas | Minimum number of replicas allowed for the container revision at any given time. | 1 | |
| 28 | +| Max replicas | Maximum number of replicas allowed for the container revision at any given time. | 10 | |
| 29 | +| endpoint | The Durable Task Scheduler endpoint that the scaler connects to. | `https://dts-ID.centralus.durabletask.io` | |
| 30 | +| maxConcurrentWorkItemsCount | The maximum concurrent work items dispatched as an event to your compute, such as telling your compute to run an orchestration. | 1 | |
| 31 | +| taskhubName | The name of the task hub connected to the scheduler. | taskhub-ID | |
| 32 | +| workItemType | The work item type that is being dispatched. Options include Orchestration, Activity, or Entity. | Orchestration | |
| 33 | +| Managed identity | The user assigned or system assigned managed identity linked to the scheduler and task hub resource. Ensure the **Authenticate with a Managed Identity ** checkbox is selected. | [email protected] | |
| 34 | + |
| 35 | +# [Bicep](#tab/bicep) |
| 36 | + |
| 37 | +```bicep |
| 38 | +scale: { |
| 39 | + minReplicas: containerMinReplicas |
| 40 | + maxReplicas: containerMaxReplicas |
| 41 | + rules: [ |
| 42 | + { |
| 43 | + name: 'dts-scaler-orchestration' |
| 44 | + custom: { |
| 45 | + type: 'azure-durabletask-scheduler' |
| 46 | + metadata: { |
| 47 | + endpoint: dtsEndpoint |
| 48 | + maxConcurrentWorkItemsCount: '1' |
| 49 | + taskhubName: taskHubName |
| 50 | + workItemType: 'Orchestration' |
| 51 | + } |
| 52 | + identity: scaleRuleIdentity |
| 53 | + } |
| 54 | + } |
| 55 | + ] |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +| Field | Description | Example | |
| 60 | +| ----- | ----------- | ------- | |
| 61 | +| `minReplicas` | Minimum number of replicas allowed for the container revision at any given time. | `1` | |
| 62 | +| `maxReplicas` | Maximum number of replicas allowed for the container revision at any given time. | `10` | |
| 63 | +| `endpoint` | The Durable Task Scheduler endpoint that the scaler connects to. | `https://dts-ID.centralus.durabletask.io` | |
| 64 | +| `maxConcurrentWorkItemsCount` | The maximum concurrent work items dispatched as an event to your compute, such as telling your compute to run an orchestration. | `1` | |
| 65 | +| `taskhubName` | The name of the task hub connected to the scheduler. | `myTaskHubName` | |
| 66 | +| `workItemType` | The work item type that is being dispatched. Options include Orchestration, Activity, or Entity. | `Orchestration` | |
| 67 | +| `identity` | The user assigned or system assigned managed identity linked to the scheduler and task hub resource. | `[email protected]` | |
| 68 | + |
| 69 | + |
| 70 | +# [Azure CLI](#tab/cli) |
| 71 | + |
| 72 | +```azurecli |
| 73 | +az containerapp create \ |
| 74 | + --resource-group <RESOURCE_GROUP> \ |
| 75 | + --name <APP_NAME> \ |
| 76 | + --environment <ENVIRONMENT_ID> \ |
| 77 | + --user-assigned <USER_ASSIGNED_IDENTITY_ID> \ |
| 78 | + --scale-rule-name dtsscaler-orchestration \ |
| 79 | + --scale-rule-type azure-durabletask-scheduler \ |
| 80 | + --scale-rule-metadata "endpoint=<DTS-ENDPOINT>" "maxConcurrentWorkItemsCount=1" "taskhubName=<TASKHUB-NAME> "workItemType=Orchestration" \ |
| 81 | + --scale-rule-identity <USER_ASSIGNED_IDENTITY_ID> |
| 82 | +``` |
| 83 | + |
| 84 | +| Field | Description | Example | |
| 85 | +| ----- | ----------- | ------- | |
| 86 | +| `minReplicas` | Minimum number of replicas allowed for the container revision at any given time. | `1` | |
| 87 | +| `maxReplicas` | Maximum number of replicas allowed for the container revision at any given time. | `10` | |
| 88 | +| `endpoint` | The Durable Task Scheduler endpoint that the scaler connects to. | `https://dts-ID.centralus.durabletask.io` | |
| 89 | +| `maxConcurrentWorkItemsCount` | The maximum concurrent work items dispatched as an event to your compute, such as telling your compute to run an orchestration. | `1` | |
| 90 | +| `taskhubName` | The name of the task hub connected to the scheduler. | `myTaskHubName` | |
| 91 | +| `workItemType` | The work item type that is being dispatched. Options include Orchestration, Activity, or Entity. | `Orchestration` | |
| 92 | +| `scale-rule-identity` | The user assigned or system assigned managed identity linked to the scheduler and task hub resource. | `[email protected]` | |
| 93 | + |
| 94 | +--- |
| 95 | + |
| 96 | +## Experiment with the sample |
| 97 | + |
| 98 | +The [Autoscaling in Azure Container Apps sample](https://github.com/Azure-Samples/Durable-Task-Scheduler/tree/main/samples/scenarios/AutoscalingInACA) demonstrates how to implement autoscaling with the Azure Durable Task Scheduler using the .NET SDK in Azure Container Apps. The function chaining application pattern showcases an orchestration workflow that benefits from dynamically scaling worker instances based on load. |
| 99 | + |
| 100 | +> [!NOTE] |
| 101 | +> Although this sample uses the Durable Task .NET SDK, autoscaling is language-agnostic. |
| 102 | +
|
| 103 | +### Prerequisites |
| 104 | + |
| 105 | +- [.NET 8 SDK](https://dotnet.microsoft.com/download/dotnet/8.0) or later |
| 106 | +- [Docker](https://www.docker.com/products/docker-desktop/) (for building the image) |
| 107 | +- [Azure Developer CLI](/azure/developer/azure-developer-cli/install-azd) |
| 108 | + |
| 109 | +### Set up your environment |
| 110 | + |
| 111 | +1. Clone the `Azure-Samples/Durable-Task-Scheduler` directory. |
| 112 | + |
| 113 | + ```bash |
| 114 | + git clone https://github.com/Azure-Samples/Durable-Task-Scheduler.git |
| 115 | + ``` |
| 116 | + |
| 117 | +1. Authenticate with Azure using the Azure Developer CLI. |
| 118 | + |
| 119 | + ```azdeveloper |
| 120 | + azd auth login |
| 121 | + ``` |
| 122 | + |
| 123 | +### Deploy the solution using Azure Developer CLI |
| 124 | + |
| 125 | +1. Navigate into the `AutoscalingInACA` sample directory. |
| 126 | + |
| 127 | + ```azdeveloper |
| 128 | + cd /path/to/Durable-Task-Scheduler/samples/scenarios/AutoscalingInACA |
| 129 | + ``` |
| 130 | + |
| 131 | +1. Provision resources and deploy the application: |
| 132 | + |
| 133 | + ```azdeveloper |
| 134 | + azd up |
| 135 | + ``` |
| 136 | + |
| 137 | +1. When prompted in the terminal, provide the following parameters. |
| 138 | + |
| 139 | + | Parameter | Description | |
| 140 | + | --------- | ----------- | |
| 141 | + | Environment Name | Prefix for the resource group created to hold all Azure resources. | |
| 142 | + | Azure Location | The Azure location for your resources. | |
| 143 | + | Azure Subscription | The Azure subscription for your resources. | |
| 144 | + |
| 145 | + This process may take some time to complete. As the `azd up` command completes, the CLI output displays two Azure portal links to monitor the deployment progress. The output also demonstrates how `azd up`: |
| 146 | + |
| 147 | + - Creates and configures all necessary Azure resources via the provided Bicep files in the `./infra` directory using `azd provision`. Once provisioned by Azure Developer CLI, you can access these resources via the Azure portal. The files that provision the Azure resources include: |
| 148 | + - `main.parameters.json` |
| 149 | + - `main.bicep` |
| 150 | + - An `app` resources directory organized by functionality |
| 151 | + - A `core` reference library that contains the Bicep modules used by the `azd` template |
| 152 | + - Deploys the code using `azd deploy` |
| 153 | + |
| 154 | + **Expected output** |
| 155 | + |
| 156 | + ```azdeveloper |
| 157 | + Packaging services (azd package) |
| 158 | +
|
| 159 | + (✓) Done: Packaging service client |
| 160 | + - Image Hash: {IMAGE_HASH} |
| 161 | + - Target Image: {TARGET_IMAGE} |
| 162 | +
|
| 163 | +
|
| 164 | + (✓) Done: Packaging service worker |
| 165 | + - Image Hash: {IMAGE_HASH} |
| 166 | + - Target Image: {TARGET_IMAGE} |
| 167 | +
|
| 168 | +
|
| 169 | + Provisioning Azure resources (azd provision) |
| 170 | + Provisioning Azure resources can take some time. |
| 171 | +
|
| 172 | + Subscription: SUBSCRIPTION_NAME (SUBSCRIPTION_ID) |
| 173 | + Location: West US 2 |
| 174 | +
|
| 175 | + You can view detailed progress in the Azure Portal: |
| 176 | + https://portal.azure.com/#view/HubsExtension/DeploymentDetailsBlade/~/overview/id/%2Fsubscriptions%SUBSCRIPTION_ID%2Fproviders%2FMicrosoft.Resources%2Fdeployments%2FCONTAINER_APP_ENVIRONMENT |
| 177 | +
|
| 178 | + (✓) Done: Resource group: GENERATED_RESOURCE_GROUP (1.385s) |
| 179 | + (✓) Done: Virtual Network: VNET_ID (862ms) |
| 180 | + (✓) Done: Container Apps Environment: GENERATED_CONTAINER_APP_ENVIRONMENT (54.125s) |
| 181 | + (✓) Done: Container Registry: GENERATED_REGISTRY (1m27.747s) |
| 182 | + (✓) Done: Container App: SAMPLE_CLIENT_APP (21.39s) |
| 183 | + (✓) Done: Container App: SAMPLE_WORKER_APP (24.136s) |
| 184 | + |
| 185 | + Deploying services (azd deploy) |
| 186 | +
|
| 187 | + (✓) Done: Deploying service client |
| 188 | + - Endpoint: https://SAMPLE_CLIENT_APP.westus2.azurecontainerapps.io/ |
| 189 | +
|
| 190 | + (✓) Done: Deploying service worker |
| 191 | + - Endpoint: https://SAMPLE_WORKER_APP.westus2.azurecontainerapps.io/ |
| 192 | +
|
| 193 | +
|
| 194 | + SUCCESS: Your up workflow to provision and deploy to Azure completed in 10 minutes 34 seconds. |
| 195 | + ``` |
| 196 | + |
| 197 | +### Confirm successful deployment |
| 198 | + |
| 199 | +In the Azure portal, verify the orchestrations are running successfully. |
| 200 | + |
| 201 | +1. Copy the resource group name from the terminal output. |
| 202 | + |
| 203 | +1. Sign in to the [Azure portal](https://portal.azure.com) and search for that resource group name. |
| 204 | + |
| 205 | +1. From the resource group overview page, click on the client container app resource. |
| 206 | + |
| 207 | +1. Select **Monitoring** > **Log stream**. |
| 208 | + |
| 209 | +1. Confirm the client container is logging the function chaining tasks. |
| 210 | + |
| 211 | + :::image type="content" source="media/durable-task-scheduler-auto-scaling-aca/client-app-log-stream.png" alt-text="Screenshot of the client container's log stream in the Azure portal."::: |
| 212 | + |
| 213 | +1. Navigate back to the resource group page to select the `worker` container. |
| 214 | + |
| 215 | +1. Select **Monitoring** > **Log stream**. |
| 216 | + |
| 217 | +1. Confirm the worker container is logging the function chaining tasks. |
| 218 | + |
| 219 | + :::image type="content" source="media/durable-task-scheduler-auto-scaling-aca/worker-app-log-stream.png" alt-text="Screenshot of the worker container's log stream in the Azure portal."::: |
| 220 | + |
| 221 | +### Understanding the custom scaler |
| 222 | + |
| 223 | +This sample includes an `azure.yaml` configuration file. When you ran `azd up`, you deployed the entire sample solution to Azure, including a custom scaler for your container apps that automatically scales based on the Durable Task Scheduler's workload. |
| 224 | + |
| 225 | +The custom scaler: |
| 226 | + |
| 227 | +- Monitors the number of pending orchestrations in the task hub. |
| 228 | +- Scales the number of worker replicas up with increased workload. |
| 229 | +- Scales back down when the load decreases. |
| 230 | +- Provides efficient resource utilization by matching capacity to demand. |
| 231 | + |
| 232 | +### Confirm the scaler is configured |
| 233 | + |
| 234 | +Verify the autoscaling is functioning correctly in the deployed solution. |
| 235 | + |
| 236 | +1. In the Azure portal, navigate to your worker app. |
| 237 | + |
| 238 | +1. From the left side menu, click **Application** > **Revisions and replicas**. |
| 239 | + |
| 240 | +1. Click the **Replicas** tab to verify your application is scaling out. |
| 241 | + |
| 242 | +1. From the left side menu, click **Application** > **Scale**. |
| 243 | + |
| 244 | +1. Click the scale name to view the scaler settings. |
| 245 | + |
| 246 | +## Next steps |
| 247 | + |
| 248 | +> [!div class="nextstepaction"] |
| 249 | +> [Learn more about the Durable Task Scheduler](./durable-task-scheduler.md) |
0 commit comments