|
| 1 | +--- |
| 2 | +title: Scale Dapr applications with KEDA scalers using Bicep |
| 3 | +description: Learn how to use KEDA scalers to scale an Azure Container App and its Dapr sidecar. |
| 4 | +author: hhunter-ms |
| 5 | +ms.author: hannahhunter |
| 6 | +ms.service: container-apps |
| 7 | +ms.topic: conceptual |
| 8 | +ms.date: 04/17/2023 |
| 9 | +--- |
| 10 | + |
| 11 | +# Scale Dapr applications with KEDA scalers |
| 12 | + |
| 13 | +[Azure Container Apps automatically scales HTTP traffic to zero.](./scale-app.md) However, to scale non-HTTP traffic (like [Dapr](https://docs.dapr.io/) pub/sub and bindings), you can use [KEDA scalers](https://keda.sh/) to scale your application and its Dapr sidecar up and down, based on the number of pending inbound events and messages. |
| 14 | + |
| 15 | +This guide demonstrates how to configure the scale rules of a Dapr pub/sub application with a KEDA messaging scaler. For context, refer to the corresponding sample pub/sub applications: |
| 16 | +- [Microservice communication using pub/sub in **C#**](https://github.com/Azure-Samples/pubsub-dapr-csharp-servicebus) |
| 17 | +- [Microservice communication using pub/sub in **JavaScript**](https://github.com/Azure-Samples/pubsub-dapr-nodejs-servicebus) |
| 18 | +- [Microservice communication using pub/sub in **Python**](https://github.com/Azure-Samples/pubsub-dapr-python-servicebus) |
| 19 | + |
| 20 | + |
| 21 | +In the above samples, the application uses the following elements: |
| 22 | +1. The `checkout` publisher is an application that is meant to run indefinitely and never scale down to zero, despite never receiving any incoming HTTP traffic. |
| 23 | +1. The Dapr Azure Service Bus pub/sub component. |
| 24 | +1. An `order-processor` subscriber container app picks up messages received via the `orders` topic and processed as they arrive. |
| 25 | +1. The scale rule for Azure Service Bus, which is responsible for scaling up the `order-processor` service and its Dapr sidecar when messages start to arrive to the `orders` topic. |
| 26 | + |
| 27 | +:::image type="content" source="media/dapr-keda-scaling/scaling-dapr-apps-keda.png" alt-text="Diagram showing the scaling architecture of the order processing application."::: |
| 28 | + |
| 29 | +Let's take a look at how to apply the scaling rules in a Dapr application. |
| 30 | + |
| 31 | +## Publisher container app |
| 32 | + |
| 33 | +The `checkout` publisher is a headless service that runs indefinitely and never scales down to zero. |
| 34 | + |
| 35 | +By default, [the Container Apps runtime assigns an HTTP-based scale rule to applications](./scale-app.md), which drives scaling based on the number of incoming HTTP requests. In the following example, `minReplicas` is set to `1`. This configuration ensures the container app doesn't follow the default behavior of scaling to zero with no incoming HTTP traffic. |
| 36 | + |
| 37 | +```bicep |
| 38 | +resource checkout 'Microsoft.App/containerApps@2022-03-01' = { |
| 39 | + name: 'ca-checkout-${resourceToken}' |
| 40 | + location: location |
| 41 | + identity: { |
| 42 | + type: 'SystemAssigned' |
| 43 | + } |
| 44 | + properties: { |
| 45 | + //... |
| 46 | + template: { |
| 47 | + //... |
| 48 | + // Scale the minReplicas to 1 |
| 49 | + scale: { |
| 50 | + minReplicas: 1 |
| 51 | + maxReplicas: 1 |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +## Subscriber container app |
| 59 | + |
| 60 | +The following `order-processor` subscriber app includes a custom scale rule that monitors a resource of type `azure-servicebus`. With this rule, the app (and its sidecar) scales up and down as needed based on the number of pending messages in the Bus. |
| 61 | + |
| 62 | +```bicep |
| 63 | +resource orders 'Microsoft.App/containerApps@2022-03-01' = { |
| 64 | + name: 'ca-orders-${resourceToken}' |
| 65 | + location: location |
| 66 | + tags: union(tags, { |
| 67 | + 'azd-service-name': 'orders' |
| 68 | + }) |
| 69 | + identity: { |
| 70 | + type: 'SystemAssigned' |
| 71 | + } |
| 72 | + properties: { |
| 73 | + managedEnvironmentId: containerAppsEnvironment.id |
| 74 | + configuration: { |
| 75 | + //... |
| 76 | + // Enable Dapr on the container app |
| 77 | + dapr: { |
| 78 | + enabled: true |
| 79 | + appId: 'orders' |
| 80 | + appProtocol: 'http' |
| 81 | + appPort: 5001 |
| 82 | + } |
| 83 | + //... |
| 84 | + } |
| 85 | + template: { |
| 86 | + //... |
| 87 | + // Set the scale property on the order-processor resource |
| 88 | + scale: { |
| 89 | + minReplicas: 0 |
| 90 | + maxReplicas: 10 |
| 91 | + rules: [ |
| 92 | + { |
| 93 | + name: 'topic-based-scaling' |
| 94 | + custom: { |
| 95 | + type: 'azure-servicebus' |
| 96 | + metadata: { |
| 97 | + topicName: 'orders' |
| 98 | + subscriptionName: 'membership-orders' |
| 99 | + messageCount: '30' |
| 100 | + } |
| 101 | + auth: [ |
| 102 | + { |
| 103 | + secretRef: 'sb-root-connectionstring' |
| 104 | + triggerParameter: 'connection' |
| 105 | + } |
| 106 | + ] |
| 107 | + } |
| 108 | + } |
| 109 | + ] |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +## How the scaler works |
| 117 | + |
| 118 | +Notice the `messageCount` property on the scaler's configuration in the subscriber app: |
| 119 | + |
| 120 | +```bicep |
| 121 | +{ |
| 122 | + //... |
| 123 | + properties: { |
| 124 | + //... |
| 125 | + template: { |
| 126 | + //... |
| 127 | + scale: { |
| 128 | + //... |
| 129 | + rules: [ |
| 130 | + //... |
| 131 | + custom: { |
| 132 | + //... |
| 133 | + metadata: { |
| 134 | + //... |
| 135 | + messageCount: '30' |
| 136 | + } |
| 137 | + } |
| 138 | + ] |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | +} |
| 143 | +``` |
| 144 | + |
| 145 | +This property tells the scaler how many messages each instance of the application can process at the same time. In this example, the value is set to `30`, indicating that there should be one instance of the application created for each group of 30 messages waiting in the topic. |
| 146 | + |
| 147 | +For example, if 150 messages are waiting, KEDA scales the app out to five instances. The `maxReplicas` property is set to `10`, meaning even with a large number of messages in the topic, the scaler never creates more than `10` instances of this application. This setting ensures you don't scale up too much and accrue too much cost. |
| 148 | + |
| 149 | +## Next steps |
| 150 | + |
| 151 | +[Learn more about using Dapr components with Azure Container Apps.](./dapr-overview.md) |
0 commit comments