Updated on 2024-11-27 GMT+08:00

Configuring API Request Throttling

Request throttling limits the number of times APIs can be called by a user or app within a specific time period to protect backend services. The throttling can be down to the minute or second. To ensure service continuity of an API, create a request throttling policy for the API.

Usage Guidelines

  • Adding a request throttling policy to an API means binding them to each other. An API can be bound with only one request throttling policy for a given environment, but each request throttling policy can be bound to multiple APIs.
  • For APIs not bound with a request throttling policy, the throttling limit is the value of ratelimit_api_limits set on the Parameters page of the gateway.
  • An API can be bound with only one policy of the same type.
  • Policies are independent of APIs. A policy takes effect for an API only after they are bound to each other. When binding a policy to an API, you must specify an environment where the API has been published. The policy takes effect for the API only in the specified environment.
  • After you bind a policy to an API, unbind the policy from the API, or update the policy, you do not need to publish the API again.
  • Taking an API offline does not affect the policies bound to it. The policies are still bound to the API if the API is published again.
  • Policies that have been bound to APIs cannot be deleted.

Creating a Request Throttling Policy

  1. Go to the APIG console.
  2. Select a dedicated gateway at the top of the navigation pane.
  1. In the navigation pane, choose API Management > API Policies.
  2. On the Policies tab, click Create Policy.
  3. On the Select Policy Type page, select Request Throttling in the Traditional Policy area.
  4. Set the policy information.

    Table 1 Request throttling parameters

    Parameter

    Description

    Name

    Request throttling policy name.

    Type

    API-based or API-shared request throttling.

    • API-specific: Request throttling is based on every API to which the policy is bound.
    • API-sharing: Request throttling is based on all APIs as a whole to which the policy is bound.

    Period

    For how long you want to limit the number of API calls. This parameter can be used together with the following parameters:

    • Max. API Requests: Limit the maximum number of times an API can be called within a specific period.
    • Max. User Requests: Limit the maximum number of times an API can be called by a user within a specific period.
    • Max. Credential Requests: Limit the maximum number of times an API can be called by a credential within a specific period.
    • Max. IP Address Requests: Limit the maximum number of times an API can be called by an IP address within a specific period.

    Max. API Requests

    The maximum number of times each bound API can be called within the specified period.

    This parameter must be used together with Period.

    Max. User Requests

    The maximum number of times each bound API can be called by a user within the specified period. This limit only applies to APIs that are accessed through app or IAM authentication.

    • The value of this parameter cannot exceed that of Max. API Requests.
    • This parameter must be used together with Period.
    • If there are many users under your account that access an API, the request throttling limits of the API will apply to all these users.

    Max. Credential Requests

    The maximum number of times each bound API can be called by a credential within the specified period. This limit only applies to APIs that are accessed through app authentication.

    • The value of this parameter cannot exceed that of Max. User Requests or Max. API Requests.
    • This parameter must be used together with Period.

    Max. IP Address Requests

    Maximum times that an API can be requested by an IP address. You can configure the real_ip_from_xff parameter of the gateway to use the IP address in the X-Forwarded-For header as the basis for request throttling.

    • The value of this parameter cannot exceed that of Max. API Requests.
    • This parameter must be used together with Period.

    Description

    Description of the request throttling policy.

  5. Click OK.

    • To control the traffic of a credential, bind a request throttling policy to the credential by referring to Binding a Request Throttling Policy to a Credential. Traffic of the credential is limited by the excluded credential threshold, while traffic of APIs and users are still limited by the request throttling policy.
    • To control the traffic of a tenant, bind a request throttling policy to the tenant by referring to Binding a Request Throttling Policy to a Tenant. Traffic of the tenant is limited by the excluded tenant threshold, while traffic of APIs and users are still limited by the request throttling policy.

    After the policy is created, perform the operations described in Binding the Policy to APIs for the policy to take effect for the API.

Binding a Request Throttling Policy to a Credential

You have created a credential or obtained a credential ID from other tenants.

  1. On the request throttling policy details page, click the Excluded Credentials tab.
  2. Click Select Excluded Credential.
  3. Select a credential to exclude. You can use one of the following methods:

    • To select an existing credential, click Existing, select a credential, and enter a threshold.
    • To select a credential of other tenants, click Cross-tenant, and enter the credential ID and a threshold.

    Excluded credential thresholds take precedence over the value of Max. Credential Requests.

    For example, a request throttling policy has been configured, with Max. API Requests being 10, Max. Credential Requests being 3, Period being 1 minute, and two excluded credentials (max. 2 API requests for credential A and max. 4 API requests for credential B). If the request throttling policy is bound to an API, credential A and B can access the API 2 and 4 times within 1 minute, respectively.

Binding a Request Throttling Policy to a Tenant

  1. On the request throttling policy details page, click the Excluded Tenants tab.
  1. Click Select Excluded Tenant.
  2. Enter the tenant information.

    Table 2 Excluded tenant configuration

    Parameter

    Description

    Tenant ID

    Account ID or project ID. For details, see the description about Excluded Tenants in Table 1.

    Threshold

    The maximum number of times an API can be called by the tenant within a specified period.

    The value of this parameter cannot exceed that of Max. API Requests.

  3. Click OK.

    Excluded tenant thresholds take precedence over the value of Max. User Requests.

    For example, a request throttling policy has been configured, with Max. API Requests being 10, Max. User Requests being 3, Period being 1 minute, and two excluded tenants (max. 2 API requests for tenant A and max. 4 API requests for tenant B). If the request throttling policy is bound to an API, tenants A and B can access the API 2 and 4 times within 1 minute, respectively.

Binding the Policy to APIs

  1. Click a policy name to go to the policy details page.
  2. Select an environment and click Select APIs.
  3. Select the API group, environment, and required APIs.

    APIs can be filtered by API name or tag. The tag is defined during API creation.

  4. Click OK.

    • If an API no longer needs this policy, click Unbind in the row that contains the API.
    • If there are multiple APIs that no longer need this policy, select these APIs, and click Unbind above the API list. You can unbind a policy from a maximum of 1000 APIs at a time.