Rate limit your APIs

Rate limit your APIs

API rate limiting is a technique that is used to limit the number of invocations made to an API during the specified time interval. Limiting the number of invocations prevents overloading the API and in turn improves its performance.

Using the Traffic Optimization policy in API Gateway, you can limit the number of API invocations during a specified time interval. When the number of invocations exceeds the configured limit, API Gateway sends alerts to a specified destination.

The Traffic optimization policy generates two types of events when the specified limit is breached:

The following illustration explains how the configured ratelimit restricts the API invocation.

Why and when do you configure rate limit?

API providers configure rate limit to:

Rate limiting considerations

Rate limit ID Quota
Specifies the number of requests that can be made to an API over a relatively shorter period such as second or minute. Specifies the number of requests that a consumer can make to an API over a longer period, such as per day, per week, or per month.
Useful for managing traffic and preventing overload of an API in real-time. Useful for controlling the usage over a longer period and ensuring fair use of API resources.

How to rate limit to an API?

In this example, let us see the steps to enforce rate limit as 10 for the API, Petstore. That is, the policy does not allow more that ten invocations to the API.

To configure rate limit

  1. Click APIs in the title navigation bar.
    A list of available APIs appears.

  2. Click Create API.

  3. Select Import API from URL.

  4. Provide Petstore in the Name field.

  5. Provide the URL, https://petstore.swagger.io/v2/swagger.json.

  6. Click Create. The Petstore API is created, and the API details page appear.

  7. Click Edit.

  8. Click the Policies tab.

  9. Click Traffic Monitoring from the Policy Catalog section and click Traffic optimization. The policy is added. By default, the values Total Request Count and Greater than are selected in the Rule Name and Operator fields respectively.

  10. Provide 5 in the Value field. When the number of invocations go beyond five, the policy is considered violated.

  11. Select Consumer-specific throttling and type *.**, and select Each consumer. This is to specify that the configured invocation limit must apply to each consumer application individually.

  12. Select API Gateway under Destinations. The policy violation alerts are displayed in the API analytics page.

  13. Provide 1 in the Alert Interval field and select Minutes in the Unit field.

  14. Select Every Time in the Alert Frequency field to log a event every time the API is invoked beyond the specified limit.

  15. Provide Number of invocations reached the maximum limit. Please try later in the Alert message field.

  16. Click Save. The policy is enforced to the Petstore API. When the number of invocations go beyond 10, an e-mail with the specified Alert message is sent to the provided e-mail address.

Testing the API ratelimit

Before you begin

Ensure that you have

To test the GraphQL API using Postman

  1. Invoke the API for more than five times using a REST client. In this example, the API is invoked for more than five times. Notice the change in API response. API response till the rate limit is reached.

    API response after the rate limit is reached.

  2. Navigate to the API analytics page.

  3. View the bar graph plotted for the violation events for the Last 15 minutes.

  4. View the Runtime events section to view the alert message that you provided during policy configuration.