{
"version": 3,
"name": "My lovely gateway",
"port": 8084,
"cache_ttl": "3600s",
"timeout": "3s",
"extra_config": {
},
"endpoints": [
{
"endpoint": "/rate",
"method": "GET",
"output_encoding": "json",
"extra_config": {
"qos/ratelimit/router": {
"max_rate": 4,
"every": "5m",
"capacity": 4
}
},
"backend": [
{
"url_pattern": "/rate",
"method": "GET",
"host": [
"http://127.0.0.1:8091/"
]
}
],
"input_query_strings":[
"*"
],
"input_headers": [
"*"
]
}
]
}
This is my krakend.json file.I want to add rate limit in krakend service.I get the reference from this link. But When I try to hit the server from postman it didn't return 503 unavailable message.I alos try to hit from browser.How to add a basic rate limit in krakend? N.B : The backend server is a basic express.js server.Should I also add rate limit in that server or it's solely handled by krakend end.
Rate limit should be applied by KrakenD, without implementing anything additional in your backend. Your configuration seems OK. What KrakenD version are you running? You can check it executing krakend version
. The "every" parameter was introduced in rate limit component in KrakenD 2.4.x, so please verify that you're running the latest KrakenD version.
Below you can find a simplified KrakenD configuration and the expected output when rate limit is set to 4 every 5 minutes and you send 6 calls.
The configuration:
{
"$schema": "https://www.krakend.io/schema/krakend.json",
"version": 3,
"name": "My lovely gateway",
"port": 8080,
"endpoints": [
{
"endpoint": "/rate",
"extra_config": {
"qos/ratelimit/router": {
"max_rate": 4,
"every": "5m",
"capacity": 4
}
},
"backend": [
{
"url_pattern": "/__debug",
"host": ["http://localhost:8080/"]
}
]
}
]
}
The hey
command to launch 6 requests, 1 every second:
hey -n 6 -c 1 http://localhost:8080/rate
The output (just status codes):
[...]
Status code distribution:
[200] 4 responses
[503] 2 responses
KrakenD implements the token bucket algorithm to refresh the available tokens for new requests every set amount of time. To better understand the token bucket algorithm and how it affects the rate limit calculation over time I recommend reading https://www.krakend.io/docs/throttling/token-bucket/
I hope it helps!