I have configured tyk in my local machine and i have a trial account for now just to explore tyk and i have managed to successfully configure my first API and it is running fine but when i am enabling the Rate limiting option and set the Rate: to “4” in “20” Per (seconds) so what happening is it is limiting the API in “2” in “20” seconds, if i set Rate to 6 the api will trigger 3 times and on 10 it triggers only 5 times and so on so the API is running on half of it’s rate limit so i can not figure out why this is happening as i am new to this platform. Any help?
hi @taby welcome to the community!
What method are you using to test the rate limiting function?
can you post your config and we can take a look?
I am using the rate limiting option under APIs->My API->Rate Limiting and Quotas.
and do you want me to post full tyk.conf file?
The general idea is that Tyk gives multiple rate limit algorithms, each with own specifics.
By default, Tyk use so-called distributed rate limiter, which is fast in-memory algorithm, which works by equally distributing “rate” chunks to each gateways, and synchronizing them from time to time.
It works well in the majority of cases, however have an issue when you have small rate limit like yours.
So if you Rate limit is set to 2, then each gateway has only 1 request allowance, and chance of false positive is very big. If it is happening for you with a single gateway, most likely it is some sort of edge case (but it will happen more the more gateways you have). On the other hand if you have rate limit set to 100 per second, there will be WAY less false positives.
Also worth noticing that in Tyk there are two mechanics for limiting users:
- Rate limits: used to protect your services, and if there is some inaccuracy here it is not that critical
- Quotas: usually used for the billing purpose, and in this case it require to be accurate. Quotas are always accurate (but a bit slower since they need to do a call to the database on each request).
If you need accuracy for rate limits too, you just need to change Rate limiting algorithm and set
"enable_redis_rolling_limiter": true in your gateway config. It will make Gateway to do one more call to Redis on each request, but it will be 100% accurate.
Hope it makes sense!