API rate limiting

更新时间:
复制 MD 格式

API rate limiting lets you set limits for individual APIs, default rate limits for all APIs, and a total rate limit for an application. This feature prevents your backend servers from being overwhelmed during peak traffic. If you set both a default API rate limit and a total application rate limit, the lower of the two limits takes precedence.

This topic describes how to configure default API rate limits and total application rate limits. To set a rate limit for a specific API, you can configure it on the API details page. For more information, see Configure an API.

Prerequisites

To use rate limiting configurations, you must first enable the rate limiting feature. Log on to the mPaaS console. In the navigation pane on the left, choose Mobile Gateway > Gateway Management. Then, turn on the Rate Limiting switch.

Default API rate limit

You can set a default rate limit for all APIs in the current application. The default rate limit is applied based on the following rules:

  • If a rate limit is configured for a specific API, that limit overwrites the default rate limit.

  • A per-API throttling configuration overwrites the default configuration.

  • Changes to the default rate limit configuration affect all APIs that use the default configuration.

Steps:

  1. Turn on the Default API Rate Limit Configuration switch.

  2. In the default rate limit configuration box, click Edit and configure the rate limit information.

    • Default rate limit: Set a reasonable threshold based on your business needs. Requests that exceed this value are throttled.

      Note

      The rate limit threshold is the maximum number of requests per second.

    • Throttling response: The default response for throttled requests is: {"resultStatus":1002,"tips":"The server is busy. Please try again later."} To customize the throttling response, use the following format:

        {
            "result": "==Enter your custom response content here==",
            "tips": "ok",
            "resultStatus": 1000,
        }

      Where:

      • result is the custom response data in JSON format. The client processes this field only when resultStatus is 1000.

      • tips is the custom throttling message. If resultStatus is 1002, this field is used to notify the user.

      • resultStatus is the result code returned for a throttled request. For more information about result code definitions, see Gateway result codes.

Total application rate limit

You can set a total rate limit for all APIs in the current application. If the total number of requests exceeds this limit, all subsequent requests to any API in the application are throttled.

Steps:

  1. Turn on the Total Application Rate Limit switch.

  2. In the total application rate limit configuration box, click Edit and configure the rate limit information.

    • Total rate limit: Set a reasonable threshold in requests per second based on your business needs. Requests that exceed this value are throttled.

    • Throttling response: The default response for throttled requests is: {"resultStatus":1002,"tips":"The server is busy. Please try again later."} To customize the throttling response, use the following format:

        {
            "result": "==Enter your custom response content here==",
            "tips": "ok",
            "resultStatus": 1000,
        }

      Where:

      • result is the custom response data in JSON format. The client processes this field only when resultStatus is 1000.

      • resultStatus is the result code returned for a throttled request. For more information about result code definitions, see Gateway result codes.

      • tips is the custom throttling message. If resultStatus is 1002, this field is used to notify the user.