All Products
Search
Document Center

Scaling control by volume instance

Last Updated: Mar 06, 2020

You can configure the maximum concurrency for your account in each region. The limit is the number of requests processed by all functions in a region. This feature prevents unexpected invocations from generating unnecessary charges.

For example, if the maximum concurrency of the 12345789 account is 100 and the account contains three functions, namely func-a, func-b, and func-c, it indicates that the number of instances that are concurrently processing requests can be up to 100.

In Function Compute V1.0, an instance can process only one request. The maximum achievable concurrency depends on the number of instances. In Function Compute V2.0 and later, one instance can concurrently process multiple requests. For more information, see Concurrent requests processed by one instance.

Apart from the maximum concurrency of all the functions in an account, Function Compute enables you to specify the maximum number of pay-as-you go instances by using the console or calling API operations. This feature can protect function resources and prevent functions from being over-invoked which can result in unnecessary charges.

Scenarios

  • Ensure the concurrency of a function required for normal business.
    • For example, an account contains the func-a and func-b functions, and func-a is deployed for key business. If func-b is over-invoked, func-a will not be assigned the required instances for normal business. You can specify the maximum concurrency for func-b to prevent it from overconsuming resources.
  • Protect downstream services.
    • For example, if Function Compute makes a large amount of access requests to ApsaraDB for RDS, you can specify the maximum concurrency for functions that access ApsaraDB for RDS. In this way, RDS will not crash because of requests that exceed the processing capacity of RDS.
  • Terminate invocations of a function.
    • If a function that is invoked is under exception, you can stop the invocation by specifying the maximum concurrency to 0.
  • Prevent over-invocations from causing excessive costs.
    • You can specify the maximum concurrency of a function to prevent unexpected invocations caused by the browser or client.
  • Use with reserved instances.
    • You can configure the maximum concurrency of pay-as-you-go instances and reserved instances and use one or both types of instances as needed.

Aliases and the maximum concurrency of pay-as-you-go instances

  • To configure the maximum concurrency for a function, you are configuring for the aliases that each points to a unique version or the LATEST version of the function.
  • You can specify the maximum concurrency for each alias of a function.
  • You can distinguish an alias and the corresponding concurrency based on the qualifier parameter of the API operation that invokes the function.
  • If your service does not require other versions of the function, you can only specify the maximum concurrency for the LATEST version generated automatically by Function Compute.

Example:

  • The function of the service s has the alias prod and alias test that point to the version 1 and version 2. The LATEST version is the version 2 with the test alias.
  • Assume that the maximum concurrency of the account is 200 and that no other functions in the account occupy the concurrency. The following table lists the configuration of the function.
serviceName qualifier functionName path Allowed maximum concurrency Specified maximum concurrency
s prod func-foo /services/s.prod/functions/func-foo 100 100
s test func-foo /services/s.test/functions/func-foo 10 10
s LATEST func-foo /services/s.LATEST/functions/func-foo or /services/s/functions/func-foo 20 20

Invocation operations after the concurrency is specified

Type Operation
Synchronous invocation When the required number of instances exceeds the maximum concurrency, the exceeded requests are denied and the ResourceExhausted error is displayed.
Asynchronous invocation When the number of required instances exceeds the maximum concurrency, the requests are not denied but the asynchronous invocation is slowed down. The number of occupied instances does not exceed the maximum concurrency.

For more information, see Invoking a function

Use pay-as-you-go and reserved instances in combination

If an alias of your function has reserved instances, the reserved instances are used first. When all reserved instances are processing requests, new requests are processed by pay-as-you-go instances.

For example, you specify the maximum concurrency for pay-as-you-go and reserved instances in the following three ways. The service is service-1, the alias is prod, and the function is foo. You can invoke the function through the /services/service-1.prod/functions/foo path.

Pay-as-you-go instance limit Reserved instance limit Operation
0 10 Only the 10 reserved instances can be used.
20 0 Up to 20 pay-as-you-go instances and no reserved instances can be used.
50 30 Up to 50 pay-as-you-go instances can be used after all 30 reserved instances are used. You can use up to 80 instances in total.

Limits

  • You can configure up to 100 concurrency rules for an account in each region.
  • The concurrency rules must be applied to aliases or the LATEST version of a function.
  • The maximum concurrency of a rule must be no larger than the maximum concurrency of the account.

Calculate the number of function instances, function concurrency, and TPS

  • Execution duration: starts from the time when a request reaches your function and ends when the function has processed the request and returned it to Function Compute.
  • Maximum concurrency of an instance: In Function Compute V1.0, an instance can only process one request. In Function Compute V2.0 and later, an instance can concurrently process multiple requests as needed.
  • Concurrency: the number of requests being processed at a time point.
  • The following figure shows the detailed procedure.

    • If the maximum concurrency of an instance is 2, it indicates an instance can concurrently process two requests.
    • `Four invocation requests A, B, C, and D are concurrently initiated.
    • `Instance 1 is active at the t0 time point and only processes request A. The concurrency of Instance 1 is 1.
    • `Instance 1 and Instance 2 are active at the t1 time point when requests A, B, C, and D are being processed. The total concurrency is 4.
  • TPS: the number of requests that a service can process in one second.

    • TPS = 1/DurationInSecond × InstanceConcurrency × MaxInstances
  • Example:
    • Assume that a request takes 0.1 second to process (DurationInSecond = 0.1s) and the function contains 5 instances (MaxInstances = 5).
    • If an instance processes only one request at a time (InstanceConcurrency = 1), the instance can process 10 requests per second, and the five instances of the function can process 10 × 1 × 5 = 50 requests per second (TPS = 50).
    • If an instance can concurrently process up to two requests (InstanceConcurrency = 2), the five instances can process 10 × 2 × 5 = 100 requests per second (TPS = 100).

Configure the maximum concurrency of pay-as-you-go instances

Configure through the console

  • Log on to the Function Compute console, find the service to which the target function belong, click the On-Demand Resources tab, and click Configure Instances, as shown in the following figure. Pay-as-you-go instances
  • On the page that appears, specify Function Name as the function to be configured. You can view the number of instances used by this function in the specified Time Range in the line chart.Configure instances for a function
  • You can estimate the number of required instances based on the number of recently used instances, and specify the maximum number of instances in the Maximum Instances spin box.

Configure through the SDK

  • For more information about how to use the Java SDK to configure the concurrency, see Example.
  • Use the Golang SDK to configure concurrency:
  1. package main
  2. import (
  3. "fmt"
  4. fc "https://github.com/aliyun/fc-go-sdk"
  5. )
  6. const (
  7. endpoint = "your-endpoint"
  8. fcAPIVersion = "2016-08-15"
  9. AccessKeyID = "your-ak-id"
  10. AccessKeySecret = "your-ak-secret"
  11. )
  12. var (
  13. serviceName = "service-name"
  14. qualifier = "LATEST"
  15. functionName = "function-name"
  16. )
  17. func main() {
  18. fcClient, _ := fc.NewClient(endpoint, fcAPIVersion, AccessKeyID, AccessKeySecret)
  19. // set
  20. putInput := fc.NewPutOnDemandConfigInput(serviceName, qualifier, functionName)
  21. putInput.WithMaximumInstanceCount(10)
  22. putResp, err := fcClient.PutOnDemandConfig(putInput)
  23. fmt.Println("put on-demand config", putResp, err)
  24. // list
  25. listInput := fc.NewListOnDemandConfigsInput()
  26. listInput.WithPrefix(fmt.Sprintf("services/%s", serviceName))
  27. listResp, err := fcClient.ListOnDemandConfigs(listInput)
  28. fmt.Println("list on-demand configs", listResp, err)
  29. // get
  30. getInput := fc.NewGetOnDemandConfigInput(serviceName, qualifier, functionName)
  31. getResp, err := fcClient.GetOnDemandConfig(getInput)
  32. fmt.Println("get on-demand configs", getResp, err)
  33. // delete
  34. deleteInput := NewDeleteOnDemandConfigInput(serviceName, qualifier, functionName)
  35. deleteResp, err := fcClient.DeleteOnDemandConfig(deleteInput)
  36. fmt.Println("delete on-demand configs", deleteResp, err)
  37. }