The RateLimit policy in API Studio for the DataPower Nano Gateway: worked examples
Draft!!
RateLimit answers a question a plain count limit cannot: when does the count start again? This post runs seven variations against a real gateway, with the YAML from API Studio code view and the status the caller got.
The traffic post covers CountLimit, which has no window at all. With autoDecrement: false it counts calls and never resets.
The test project
One API Studio project, one API called nano-ratelimit, one operation per variation. Every operation calls the same echo backend, which returns the headers it received. A refused call never reaches it.
ExtractIdentity, Authenticate and Authorize check client credentials, then a RateLimit runs before an invoke.
Every call goes to the published product URL with the client credentials of a subscribed application. Most operations take a run query parameter. It is part of the limit key in these samples, so I can use a new bucket for each test:
curl -s \
-H "X-IBM-Client-Id: <client-id>" \
-H "X-IBM-Client-Secret: <client-secret>" \
"https://<product>-<catalog>-<org>.nanogw.<ingress-domain>/1.0.0/nano-ratelimit/rate/consume?run=test1"
How a limit is built
In API Studio a limit is two resources. A RateLimitDef holds the numbers. The RateLimit policy references it:
kind: RateLimitDef
spec:
max: 3
intervalLen: 1
intervalUnit: minute
extensions:
dp-nano-gateway:
apiVersion: api.ibm.com/v1
spec:
dynamicValue: $queryParameter('request', 'run')
kind: RateLimit
spec:
limits:
- limitDef:
name: quota
operation: consume
definition:
$ref: nanogw-policy-samples:nano-ratelimit-def-quota:1.0.0
max is the allowance for each interval. intervalLen and intervalUnit (second, minute, hour or day) set the window. max: 0 means unlimited. The reference page lists an inline form with the definition inside the policy. My project used the two-resource form above.
dynamicValue, weightExpr and exceedAction sit under extensions.dp-nano-gateway.spec in my project files. From my project notes, a copy of these at the top level of the definition lints, publishes and then does nothing. I did not re-run that.
The key is the extracted client identity plus dynamicValue. Without dynamicValue, every subscriber on the plan shares one bucket.
Example 1: three a minute
consume takes one unit per call. The window is max: 3 per minute, keyed on ?run=.
curl ... "https://<gateway-host>/1.0.0/nano-ratelimit/rate/consume?run=test1"
Four calls in a row:
| Call | Status |
|---|---|
| 1 | 200 |
| 2 | 200 |
| 3 | 200 |
| 4 | 429 |
The fourth call:
HTTP/2 429
Too Many Requests
The gateway answered. The backend never saw the fourth call. The refusal has no Content-Type and no Retry-After header, and I found no way to add one in the assembly. Clients cannot see when to come back.
Example 2: one bucket per tenant
Key the limit on a header the caller sends:
kind: RateLimitDef
spec:
max: 2
intervalLen: 1
intervalUnit: minute
extensions:
dp-nano-gateway:
apiVersion: api.ibm.com/v1
spec:
dynamicValue: $header('request', 'X-Tenant') & ':' & $queryParameter('request', 'run')
The & ':' & $queryParameter(...) tail exists only so I can replay the test. A real tenant limit is the header alone.
curl ... -H 'X-Tenant: acme' "https://<gateway-host>/1.0.0/nano-ratelimit/rate/keyed?run=test2"
Three calls with acme and one with globex:
| Tenant | Call | Status |
|---|---|---|
| acme | 1 | 200 |
| acme | 2 | 200 |
| acme | 3 | 429 |
| globex | 1 | 200 |
globex had its own two calls. Exhausting acme did not touch it.
Two cautions. A missing header turns into an empty string in the concatenation, so every caller without the header lands in one shared bucket. I did not test that one for this post. If the header is optional, key on the identity from ExtractIdentity instead. And a key the caller can choose freely is a denial-of-service tool. A tenant name is acceptable because the security policies authenticate the tenant first.
Example 3: weighted calls
weightExpr makes the cost of a call a number the request computes:
kind: RateLimitDef
spec:
max: 10
intervalLen: 1
intervalUnit: minute
extensions:
dp-nano-gateway:
apiVersion: api.ibm.com/v1
spec:
weightExpr: $number($header('request', 'X-Weight'))
dynamicValue: $queryParameter('request', 'run')
curl ... -H 'X-Weight: 7' "https://<gateway-host>/1.0.0/nano-ratelimit/rate/weighted?run=test3"
Four calls on the same key:
X-Weight |
Status | Units used |
|---|---|---|
| 7 | 200 | 7 of 10 |
| 4 | 429 | still 7 |
| 3 | 200 | 10 of 10 |
| 1 | 429 | still 10 |
A refused call does not spend anything. The remaining count is arithmetic, not a yes or no.
weightExpr must evaluate to a positive integer. I tried three bad values on fresh keys:
X-Weight |
Status |
|---|---|
| (no header) | 500 |
0 |
500 |
1.5 |
500 |
The runtime did not fall back to a weight of 1 and said nothing to the caller about the cause. Check the header before the limit runs, with a switch or a validate.
Use a weight to turn “one request” into “one unit of work”. A bulk endpoint can charge for the number of records in the body, and a health check can stay at 1.
Example 4: a refund that does not work across operations
consume takes a unit. replenish gives it back. This looks like a reservation: take the unit before the work and return it if the work fails.
kind: RateLimit
spec:
limits:
- limitDef:
name: refundable
operation: replenish
definition:
$ref: nanogw-policy-samples:nano-ratelimit-def-refundable:1.0.0
The definition is max: 1 per minute. I used a consume in one operation and the replenish above in another, both with the same limit name, the same definition and the same key. Four calls:
| Call | Status |
|---|---|
GET /rate/refundable |
200 |
GET /rate/refundable |
429 |
GET /rate/refund |
200 |
GET /rate/refundable |
429 |
The refund ran and returned 200. The bucket stayed empty. A replenish in one operation did not restore a consume in another. The counter is resolved per operation.
Put the consume and the replenish in the same operation. For example, consume before the invoke and replenish in a catch block. I did not test that layout, so check the arithmetic on your build.
replenish is addition, not a reset. The reference says it adds the weight back whether or not the limit was exceeded. To empty a window use update or a new key.
Example 5: count but do not refuse
exceedAction decides what an over-limit call does:
| Value | Over-limit result |
|---|---|
return429 |
429. The default |
return500 |
500 |
continue |
the call carries on |
With continue the counter still moves but nothing is refused:
kind: RateLimitDef
spec:
max: 1
intervalLen: 1
intervalUnit: minute
extensions:
dp-nano-gateway:
apiVersion: api.ibm.com/v1
spec:
exceedAction: continue
dynamicValue: $queryParameter('request', 'run')
Three calls with max: 1:
| Call | Status |
|---|---|
| 1 | 200 |
| 2 | 200 |
| 3 | 200 |
The second and third calls were over the limit and still reached the backend. This is how to roll out a limit: run it in continue mode, look at the traffic that would have been refused, then switch to return429. I did not look at how the over-limit calls are recorded.
Example 6: report it as a 500
kind: RateLimitDef
spec:
max: 1
intervalLen: 1
intervalUnit: minute
extensions:
dp-nano-gateway:
apiVersion: api.ibm.com/v1
spec:
exceedAction: return500
dynamicValue: $queryParameter('request', 'run')
First call 200. Second call:
HTTP/2 500
Internal Server Error
Use this only when a specific consumer forces you to, for example a client that backs off on 5xx but ignores 429. It is a deliberate false report. Your own error-rate dashboards and alerts count it.
Summary
- Pick
maxand the window first.max: 0ships the policy switched off. - Key on something the caller cannot choose. Use
dynamicValuefor tenants and leave it out for a limit per application. - A weight must be a positive integer. Anything else is a
500. - Keep
consumeandreplenishin one operation. - Start in
continuemode if you do not know the traffic yet.
Limitations and Notes
- Tested on a DataPower Nano Gateway with the API Connect 12.1 documentation set. Check the version you run.
- The counters are held by the gateway, not by the API. I did not test whether a republish resets them.
- Not tested:
checkandupdateoperations,intervalOffset,refandaliaslimits from the gateway configuration or a plan, and a window longer than a minute. I did not wait for a window to turn over, so the reset is not shown here. - I did not look at the gateway log for any of these calls.
