How to ensure idempotence

更新时间:
复制 MD 格式

When calling the Auto Scaling API, a request can time out or fail due to an internal server error. If your client retries the request, include the ClientToken parameter to prevent duplicate operations.

What is idempotence?

Idempotence is a property of some operations or resources in mathematics and computer science. It means that an operation or resource leads to the same result, regardless of how many requests are sent. Idempotence plays an important role in the distributed system design.

Ensure idempotence

Typically, a client should retry a request only if it receives a 500 (InternalError) or 503 (ServiceUnavailable) error, or if it does not receive a response. To ensure idempotence for retried requests when using the Auto Scaling API, generate a unique string of up to 64 ASCII characters on the client and assign it to the ClientToken parameter.

ClientToken

  • ClientToken is a unique, case-sensitive string of up to 64 ASCII characters that you generate on the client. For example: ClientToken=123e4567-e89b-12d3-a456-42665544****.

  • If you reuse a ClientToken with different request parameters, Auto Scaling returns an IdempotentParameterMismatch error.

    Note

    The SignatureNonce, Timestamp, and Signature parameters must change with each retry. Auto Scaling uses SignatureNonce to prevent replay attacks and Timestamp to mark the time of each request. Therefore, when you retry a request, you must provide different values for SignatureNonce and Timestamp, which in turn changes the Signature parameter.

  • After you include the ClientToken parameter:

    • If the request returns an HTTP 200 status code, retrying it with the same token returns the original result without changing the server state.

    • If the request returns a 4xx HTTP status code, retrying it will fail unless the error message suggests otherwise (for example, try it later). You must resolve the issue based on the error message before retrying the request.

Idempotent request example

For example, when you call the CreateScalingGroup API to create a scaling group, using the same ClientToken with identical request parameters ensures that the server returns the same response and creates only one scaling group.

http(s)://ess.aliyuncs.com/?Action=CreateScalingGroup
&ScalingGroupName=scalinggroup****
&InstanceId=i-28wt4****
&RegionId=cn-qingdao
&MinSize=2
&MaxSize=20
&DefaultCooldown=300
.......
&ClientToken=123e4567-e89b-12d3-a456-42665544****
&<common request parameters>

API list

The following list includes some of the APIs that support the ClientToken parameter. For a complete list, see the specific API documentation for each operation.