Skip to main content

Rate limiting per Consumer with limit-count

Give every Consumer of a Route its own request budget with limit-count, count in Redis across gateway nodes, and read the 429s in Traffic Explorer.

By Akif F. Güven Updated Gateway

What you will have at the end

A Route, orders-api, on which every Consumer may make a fixed number of requests per minute. The mobile app exceeding its budget is answered 429 while the partner portal, with its own budget, is not. The counter lives in Redis, so every gateway node of the Environment counts the same window. The refusals appear as request records in Traffic Explorer, with the Consumer’s name on each, and as a share of the Route’s traffic in Analytics.

Prerequisites

  • A Route with an authentication Plugin, so that requests carry a Consumer. Here orders-api serves /orders/* from the Upstream orders-backend and carries key-auth.
  • Two Consumers with a key-auth credential each: mobile_app and partner_portal. A credential is created on the Consumer’s Credentials tab; the key is shown as •••• afterwards.
  • A Redis the gateway nodes can reach. The address in this article is redis:6379.
  • Traffic logging turned on under Traffic Explorer › Settings, and a Prometheus the Environment reads.

Count, window and key

Open the Route, select the Plugins tab, Add plugin, and choose limit-count from the catalog. Under Basics, keep One limit for everything here and set the budget:

  • Count: how many requests one key may make in each window. The article uses 5, so the refusal is easy to reach by hand; a production value is a matter of the backend’s capacity.
  • Time window: 60 seconds.
  • Key type: var, the default. Key: consumer_name. The engine then counts under the name of the authenticated Consumer, so every Consumer gets a window of its own.

The limit-count form on the orders-api Route: Count 5, Time window 60 seconds, Key type var and Key consumer_name, with the note under Key in full One budget, counted per Consumer. The note under Key says what happens when the variable is empty: the request is counted by client address instead.

That note matters. consumer_name is only set once an authentication Plugin has admitted the request, which is why key-auth sits on the Route first. The engine runs key-auth at priority 2500 and limit-count at 1002, so a request without a valid key is refused with 401 before it is counted.

A limit for each key, the other choice in Basics, takes a list of rules, each with its own key, count and window, for a Route that needs different budgets for different kinds of callers.

The Redis policy for several nodes

Under Behavior, Policy is local by default: each gateway node keeps its own counter in memory, so an Environment with three nodes allows three times the budget. Choose redis and the fields of the store appear: Redis host redis, Redis port 6379, and the Redis password, which is stored on the engine and shown only to roles that manage the Route. redis-cluster and redis-sentinel are the other two choices, with their own fields.

The Behavior section of the limit-count form with Policy set to redis and the Redis fields it opens: Redis database 0, Redis host redis, the keepalive settings at their defaults With Policy set to redis, every node of the Environment reads and writes the same counter.

The engine keeps one counter per Consumer under the Route’s id, which is how the two budgets stay apart.

Allow degradation is off by default. The form explains the two choices: when the limit cannot be counted, because the store is unreachable, off answers 500 and on lets every request through unlimited. Decide this before the first incident, not during it.

The rejected code

The engine’s default answer for a request over the budget is 503. The first measurement of this article got exactly that: five 200s and then a 503, which Analytics would count as a server error. Set Rejected code to 429 under Behavior, and leave Show limit quota header on, so the client learns its budget from the response headers.

The Behavior section of the limit-count form: Rejected code 429, Show limit quota header on and Allow degradation off 429 is the answer a client library understands as “slow down”. Set it; the engine does not assume it.

Save the Plugin. The change is in the audit log as a Route update with the limit-count block as the difference and the Redis password masked.

The audit record of the Route update that added limit-count to orders-api: the Plugin block with count, time window, key, policy redis and the Redis host and port in the diff, and the Redis password masked The budget is a change like any other: who set it, when, and what it replaced, with the store password masked.

What a client sees

Eight requests from the mobile app within one minute, then one from the partner portal:

mobile_app      1 200  2 200  3 200  4 200  5 200  6 429  7 429  8 429
partner_portal  200

The refused response carries the budget:

HTTP/1.1 429 Too Many Requests
X-RateLimit-Limit: 5
X-RateLimit-Remaining: 0
X-RateLimit-Reset: 59

The 429 in Traffic Explorer

Open Observe › Traffic Explorer, type the Consumer’s name in the search field and choose Filter by Consumer. The list is the mobile app’s traffic alone: five 200s answered by the backend node, then 429s whose Upstream node reads gateway, because the engine answered them without calling the backend.

Traffic Explorer filtered to the Consumer mobile_app: five 200 records answered by the backend node followed by 429 records answered by the gateway, each naming the Route orders-api and the Consumer The window is visible in the records: the budget, then the refusals, then a new window a minute later.

Open a 429 record: the Overview names the Route and the Consumer, and the Response tab holds the x-ratelimit-limit, x-ratelimit-remaining and x-ratelimit-reset headers as the client received them.

The 4xx share in Analytics

The Route’s Analytics tab counts the same requests from Prometheus. The Requests card splits them by status class; after half an hour of the mobile app overrunning its budget once a minute, 4xx was 43 percent of the Route’s traffic. The Client errors card beside it splits the 4xx by code and by source: 429 and 401 only, all answered by the gateway and none by the backend, so the limit and the key check did their work before any request reached the orders service.

The Requests, Server errors and Client errors cards on the orders-api Analytics tab, read together: 283 requests, 57 percent 2xx and 43 percent 4xx, no 5xx, and 122 client errors of which 93 are 429 and 29 are 401, all from the gateway A rate limit that works shows up as 4xx. 429 from the limit, 401 from key-auth; a 4xx from the backend would appear under a second source.

What to do next

Written by
Akif F. Güven
Engineer, G8
Was this helpful?
Next step

See G8 in a demo

We’ll show you the console with APIs and AI traffic like yours, and answer the questions these articles leave open.

  1. 01Demo
  2. 02Proof of conceptOn Docker for a quick start, or on the infrastructure you plan to run G8 on.
  3. 03LicenseWe prepare a quote for your edition and nodes, and you receive a signed license file.