Skip to main content

Version: 3.2.14.3

Apply Rate Limiting to APIs

Applying rate limiting controls the number of requests sent to your API backend. This protects your backend from too much traffic, both wanted and unwanted (web crawlers, DDoS attacks), which can result in operational inefficiencies and higher costs.

This guide walks you through applying rate limits to control the requests sent to your upstream nodes over time.

Rate Limiting

Prerequisite(s)

  1. Install API7 Enterprise.
  2. Have a running API on the gateway group.

You should not configure rate-limiting plugins globally, as different APIs typically require different rate-limiting quotas. If you configure the same plugin globally (in a global rule) and locally (in a route), the API7 Gateway executes both plugin instances sequentially.

Apply Rate Limiting for a Single Route

Limit Request Count

This section configures a route with rate limiting to only allow 3 requests in 60 seconds. When the limit is exceeded, a 429 status code is returned to the consumer.

  1. Select Published Services of your gateway group from the side navigation bar, then click the service you want to modify, for example, httpbin with version 1.0.0.
  2. Under the published service, select Routes from the side navigation bar.
  3. Select your target route, for example, getting-started-anything.
  4. Search for the limit-count plugin.
  5. Click the Plus icon (+).
  6. In the dialog box, do the following:
  • Add the following configuration to the JSON Editor:

    {
    "count": 3,
    "time_window": 60,
    "key_type": "var",
    "rejected_code": 429,
    "rejected_msg": "Too many requests",
    "policy": "local",
    "allow_degradation": false,
    "show_limit_quota_header": true
    }
  • Click Enable.

Below is an interactive demo that provides a hands-on introduction to limiting request numbers. You will gain a better understanding of how to use it in API7 Enterprise by clicking and following the steps.

Validate

To validate, send five consecutive requests to the route:

for i in {1..5}; do curl -i '127.0.0.1:9080/ip';  done

The first three requests will be successful and the last two will be rejected with a 429 Too Many Requests status code:

Date: Fri, 01 Jun 2024 04:43:51 GMT
Content-Type: text/plain; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
X-RateLimit-Limit: 3
X-RateLimit-Remaining: 0
X-RateLimit-Reset: 0
Server: API7/3.2.13.0

{"error_msg":"Too many requests"}

Limit Requests per Second

This section configures a route with rate-limiting to only allow 1 request per second. When the number of requests per second is between 1 and 3 they will be delayed/throttled. When the number of requests per second is more than 3, a 429 status code is returned.

  1. Select Published Services of your gateway group from the side navigation bar, then select the service you want to modify, for example, httpbin with version 1.0.0.
  2. Under the published service, select Routes from the side navigation bar.
  3. Select your target route, for example, getting-started-anything.
  4. Search for the limit-req plugin.
  5. Click the Plus icon (+).
  6. In the dialog box, do the following:
  • Add the following configuration to the JSON Editor:

    {
    "rate": 1,
    "burst": 2,
    "rejected_code": 429,
    "key_type": "var",
    "key": "remote_addr",
    "rejected_msg": "Requests are too frequent, please try again later."
    }
  • Click Enable.

Validate

To validate, send five requests to the route:

for i in {1..5}; do curl -i '127.0.0.1:9080/ip';  done 

You will get back the required response because the requests are sequential. Now send five concurrent requests to the route:

curl -i "http://127.0.0.1:9080/ip" & \
curl -i "http://127.0.0.1:9080/ip" & \
curl -i "http://127.0.0.1:9080/ip" & \
curl -i "http://127.0.0.1:9080/ip" & \
curl -i "http://127.0.0.1:9080/ip"

Three of these requests will have the required response, and the other two will be rejected with the following response:

HTTP/1.1 429 Too Many Requests
Date: Fri, 01 Jun 2024 04:43:51 GMT
Content-Type: text/plain; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Server: API7/3.2.13.0

{"error_msg":"Requests are too frequent, please try again later."}

Additional Resource(s)


API7.ai Logo

API Management for Modern Architectures with Edge, API Gateway, Kubernetes, and Service Mesh.

Product

API7 Cloud

SOC2 Type IRed Herring

Copyright © APISEVEN Ltd. 2019 – 2024. Apache, Apache APISIX, APISIX, and associated open source project names are trademarks of the

Apache Software Foundation