Skip to content

Commit 5c05a83

Browse files
authored
Merge pull request #3144 from emqx/clent-tag-ratelimit
feat: clent tag ratelimit
2 parents 0227bed + 19856af commit 5c05a83

File tree

5 files changed

+285
-30
lines changed

5 files changed

+285
-30
lines changed

directory_ee.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,10 @@
275275
{
276276
"title": "用户名配额限制",
277277
"path": "modules/username_quota"
278+
},
279+
{
280+
"title": "客户端标签管理",
281+
"path": "modules/client_tag"
278282
}
279283
]
280284
},
@@ -1517,4 +1521,4 @@
15171521
]
15181522
}
15191523
]
1520-
}
1524+
}

en_US/advanced/rate-limit.md

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,56 @@
1-
# Rate limit
1+
# Rate Limit
22
EMQX Broker specifies the limit on access speed and message speed. When the client's connection request speed exceeds the specified limit, the establishment of a new connection is suspended; when the message reception speed exceeds the specified limit, the reception of messages is suspended.
33

44
Rate limit is a *backpressure* scheme that avoids system overload from the entrance and guarantees system stability and predictable throughput. The rate limit can be configured in `etc/emqx.conf` :
55

6-
| Configuration item | Type | Default value | Description |
7-
| ----------------------------------- | --------------- | ------------- | ------------------------------------------------------------ |
8-
| listener.tcp.external.max_conn_rate | Number | 1000 | The maximum allowable connection rate on this node (conn/s) |
9-
| zone.external.rate_limit.conn_messages_in | Number,Duration | No limit | Maximum allowable publish rate on a single connection (msg/s) |
10-
| zone.external.rate_limit.conn_bytes_in | Size,Duration | No limit | Maximum allowable packet rate on a single connection (bytes/s) |
6+
| Configuration Item | Type | Default Value | Description |
7+
| ----------------------------------------- | ---------------- | ------------- | ------------------------------------------------------------ |
8+
| listener.tcp.external.max_conn_rate | Number | 1000 | The maximum allowable connection rate on this node (conn/s) |
9+
| zone.external.rate_limit.conn_messages_in | Number, Duration | No limit | Maximum allowable publish rate on a single connection (msg/s) |
10+
| zone.external.rate_limit.conn_bytes_in | Size, Duration | No limit | Maximum allowable packet rate on a single connection (bytes/s) |
1111

12-
- **max_conn_rate** is the rate limit for connection establishment on a single emqx node. `1000` means that 1000 clients can access at most.
12+
- **max_conn_rate** is the rate limit for connection establishment on a single EMQX node. `1000` means that 1000 clients can access at most.
1313
- **conn_messages_in** is the rate limit for receiving PUBLISH packets on a single connection. `100,10s` means that the maximum PUBLISH message rate allowed on each connection is 100 every 10 seconds.
1414
- **conn_bytes_in** is the rate limit for receiving TCP packets on a single connection. `100KB,10s` means that the maximum TCP packet rate allowed on each connection is 100KB every 10 seconds.
1515

1616
`conn_messages_in` and `conn_bytes_in` both provide limits for a single connection. EMQX Broker currently does not provide a global message rate limit.
1717

18-
## Rate limit explanation
18+
## Rate Limit Explanation
1919
EMQX Broker uses the [Token Bucket](https://en.wikipedia.org/wiki/Token_bucket) algorithm to control all Rate Limits. The logic of the token bucket algorithm is as follows:
2020

2121
![image-20190604103907875](../assets/token-bucket.jpg)
2222

23-
- There is a bucket that can hold the maximum burst of the token. The maximum burst is abbreviated as b.
24-
- There is a rate for adding tokens to the bucket per second, abbreviated as r. When the bucket is full, no tokens are added to the bucket.
25-
- Whenever 1 (or N) request arrives, take 1 (or N) token from the bucket. If the token is not enough, it will be blocked and wait for the token to be generated.
23+
- A *bucket* holds tokens, with a maximum capacity of $burst$ tokens, abbreviated as $b$.
24+
- Tokens are added to the bucket at a constant rate $rate$, abbreviated as $r$. When the bucket is full, no additional tokens are added.
25+
- When a request (or N requests) arrives, it must consume 1 (or N) tokens from the bucket. If there are not enough tokens, the request is blocked until more tokens are generated.
2626

27-
It can be seen from this algorithm:
27+
In this algorithm:
2828

29-
- In the long run, the average value of the limited request rate is equal to the value of rate.
29+
1. When a large number of requests arrive and the bucket is full:
3030

31-
- When the actual request reaching speed is M, and M> r, then the maximum (peak) rate that can be achieved in actual operation is M = b + r.
31+
The maximum number of tokens that can be consumed per unit time is $b + r/1$, i.e., all the tokens in the bucket plus those generated during the unit time.
3232

33-
It is easy to think that the maximum rate M is the speed that can consume the full state token bucket in 1 unit of time. The consumption rate of token bucket is M-r, so we can see that: b / (M-r) = 1, and we get M = b + r
34-
35-
33+
This is the maximum achievable rate: $M = b + r/1$.
3634

37-
### Application of Token Bucket Algorithm in EMQX Broker
35+
2. After all tokens in the bucket are consumed (i.e., the bucket is empty):
36+
37+
The number of tokens available per unit time is $0 + r/1$, meaning only newly generated tokens can be consumed.
38+
39+
This represents the long-term average rate: $r$.
40+
41+
### Application of Token Bucket Algorithm in EMQX
3842
When the following configuration is used for packet rate limiting:
3943

4044
```
4145
zone.external.rate_limit.conn_bytes_in = 100KB,10s
4246
```
4347

44-
EMQX Broker will initialize the rate-limit processor of each connection with two values:
48+
EMQX will initialize the rate-limit processor of each connection with two values:
4549

4650
- rate = 100 KB / 10s = 10240 B/s
4751
- burst = 100 KB = 102400 B
4852

49-
According to the algorithm in [Message Rate Limitation Explanation](#rate-limit-explanation), it is known:
53+
According to the algorithm in [Rate Limit Explanation](#rate-limit-explanation), it is known:
5054

5155
- In the long run, the allowable average rate is limited to 10240 B/s
5256
- The allowable peak rate is 102400 + 10240 = 112640 B/s

en_US/modules/client_tag.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# Client Tags and Group-Based Rate Limiting
2+
3+
Starting from EMQX version 4.4.33, EMQX introduces a client tagging feature. This feature allows you to assign tags to clients based on business logic and apply differentiated rate-limiting policies to clients under different tags.
4+
5+
This module relies heavily on [HTTP Authentication/ACL](./http_authentication.md), using the data returned from the authentication interface to assign tags to clients. This document details the working mechanism, configuration, and management of client tags.
6+
7+
## How It Works
8+
9+
The core principle of client tagging is “group first, then rate limit.” EMQX automatically assigns clients to different groups based on the `tag` returned by the HTTP authentication service, and then applies independent rate-limiting policies to each group.
10+
11+
The full workflow is as follows:
12+
13+
1. **Create Tags**: Tags are pre-created in the EMQX Dashboard, each with its own rate-limiting strategy. A built-in `default` tag exists and cannot be deleted. It is used for clients that do not match any tag.
14+
2. **Client Connection**: A client initiates a connection request.
15+
3. **HTTP Authentication**: EMQX sends client information to the external HTTP authentication service.
16+
4. **Tag Assignment**: The authentication service includes tag information in its response based on custom business logic.
17+
5. **Apply Policy**: EMQX parses the tag from the response, associates the client with the corresponding tag, and enforces the rate-limiting policy configured for that tag.
18+
19+
### Tag Assignment Logic
20+
21+
- If HTTP authentication is successful (status code `200`) and the `client_attrs.tag` in the response matches a pre-created tag, the client is assigned to that tag.
22+
- If the `tag` field is missing or the value does not match any existing tag, the client is automatically assigned to the `default` tag.
23+
- If HTTP authentication fails (non-`200` status code), the client connection is denied, and no tag is assigned.
24+
25+
::: tip Note
26+
27+
You must create tags in the Dashboard first; only then will tags returned by the HTTP service take effect. The rate-limiting policy for the `default` tag can also be customized to control default client behavior.
28+
29+
If the HTTP Authentication module is not enabled, all clients are automatically assigned to the `default` tag.
30+
31+
:::
32+
33+
## HTTP Authentication Interface Specification
34+
35+
To enable EMQX to recognize client tags, your HTTP authentication service must comply with the following interface specification.
36+
37+
When authentication is successful, the HTTP service should return a 200 status code and a response body containing a JSON object. The `client_attrs.tag` field in the object specifies the client’s tag.
38+
39+
**Example Response Body:**
40+
41+
```json
42+
{
43+
"result": "allow",
44+
"client_attrs": {
45+
"tag": "group_a"
46+
}
47+
}
48+
```
49+
50+
**Field Descriptions:**
51+
52+
- `result`: `string` type. Currently reserved for EMQX 5.0 compatibility and does not affect authentication results. EMQX determines authentication success based solely on HTTP status code.
53+
- `client_attrs`: `object` type. Stores additional client attributes.
54+
- `tag`: `string` type. Specifies the client’s tag name. EMQX uses this value to group the client.
55+
56+
## Rate Limiting Policies and Handling
57+
58+
EMQX provides fine-grained rate-limiting strategies per tag, allowing you to control resource usage per client group.
59+
60+
### Configurable Limits
61+
62+
| Limit Type | Unit | Description |
63+
| --------------------------------- | ------------ | ------------------------------------------------------------ |
64+
| Sent Message TPS Limit | Messages/sec | Limits the number of messages a client can publish per second (TPS). |
65+
| Subscribe Message TPS Limit | Messages/sec | Limits the number of messages EMQX can deliver to a client per second. |
66+
| Sent Traffic Limit (bytes/s) | Bytes/sec | Limits the message traffic a client can publish per second. |
67+
| Subscribe Traffic Limit (bytes/s) | Bytes/sec | Limits the message traffic a client can deliver per second. |
68+
| Maximum QoS Level | 0, 1, 2 | Limits the maximum QoS level a client is allowed to publish with. |
69+
70+
::: tip
71+
72+
If a limit value is empty or set to `0`, that limit is **not applied**.
73+
74+
:::
75+
76+
### Enforcement Mechanism
77+
78+
When a client exceeds the rate limits configured for its assigned tag, EMQX handles it differently based on the type of limit:
79+
80+
- **For publishing clients**:
81+
- **QoS Limit**: If a client tries to publish with a QoS level higher than allowed, the message is dropped.
82+
- **Rate/Byte Limit**: If the client exceeds the message or byte rate limit, EMQX applies backpressure, temporarily blocking data reads from the client’s socket to slow down publishing. This behaves similarly to [EMQX’s built-in rate limiting](../advanced/rate-limit.md) but does not disconnect the client.
83+
- **For subscribing clients**:
84+
- **Rate/Byte Limit**: If the message delivery rate exceeds the configured limit, subsequent messages are dropped to prevent the client from being overwhelmed.
85+
86+
::: tip Note
87+
88+
For performance reasons, EMQX does not enqueue messages that exceed rate limits. They are dropped immediately.
89+
See: [Inflight Window and Message Queue](../advanced/inflight-window-and-message-queue.md).
90+
91+
Due to batch processing of message delivery, if the delivery rate or byte limit is set too low, rate limiting may be triggered frequently, causing the actual delivery rate to fall below the configured limit.
92+
93+
:::
94+
95+
## Manage Client Tags via Dashboard
96+
97+
You can manage client tags and their rate-limiting policies conveniently through the EMQX Dashboard.
98+
99+
1. Open the EMQX Dashboard in your browser.
100+
2. In the left menu, click **Modules**.
101+
3. Click **Add Module** on the page, then select and add **Client Tag Management** from the list.
102+
4. Once added, a **Client Tag Management** module will appear in the module list. Click **Manage** to enter the Client Tag Management page.
103+
104+
### Client Tags
105+
106+
Under the **Client Tags** tab, all created tags are displayed in a list, including the built-in `default` tag. You can:
107+
108+
- **View**: See tag names and the number of currently associated clients.
109+
- **Create**: Click **Add** to add new tags and configure rate-limiting policies. For detailed configuration descriptions, see [Configurable Limits](#configurable-limits).
110+
- **Manage**: Edit rate-limiting strategies or view the client list under a tag by clicking the **View Client List** button.
111+
- **Delete**: Delete a tag that is no longer needed.
112+
113+
### Client Search
114+
115+
Under the **Client Search** tab, you can view all online clients assigned to different tags. The list supports pagination, and you can click a client ID to view detailed information for troubleshooting and monitoring. You can quickly locate clients or tags by client ID or tag name.
116+
117+
## Monitoring and Logs
118+
119+
To assist with monitoring and debugging group-based rate limiting, EMQX provides relevant metrics and logging support:
120+
121+
- **Prometheus Metrics**: EMQX exposes metrics for messages dropped due to rate limiting, which can be integrated with monitoring and alerting systems.
122+
- **Logs**: When rate limiting is triggered, EMQX logs the event. The logging system supports **sampling** to avoid performance degradation during traffic spikes.

zh_CN/advanced/rate-limit.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ keywords: rate-limit
88
# 描述
99
description:
1010
# 分类
11-
category:
11+
category:
1212
# 引用
1313
ref:
1414
---
@@ -35,17 +35,23 @@ EMQX 使⽤[令牌桶 (Token Bucket)](https://en.wikipedia.org/wiki/Token_bucket
3535

3636
![image-20190604103907875](../assets/token-bucket.jpg)
3737

38-
- 存在一个可容纳令牌(Token) 的最大值 burst 的桶(Bucket),最大值 burst 简记为 b
39-
- 存在一个 rate 为每秒向桶添加令牌的速率,简记为 r 。当桶满时则不不再向桶中加⼊入令牌
40-
- 每当有 1 个(或 N 个)请求抵达时,则从桶中拿出 1 个 (或 N 个) 令牌。如果令牌不不够则阻塞,等待令牌的⽣生成
38+
- 存在一个可容纳令牌 (Token) 的桶 (Bucket) ,可容纳令牌数量的最大值为 $burst$ ,简记为 $b$
39+
- 存在一个每秒向桶添加令牌的速率 $rate$,简记为 $r$ 。当桶满时则不再向桶中加入令牌
40+
- 每当有 1 个 (或 N 个) 请求抵达时,则从桶中拿出 1 个 (或 N 个) 令牌。如果令牌不够则阻塞,等待令牌的生成
4141

42-
由此可知该算法中:
42+
该算法中:
4343

44-
- 长期来看,所限制的请求速率的平均值等于 rate 的值
44+
1. 在大量请求到达时,假设当前 Bucket 已满
4545

46-
- 记实际请求达到速度为 M,且 M > r,那么,实际运⾏中能达到的最大(峰值)速率为 M = b + r,证明:
46+
那么单位时间内可被请求消耗的 Token 数量最多为 $b + r/1$ 。即桶中的所有 Token 和单位时间内生成的所有 Token。
4747

48-
容易想到,最大速率 M 为:能在1个单位时间内消耗完满状态令牌桶的速度。而桶中令牌的消耗速度为 M - r,故可知:b / (M - r) = 1,得 M = b + r
48+
该速率即为实际能够达到的最大速率。记为 $M = b + r/1$。
49+
50+
2. 在桶中 Token 被完全消耗后(Bucket 为空之后)
51+
52+
单位时间内可被请求消耗的 Token 数量最多为 $0 + r/1$。即该单位时间内生成的所有 Token。
53+
54+
该速率即为长期来看所能达到的平均速率 $r$
4955

5056
### 令牌桶算法在 EMQX 中的应用
5157

@@ -60,7 +66,7 @@ EMQX 将使用两个值初始化每个连接的 rate-limit 处理器:
6066
- rate = 100 KB / 10s = 10240 B/s
6167
- burst = 100 KB = 102400 B
6268

63-
根据 [消息速率限制原理](#rate-limit-explanation) 中的算法,可知:
69+
根据[消息速率限制原理](#速率限制原理)中的算法,可知:
6470

6571
- 长期来看允许的平均速率限制为 10240 B/s
6672
- 允许的峰值速率为 102400 + 10240 = 112640 B/s

0 commit comments

Comments
 (0)