All Products
Search
Document Center

Alibaba Cloud SDK:Configure a retry mechanism

Last Updated:May 22, 2025

In Alibaba Cloud SDK V2.0, the API request processing logic includes the built-in retry logic for network exceptions. When a request fails due to network exceptions, the system automatically retries to reinitiate the request to ensure service stability and reliability. However, if the request fails due to business logic errors, such as parameter errors and missing resources, the system does not perform retries. In this case, you must adjust the request based on the corresponding error message instead of performing retries. This topic describes how to configure a retry mechanism in Alibaba Cloud SDK V2.0 for Node.js.

Methods

Note

The priority levels of methods that are used to configure a retry mechanism are in the following descending order: RuntimeOptions > the default retry settings.

  • We recommend that you use the default retry settings. By default, no retries are performed when exceptions occur. If you enable the automatic retry mechanism but do not specify the maximum number of retries, up to three retries are performed by default.

  • Configure a retry mechanism by using RuntimeOptions.

    const { default: Ecs20140526, ModifySecurityGroupRuleRequest, DescribeRegionsRequest } = require('@alicloud/ecs20140526');
    const { Config } = require('@alicloud/openapi-client');
    const { RuntimeOptions } = require('@alicloud/tea-util');
    
    async function main() {
        const config = new Config({
            // Obtain the AccessKey ID of the RAM user from an environment variable.
            accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
            // Obtain the AccessKey secret of the RAM user from an environment variable.
            accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET,
            // The region that you want to access.
            regionId: 'cn-beijing',
        });
        const client = new Ecs20140526(config);
        const request = new DescribeRegionsRequest();
        // Create a RuntimeOptions instance and configure runtime parameters. 
        const runtime = new RuntimeOptions({
            // Enable the automatic retry mechanism.
            autoretry: true,
            // Specify the maximum number of automatic retries.
            maxAttempts: 3,
        });
        const resp = await client.describeRegionsWithOptions(request, runtime);
        console.log(resp.headers);
        console.log(resp.body);
    }
    
    main();
    import Ecs20140526, * as $Ecs20140526 from '@alicloud/ecs20140526';
    import * as $OpenApi from '@alicloud/openapi-client';
    import * as $Util from '@alicloud/tea-util';
    
    export default class Client {
        static async main(): Promise<void> {
            const config = new $OpenApi.Config({
                // Obtain the AccessKey ID of the RAM user from an environment variable.
                accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
                // Obtain the AccessKey secret of the RAM user from an environment variable.
                accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET,
                // The region that you want to access.
                regionId: 'cn-beijing',
            });
            const client = new Ecs20140526(config);
            const request = new $Ecs20140526.DescribeRegionsRequest();
            // Create a RuntimeOptions instance and configure runtime parameters. 
            const runtime = new $Util.RuntimeOptions({
                // Enable the automatic retry mechanism.
                autoretry: true,
                // Specify the maximum number of automatic retries.
                maxAttempts: 3,
            });
            const resp = await client.describeRegionsWithOptions(request, runtime);
            console.log(resp.headers);
            console.log(resp.body);
        }
    }