All Products
Search
Document Center

Auto Scaling:Create a custom target tracking scaling rule

Last Updated:Feb 17, 2025

You can create a custom target tracking scaling rule tailored to your business needs. This rule allows you to use a formula that integrates multiple Hybrid Cloud Monitoring metrics for scaling purposes.

Solution overview

Benefits

  • Support for formula operations. You can combine up to three metrics in a target tracking scaling rule by using formula operations, enabling more flexible management of scaling activities.

  • Support for more metrics. Custom target tracking scaling rules, unlike built-in ones, integrate with Hybrid Cloud Monitoring, enabling the use of additional metrics for scaling operations. For information about Hybrid Cloud Monitoring, see Overview.

Procedure

The solution described in this topic is developed based on the target tracking scaling rule feature of Auto Scaling and Hybrid Cloud Monitoring. You can refer to the following process to create a custom target tracking scaling rule:

  1. Design a target tracking scaling solution.

    Identify the Hybrid Cloud Monitoring metrics aligned with your business needs and develop a target tracking scaling solution.

  2. Execute the target tracking scaling solution.

    Prepare the necessary Hybrid Cloud Monitoring metrics and create a target tracking scaling rule based on these metrics to implement the solution.

  3. Continuously monitor scaling events and adjust the solution according to the insights gathered from monitoring.

    Constantly monitor scaling events in your scaling group and track metric changes to improve the scaling solution.

1. Design a target tracking scaling solution

Identify the Hybrid Cloud Monitoring metrics aligned with your business needs and develop a target tracking scaling solution. When you design your scaling solution, you can refer to the following example:

Appendix 1: Metrics describes all the supported Hybrid Cloud Monitoring metrics. To optimize target tracking response speed, we recommend that you use metrics with a statistical period of at least 60 seconds.
To ensure smooth scaling operations, adjust the calculation of metric values according to the number of instances in the scaling group. A scale-out is triggered when actual metric values exceed the target values, while a scale-in occurs when actual metric values fall below the target values.

Sample solution

This topic provides a sample solution that is implemented based on the number of active messages in a Simple Message Queue (SMQ) queue, formerly MNS queue, to dynamically adjust the number of instances in a scaling group.

  • Desired effect

    Each instance in the scaling group processes three messages at a time. If the message count exceeds this capacity, a scale-out operation is triggered. Conversely, if the message count drops below the threshold, a scale-in operation is triggered.

  • Implementation

    To achieve the desired effect, you must prepare the following metrics: ActiveMessages, which specifies the number of active messages in an SMQ queue, and RunningInstanceCount, which specifies the number of instances that are in the In Service state in the scaling group. Then, you can create a custom target tracking scaling rule based on the preceding metrics and set the target value to 3 (Auto Scaling maintains the metric values at 3). In this case, you can use the following formula: .

2. Implement the target tracking scaling solution

2.1 Prepare the required Hybrid Cloud Monitoring metrics

If you already prepared the required Hybrid Cloud Monitoring metrics, you can skip this step.
Before you perform the subsequent operations, make sure that Hybrid Cloud Monitoring is activated. For more information, see Activate Hybrid Cloud Monitoring.

Before you create a target tracking scaling rule, you must create metrics that you want to associate with the rule in the Hybrid Cloud Monitoring console for subsequent use. To create metrics, perform the following steps:

  1. Create a namespace.

    For more information, see Create a namespace.

  2. Add metrics to the namespace.

    You can add metrics to the namespace created in Step 1. For information about how to add metrics to a namespace, see Manage metric import tasks.

    Note

    To optimize target tracking response speed, we recommend that you use metrics with a statistical period of at least 60 seconds.

Sample metrics

Create a namespace named ess-metrics-repo-test and add the following metrics to the namespace:

  • ActiveMessages provided by SMQ (formerly MNS): the number of active messages

    image

  • RunningInstanceCount provided by Auto Scaling: the number of running instances

    image

2.2 Create a target tracking scaling rule

To create a custom target tracking scaling rule in the Auto Scaling console, perform the following steps:

Before you proceed, make sure that a scaling group is created. For information about how to create a scaling group, see Create a scaling group of the ECS type or Create a scaling group of the Elastic Container Instance type.
Important

You can execute a target tracking scaling rule to decrease the number of instances in a scaling group to 0. Before you create a target tracking scaling rule, configure the Minimum Number of Instances and Maximum Number of Instances parameters based on your business requirements. To prevent service downtime or excessive spending caused by the solution, carefully control the scaling boundaries.

  1. Go to the details page of the scaling group for which you want to create the rule.

    image

    Specific steps

    1. Log on to the Auto Scaling console.

    2. In the left-side navigation pane, click Scaling Groups.

    3. In the top navigation bar, select a region.

    4. Find the scaling group in which you want to create a custom target tracking scaling rule and click Details in the Actions column.

    5. In the upper part of the page that appears, click the Scaling Rules and Event-triggered Tasks tab.

    6. On the Scaling Rules tab, click Create Scaling Rule. Then, the Create Scaling Rule dialog box appears.

  2. Configure the parameters displayed in the Create Scaling Rule dialog box based on your business requirements.

    Parameter

    Description

    Rule Type

    The type of the scaling rule that you want to create. In this example, Target Tracking Scaling Rule is selected.

    Metric Type

    Hybrid Cloud Monitoring

    Monitor Namespace

    The namespace of the metrics that you want to specify in the scaling rule. In this example, the namespace created in Step 2.1 is selected.

    Metric Description

    The metrics that you want to specify in the scaling rule and the monitored resources. In this example, the metrics prepared in Step 2.1 are selected.

    Metric Calculation Expression

    The formula based on which the metric values are calculated. You can specify + - * / and () in each formula.

    Important
    • You can specify up to three metrics in each formula.

    • To ensure smooth scaling operations, adjust the calculation of metric values according to the number of instances in the scaling group. A scale-out is triggered when actual metric values exceed the target values, while a scale-in occurs when actual metric values fall below the target values.

    Target Value

    The target value of the metrics. Auto Scaling ensures that the values of the specified metrics remain close to the target value.

    Instance Warmup Time

    The period of time during which instances are in the Warmup state. Unit: seconds. Auto Scaling adds instances in the Warmup state to a scaling group but does not report their monitoring data to Hybrid Cloud Monitoring. When determining the number of instances to scale based on monitoring metrics, Auto Scaling excludes instances in the Warmup state from the count of existing instances in the scaling group. This prevents metric fluctuations until the warmup period concludes.

    Threshold for Scale-out Alerts

    When a target tracking scaling rule is created, an event-triggered task is automatically generated. This parameter defines the number of consecutive times the alert condition must be satisfied before the event-triggered task initiates a scale-out operation.

    Threshold for Scale-in Alerts

    When a target tracking scaling rule is created, an event-triggered task is automatically generated. This parameter defines the number of consecutive times the alert condition must be satisfied before the event-triggered task initiates a scale-in operation.

    Disable Scale-in

    Specifies whether to disable scale-in operations. This setting impacts the number of automatically generated event-triggered tasks.

    • When the Disable Scale-in feature is enabled, Auto Scaling creates only one event-triggered task for scale-out operations and does not remove instances from the scaling group.

    • When the Disable Scale-in feature is disabled, Auto Scaling creates separate event-triggered tasks for both scale-out and scale-in operations.

    Sample configurations

    Parameter

    Example

    Metric Type

    Hybrid Cloud Monitoring

    Monitor Namespace

    ess-metrics-repo-test

    Metric Description

    For Metric a:

    • Metric name: AliyunEss_RunningInstanceCount

    • Monitoring resource: current scaling group

    For Metric b:

    • Metric name: AliyunMnsnew_ActiveMessages

    • Monitoring resource: the SMQ queue that you want to monitor.

    Metric Calculation Expression

    b/a. The preceding formula specifies the average number of messages that must be processed on each instance.

    Target Value

    3. A value of 3 specifies that each instance has an average of three messages in queue.

  3. After you complete the configuration, click OK.

3. Constantly monitor scaling events and adjust the solution based on the monitoring findings

After creating the target tracking scaling rule, you must regularly monitor the scaling group to ensure the desired outcome is achieved. You can evaluate the performance of the scaling group by analyzing metrics, scaling activities, and monitoring data.

Related operation

  • View the target tracking scaling rule

    Choose Scaling Rules and Event-triggered Tasks > Scaling Rules. On the page that appears, find the target tracking scaling rule you want to view. Then, click the corresponding ID to go to the rule details page. You can also click the event-triggered task associated with the rule to view the monitoring information about the task.

    image

  • View metric calculations

    After you create a target tracking scaling rule, two event-triggered tasks are automatically created. Choose Scaling Rules and Event-triggered Tasks > Event-triggered Tasks > Event-triggered Task (Hybrid Cloud Monitoring). On the page that appears, find the desired event-triggered task and click the ID to go to the task details page. Then, view the metric calculations.

    image

    image

  • View scaling activities

    On the Scaling Activities tab of the scaling group details page, view the logs of scaling activities that occurred in the scaling group.

    image

Create a target tracking scaling rule by calling an API operation

You can call the CreateScalingRule API operation to create a custom target tracking scaling rule. The following table describes the request parameters.

Request parameters

Parameter

Type

Description

Operation

RegionId

String

The ID of the scaling group.

Set the value to the ID of the region where the desired scaling group resides.

ScalingGroupId

string

The ID of the scaling group.

Set the value to the ID of the desired scaling group.

ScalingRuleName

string

The name of the scaling rule.

The name must be 2 to 64 characters in length, and can contain letters, digits, underscores (_), hyphens (-), and periods (.). The name must start with a letter or a digit.

The name of each scaling rule must be unique within the same Alibaba Cloud account in a region.

If you leave this parameter empty, the scaling rule ID is used.

ScalingRuleType

string

The type of the scaling rule.

Set the value to TargetTrackingScalingRule.

DisableScaleIn

boolean

Specifies whether to disable scale-in. This parameter is available only if you set ScalingRuleType to TargetTrackingScalingRule.

  • true

  • false

Set the value based on your business requirements.

TargetValue

integer

The target value of the metric.

The value of TargetValue must be greater than 0 and can have a maximum of three decimal places.

EstimatedInstanceWarmup

integer

The warmup period of instances. Auto Scaling adds instances in the Warmup state to a scaling group but does not report their monitoring data to Hybrid Cloud Monitoring.

When determining the number of instances to scale based on monitoring metrics, Auto Scaling excludes instances in the Warmup state from the count of existing instances in the scaling group. This prevents metric fluctuations until the warmup period concludes.

Valid values: 0 to 86400. Unit: seconds.

Default value: 300.

ScaleOutEvaluationCount

integer

When a target tracking scaling rule is created, an event-triggered task is automatically generated. This parameter defines the number of consecutive times the alert condition must be satisfied before the event-triggered task initiates a scale-out operation.

Default value: 3.

Set the value based on your business requirements.

ScaleInEvaluationCount

integer

When a target tracking scaling rule is created, an event-triggered task is automatically generated. This parameter defines the number of consecutive times the alert condition must be satisfied before the event-triggered task initiates a scale-out operation.

Default value: 15.

Set the value based on your business requirements.

MetricType

string

The metric type.

If you want to create a custom target tracking scaling rule that is integrated with Hybrid Cloud Monitoring, set the value to hybrid.

HybridMonitorNamespace

string

The ID of the metric namespace.

For information about how to create a namespace, see Manage namespaces.

Set the value based on your business requirements.

HybridMetrics

array<object>

The metrics that you want to use.

Set the value to an array of Hybrid Cloud Monitoring metrics.

Sub-properties for HybridMetrics

Id

string

The reference ID of the metric in the metric expression.

A reference ID is similar to declaring a variable that is later used when you define a formula.

Sample values: a, b, and c.

MetricName

string

The name of the Hybrid Cloud Monitoring metric.

Set the value based on your business requirements.

Statistic

string

The statistical method of the metric values.

Set the value based on your business requirements. Valid values:

  • Average: calculates the average of all data points within the given interval.

  • Minimum: calculates the minimum of all data points within the given interval.

  • Maximum: calculates the maximum of all data points within the given interval.

Expression

string

The expression, which comprises multiple Hybrid Cloud Monitoring metrics. The calculation of the expression serves as the basis for triggering scaling operations.

It must adhere to the Reverse Polish Notation (RPN) specification, and only the following operators are supported: + - * /.

Set the value based on your business requirements.

Dimensions

object

The metric dimension, which specifies the resource that you want to monitor.

Sub-properties for Dimensions

DimensionKey

string

The key of the metric dimension.

Set the value based on your business requirements.

DimensionValue

string

The value of the metric dimension.

Set the value based on your business requirements.

Call example

The following API operation is called to implement the solution designed in Step 1.

Sample code:

{
    "RegionId": "cn-hangzhou",
    "ScalingGroupId": "asg-bp1******",
    "ScalingRuleType": "TargetTrackingScalingRule",
    "ScalingRuleName": "TrackingQueueLength",
    "DisableScaleIn": false,
    "TargetValue": 3,
    "EstimatedInstanceWarmup": 300,
    "ScaleOutEvaluationCount": 3,
    "ScaleInEvaluationCount": 15,
    "MetricType": "hybrid",
    "HybridMonitorNamespace": "ess-metrics-repo-test",
    "HybridMetrics.1.Id": "a",
    "HybridMetrics.1.MetricName": "AliyunEss_RunningInstanceCount",
    "HybridMetrics.1.Statistic": "Average",
    "HybridMetrics.1.Dimensions.1.DimensionKey": "instanceId",
    "HybridMetrics.1.Dimensions.1.DimensionValue": "asg-bp1******",
    "HybridMetrics.2.Id": "b",
    "HybridMetrics.2.MetricName": "AliyunMnsnew_ActiveMessages",
    "HybridMetrics.2.Statistic": "Average",
    "HybridMetrics.2.Dimensions.1.DimensionKey": "region",
    "HybridMetrics.2.Dimensions.1.DimensionValue": "cn-hangzhou",
    "HybridMetrics.2.Dimensions.2.DimensionKey": "userId",
    "HybridMetrics.2.Dimensions.2.DimensionValue": "******",
    "HybridMetrics.2.Dimensions.3.DimensionKey": "queue",
    "HybridMetrics.2.Dimensions.3.DimensionValue": "test",
    "HybridMetrics.3.Id": "Expression",
    "HybridMetrics.3.Expression": "b/a"
}

Core JSON snippet interpretation:

  • Declare a variable named a, set the variable value to the value of the AliyunEss_RunningInstanceCount metric from the ess-metrics-repo-test namespace, and then set the scaling group ID to asg-bp1******.

    Meaning: instanceId specifies the IDs of running instances in scaling group asg-bp1******.
    "HybridMetrics.1.Id": "a",
    "HybridMetrics.1.MetricName": "AliyunEss_RunningInstanceCount",
    "HybridMetrics.1.Statistic": "Average",
    "HybridMetrics.1.Dimensions.1.DimensionKey": "instanceId",
    "HybridMetrics.1.Dimensions.1.DimensionValue": "asg-bp1******",
  • Declare a variable named b, set the variable value to the value of the AliyunMnsnew_ActiveMessages metric from the ess-metrics-repo-test namespace, and then specify the region=cn-hangzhou, userId=******, and queue=test dimensions.

    Meaning: userId specifies the ID of the user account used to create active messages in the test queue within the China (Hangzhou) region.
    "HybridMetrics.2.Id": "b",
    "HybridMetrics.2.MetricName": "AliyunMnsnew_ActiveMessages",
    "HybridMetrics.2.Statistic": "Average",
    "HybridMetrics.2.Dimensions.1.DimensionKey": "region",
    "HybridMetrics.2.Dimensions.1.DimensionValue": "cn-hangzhou",
    "HybridMetrics.2.Dimensions.2.DimensionKey": "userId",
    "HybridMetrics.2.Dimensions.2.DimensionValue": "******",
    "HybridMetrics.2.Dimensions.3.DimensionKey": "queue",
    "HybridMetrics.2.Dimensions.3.DimensionValue": "test",
  • Design the b/a metric expression.

    Meaning: the implementation formula is.
    "HybridMetrics.3.Id": "Expression",
    "HybridMetrics.3.Expression": "b/a"
  • Set the target value.

    Meaning: The target value of the metric expression is 3.
    "TargetValue": 3