|
| 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. |
0 commit comments