The Rate Limiting Advanced plugin supports three rate limiting strategies: local
, cluster
, and redis
.
This is controlled by the config.strategy
parameter.
Strategy
|
Description
|
Pros
|
Cons
|
local
|
Counters are stored in-memory on the node.
|
Minimal performance impact.
|
Less accurate. Unless there’s a consistent-hashing load balancer in front of Kong Gateway, it diverges when scaling the number of nodes.
|
cluster
|
Counters are stored in the Kong Gateway data store and shared across nodes.
|
Accurate1, no extra components to support.
|
Each request forces a read and a write on the data store. Therefore, relatively, the biggest performance impact. Not supported in hybrid mode or Konnect deployments.
|
redis
|
Counters are stored on a Redis server and shared across nodes.
|
Accurate1, less performance impact than a cluster policy.
|
Needs a Redis installation. Bigger performance impact than a local policy.
|
[1]: Only when config.sync_rate
option is set to 0
(synchronous behavior).
Two common use cases for rate limiting are:
-
Every transaction counts: The highest level of accuracy is needed. An example is a transaction with financial consequences.
-
Backend protection: Accuracy is not as relevant.
The requirement is only to protect backend services from overloading that’s caused either by specific users or by attacks.
In this scenario, because accuracy is important, the local
policy is not an option.
Consider the support effort you might need for Redis, and then choose either cluster
or redis
.
You could start with the cluster
policy, and move to redis
if performance reduces drastically.
If using a very high sync frequency, use redis
. Very high sync frequencies with cluster
mode are not scalable and not recommended.
The sync frequency becomes higher when the sync_rate
setting is a lower number - for example, a sync_rate
of 0.1 is a much higher sync frequency (10 counter syncs per second) than a sync_rate
of 1 (1 counter sync per second).
You can calculate what is considered a very high sync rate in your environment based on your topology, number of plugins, their sync rates, and tolerance for loose rate limits.
Together, the interaction between sync rate and window size affects how accurately the plugin can determine cluster-wide traffic.
For example, the following table represents the worst-case scenario where a full sync interval’s worth of data hasn’t yet propagated across nodes:
Property
|
Formula or config location
|
Value
|
Window size in seconds
|
Value set in config.window_size
|
5
|
Limit (in window)
|
Value set in config.limit
|
1000
|
Sync rate (interval)
|
Value set in config.sync_rate
|
0.5
|
Number of nodes (>1)
|
–
|
10
|
Estimated load balanced requests-per-second (RPS) to a node
|
Limit / Window size / Number of nodes
|
1000 / 5 / 10 = 20
|
Max potential lag in cluster count for a given node/s
|
Estimated load balanced RPS * Sync rate
|
20 * 0.5 = 10
|
Cluster-wide max potential overage/s
|
Max potential lag * Number of nodes
|
10 * 10 = 100
|
Cluster-wide max potential overage/s as a percentage
|
Cluster-wide max potential overage / Limit
|
100 / 1000 = 10%
|
Effective worst case cluster-wide requests allowed at window size
|
Limit * Cluster-wide max potential overage
|
1000 + 100 = 1100
|
If you choose to switch strategies, note that you can’t port the existing usage metrics from the Kong Gateway data store to Redis.
This might not be a problem with short-lived metrics (for example, seconds or minutes)
but if you use metrics with a longer time frame (for example, months), plan your switch carefully.
If accuracy is less important, choose the local
policy.
You might need to experiment a little before you get a setting that works for your scenario.
As the cluster scales to more nodes, more user requests are handled.
When the cluster scales down, the probability of false negatives increases.
Make sure to adjust your rate limits when scaling.
For example, if a user can make 100 requests every second, and you have an equally balanced 5-node Kong Gateway cluster, you can set the local
limit to 30 requests every second.
If you see too many false negatives, increase the limit.
To minimize inaccuracies, consider using a consistent-hashing load balancer in front of Kong Gateway.
The load balancer ensures that a user is always directed to the same Kong Gateway node, which reduces inaccuracies and prevents scaling problems.
When the redis
strategy is used and a Kong Gateway node is disconnected from Redis, the rate-limiting-advanced
plugin will fall back to local
.
This can happen when the Redis server is down or the connection to Redis broken.
Kong Gateway keeps the local counters for rate limiting and syncs with Redis once the connection is re-established.
Kong Gateway will still rate limit, but the Kong Gateway nodes can’t sync the counters. As a result, users will be able
to perform more requests than the limit, but there will still be a limit per node.