Skip to content

Commit 5f8cb87

Browse files
authored
tiproxy: add docs for label-based balance (pingcap#19288)
1 parent 8295738 commit 5f8cb87

File tree

4 files changed

+86
-14
lines changed

4 files changed

+86
-14
lines changed
84.3 KB
Loading

tiproxy/tiproxy-configuration.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,13 @@ Configurations for HTTP gateway.
112112

113113
Configurations for the load balancing policy of TiProxy.
114114

115+
#### `label-name`
116+
117+
+ Default value: `""`
118+
+ Support hot-reload: yes
119+
+ Specifies the label name used for [label-based load balancing](/tiproxy/tiproxy-load-balance.md#label-based-load-balancing). TiProxy matches the label values of TiDB servers based on this label name and prioritizes routing requests to TiDB servers with the same label value as itself.
120+
+ The default value of `label-name` is an empty string, indicating that label-based load balancing is not used. To enable this load balancing policy, you need to set this configuration item to a non-empty string and configure both [`labels`](#labels) in TiProxy and [`labels`](/tidb-configuration-file.md#labels) in TiDB. For more information, see [Label-based load balancing](/tiproxy/tiproxy-load-balance.md#label-based-load-balancing).
121+
115122
#### `policy`
116123

117124
+ Default value: `resource`

tiproxy/tiproxy-grafana.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ TiProxy has four panel groups. The metrics on these panels indicate the current
1616
- **TiProxy-Server**: instance information.
1717
- **TiProxy-Query-Summary**: SQL query metrics like CPS.
1818
- **TiProxy-Backend**: information on TiDB nodes that TiProxy might connect to.
19-
- **TiProxy-Balance**: loadbalance mertrics.
19+
- **TiProxy-Balance**: load balancing metrics.
2020

2121
## Server
2222

@@ -59,6 +59,7 @@ TiProxy has four panel groups. The metrics on these panels indicate the current
5959
- Session Migration Duration: average, P95, P99 session migration duration.
6060
- Session Migration Reasons: the number of session migrations that happened every minute and the reason for them. The reasons include the following:
6161
- `status`: TiProxy performed [status-based load balancing](/tiproxy/tiproxy-load-balance.md#status-based-load-balancing).
62+
- `label`: TiProxy performed [label-based load balancing](/tiproxy/tiproxy-load-balance.md#label-based-load-balancing).
6263
- `health`: TiProxy performed [health-based load balancing](/tiproxy/tiproxy-load-balance.md#health-based-load-balancing).
6364
- `memory`: TiProxy performed [memory-based load balancing](/tiproxy/tiproxy-load-balance.md#memory-based-load-balancing).
6465
- `cpu`: TiProxy performed [CPU-based load balancing](/tiproxy/tiproxy-load-balance.md#cpu-based-load-balancing).

tiproxy/tiproxy-load-balance.md

Lines changed: 77 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,86 @@ summary: Introduce the load balancing policies in TiProxy and their applicable s
55

66
# TiProxy Load Balancing Policies
77

8-
TiProxy v1.0.0 only supports status-based and connection count-based load balancing policies for TiDB servers. Starting from v1.1.0, TiProxy introduces four additional load balancing policies that can be configured independently: health-based, memory-based, CPU-based, and location-based.
8+
TiProxy v1.0.0 only supports status-based and connection count-based load balancing policies for TiDB servers. Starting from v1.1.0, TiProxy introduces five additional load balancing policies: label-based, health-based, memory-based, CPU-based, and location-based.
99

10-
By default, TiProxy enables all policies with the following priorities:
10+
By default, TiProxy applies these policies with the following priorities:
1111

1212
1. Status-based load balancing: when a TiDB server is shutting down, TiProxy migrates connections from that TiDB server to an online TiDB server.
13-
2. Health-based load balancing: when the health of a TiDB server is abnormal, TiProxy migrates connections from that TiDB server to a healthy TiDB server.
14-
3. Memory-based load balancing: when a TiDB server is at risk of running out of memory (OOM), TiProxy migrates connections from that TiDB server to a TiDB server with lower memory usage.
15-
4. CPU-based load balancing: when the CPU usage of a TiDB server is much higher than that of other TiDB servers, TiProxy migrates connections from that TiDB server to a TiDB server with lower CPU usage.
16-
5. Location-based load balancing: TiProxy prioritizes routing requests to the TiDB server geographically closest to TiProxy.
17-
6. Connection count-based load balancing: when the connection count of a TiDB server is much higher than that of other TiDB servers, TiProxy migrates connections from that TiDB server to a TiDB server with fewer connections.
13+
2. Label-based load balancing: TiProxy prioritizes routing requests to TiDB servers that share the same label as the TiProxy instance, enabling resource isolation at the computing layer.
14+
3. Health-based load balancing: when the health of a TiDB server is abnormal, TiProxy migrates connections from that TiDB server to a healthy TiDB server.
15+
4. Memory-based load balancing: when a TiDB server is at risk of running out of memory (OOM), TiProxy migrates connections from that TiDB server to a TiDB server with lower memory usage.
16+
5. CPU-based load balancing: when the CPU usage of a TiDB server is much higher than that of other TiDB servers, TiProxy migrates connections from that TiDB server to a TiDB server with lower CPU usage.
17+
6. Location-based load balancing: TiProxy prioritizes routing requests to the TiDB server geographically closest to TiProxy.
18+
7. Connection count-based load balancing: when the connection count of a TiDB server is much higher than that of other TiDB servers, TiProxy migrates connections from that TiDB server to a TiDB server with fewer connections.
1819

1920
To adjust the priorities of load balancing policies, see [Configure load balancing policies](#configure-load-balancing-policies).
2021

2122
## Status-based load balancing
2223

2324
TiProxy periodically checks whether a TiDB server is offline or shutting down using the SQL port and status port.
2425

26+
## Label-based load balancing
27+
28+
Label-based load balancing prioritizes routing connections to TiDB servers that share the same label as TiProxy, enabling resource isolation at the computing layer. This policy is disabled by default and should only be enabled when your workload requires computing resource isolation.
29+
30+
To enable label-based load balancing, you need to:
31+
32+
- Specify the matching label name through [`balance.label-name`](/tiproxy/tiproxy-configuration.md#label-name).
33+
- Configure the [`labels`](/tiproxy/tiproxy-configuration.md#labels) configuration item in TiProxy.
34+
- Configure the [`labels`](/tidb-configuration-file.md#labels) configuration item in TiDB servers.
35+
36+
After configuration, TiProxy uses the label name specified in `balance.label-name` to route connections to TiDB servers with matching label values.
37+
38+
Consider an application that handles both transaction and BI workloads. To prevent these workloads from interfering with each other, configure your cluster as follows:
39+
40+
1. Set [`balance.label-name`](/tiproxy/tiproxy-configuration.md#label-name) to `"app"` in TiProxy, indicating that TiDB servers will be matched by the label name `"app"`, and connections will be routed to TiDB servers with matching label values.
41+
2. Configure two TiProxy instances, adding `"app"="Order"` and `"app"="BI"` to their respective [`labels`](/tiproxy/tiproxy-configuration.md#labels) configuration items.
42+
3. Divide TiDB instances into two groups, adding `"app"="Order"` and `"app"="BI"` to their respective [`labels`](/tidb-configuration-file.md#labels) configuration items.
43+
4. Optional: For storage layer isolation, configure [Placement Rules](/configure-placement-rules.md) or [Resource Control](/tidb-resource-control.md).
44+
5. Direct transaction and BI clients to connect to their respective TiProxy instance addresses.
45+
46+
<img src="https://download.pingcap.com/images/docs/tiproxy/tiproxy-balance-label.png" alt="Label-based Load Balancing" width="600" />
47+
48+
Example configuration for this topology:
49+
50+
```yaml
51+
component_versions:
52+
tiproxy: "v1.1.0"
53+
server_configs:
54+
tiproxy:
55+
balance.label-name: "app"
56+
tidb:
57+
graceful-wait-before-shutdown: 15
58+
tiproxy_servers:
59+
- host: tiproxy-host-1
60+
config:
61+
labels: {app: "Order"}
62+
- host: tiproxy-host-2
63+
config:
64+
labels: {app: "BI"}
65+
tidb_servers:
66+
- host: tidb-host-1
67+
config:
68+
labels: {app: "Order"}
69+
- host: tidb-host-2
70+
config:
71+
labels: {app: "Order"}
72+
- host: tidb-host-3
73+
config:
74+
labels: {app: "BI"}
75+
- host: tidb-host-4
76+
config:
77+
labels: {app: "BI"}
78+
tikv_servers:
79+
- host: tikv-host-1
80+
- host: tikv-host-2
81+
- host: tikv-host-3
82+
pd_servers:
83+
- host: pd-host-1
84+
- host: pd-host-2
85+
- host: pd-host-3
86+
```
87+
2588
## Health-based load balancing
2689
2790
TiProxy determines the health of a TiDB server by querying its error count. When the health of a TiDB server is abnormal while others are normal, TiProxy migrates connections from that server to a healthy TiDB server, achieving automatic failover.
@@ -99,11 +162,12 @@ tidb_servers:
99162
zone: west
100163
tikv_servers:
101164
- host: tikv-host-1
102-
port: 20160
103165
- host: tikv-host-2
104-
port: 20160
105166
- host: tikv-host-3
106-
port: 20160
167+
pd_servers:
168+
- host: pd-host-1
169+
- host: pd-host-2
170+
- host: pd-host-3
107171
```
108172

109173
In the preceding configuration, the TiProxy instance on `tiproxy-host-1` prioritizes routing requests to the TiDB server on `tidb-host-1` because `tiproxy-host-1` has the same `zone` configuration as `tidb-host-1`. Similarly, the TiProxy instance on `tiproxy-host-2` prioritizes routing requests to the TiDB server on `tidb-host-2`.
@@ -121,9 +185,9 @@ Typically, TiProxy identifies the load on TiDB servers based on CPU usage. This
121185

122186
TiProxy lets you configure the combination and priority of load balancing policies through the [`policy`](/tiproxy/tiproxy-configuration.md#policy) configuration item.
123187

124-
- `resource`: the resource priority policy performs load balancing based on the following priority order: status, health, memory, CPU, location, and connection count.
125-
- `location`: the location priority policy performs load balancing based on the following priority order: status, location, health, memory, CPU, and connection count.
126-
- `connection`: the minimum connection count policy performs load balancing based on the following priority order: status and connection count.
188+
- `resource`: the resource priority policy performs load balancing based on the following priority order: status, label, health, memory, CPU, location, and connection count.
189+
- `location`: the location priority policy performs load balancing based on the following priority order: status, label, location, health, memory, CPU, and connection count.
190+
- `connection`: the minimum connection count policy performs load balancing based on the following priority order: status, label, and connection count.
127191

128192
## More resources
129193

0 commit comments

Comments
 (0)