# Rate limit

EMQX broker can specifies the limit on access speed and message speed, this is a backpressure scheme that avoids system overload from the entrance and guarantees system stability and predictable throughput.

In 5.0 we introduced a new rate limiter which based on the hierarchical token bucket algorithm, it can flexibly and accurately control the usage rate of the corresponding resources in the EMQX node.

# Terminologies

# Resource

The system currently supports rate control services for the following resources:

TypeDescriptionPost-Overload Behavior
bytes_inIncoming message size in bytes per secondPause receiving client messages
message_inIncoming messages per secondPause receiving client messages
connectionConnections per secondPause for new connections
message_routingThe number of messages deliver by the session per secondPause session delivery

# Rate Control Hierarchy

  • Node level: resource rate control on the current EMQX node
  • Listener level: The aggregate rate limit of all connections (sessions) coming in through a same listener
  • Connection level: rate control of resources on a single connection (session)

# Hierarchy

The hierarchical relationship is shown in the figure:

image

# Wooden Barrel Effect

No matter how much the rate of the lower layer is configured, it is limited by the actual rate of the upper layer, and it is guaranteed that it will never exceed the actual rate limit of the upper layer

# Useage

# Connection Level

The connection-level rate limit is for a single connection. Assuming that the inflow rate of each session accessed through port 1883 needs to be limited to 100 messages per second, you only need to modify the configuration of port 1883 in emqx.conf as follows:

listeners.tcp.default {
  bind = "0.0.0.0:1883"
  max_connections = 1024000
  limiter.client.message_in {
  rate = "100/s"
  capacity = 100
  }
}
1
2
3
4
5
6
7
8

All options supported at this level are as follows:

OptionsTypeDefaultDescription
rateintinfinitytoken generation rate
initialint0initial number of tokens
capacityintinfinitymaximum number of tokens
low_watermarkint0The low water mark, when the number of tokens is lower than this number, even if the token request is successful, the rate also will be slightly limited
divisibleboolfalseWhether the token request can be split into multiple requests when the number of tokens is not enough
max_retry_timeduration10sThe maximum attempt time for token request, if no token is obtained after this time, the request fails

# Listener Level

The listener-level limite the total rate limit of all sessions connected through a same listener. For example, if you want the sum of messages in per second does not exceed 100 of all sessions that connected through port 1883, you can modify the configuration as follows:

listeners.tcp.default {
  bind = "0.0.0.0:1883"
  max_connections = 1024000
  limiter.message_in {
  rate = "100/s"
  capacity = 100
  }
}
1
2
3
4
5
6
7
8

All options supported at this level are as follows:

OptionsTypeDefaultDescription
rateintinfinitytoken generation rate
initialint0initial number of tokens
capacityintinfinitymaximum number of tokens

# Node level

The node-level limit the resource consumption speed of the current node. If you want to limit the number of messages flowing in the current node per second to no more than 100, you can add the following configuration to emqx.conf:

limiter.message_in.rate = "100/s"
1

Note: Only listeners with rate limiting configured will be affected by node-level settings

All options supported at this level are as follows:

OptionsTypeDefaultDescription
rateintinfinitytoken generation rate
burstint0When overloaded (the consumption rate of tokens is greater than the generation rate), burst will be used as an additional generation rate to supplement tokens