## 模块 3：TiDB 通过 TiProxy 缩容时的应用最佳实践

+ 本模块将演示当应用通过 TiProxy 连接数据库时，TiDB 缩容对应用的影响。

### 步骤 1. 验证 TiProxy 配置

+ [graceful-wait-before-shutdown](https://docs.pingcap.com/zh/tidb/stable/tidb-configuration-file/#graceful-wait-before-shutdown-new-in-v50) 用于指定当关闭 TiDB Server（关闭 TCP 端口）之前，TiDB 等待的秒数，以便客户端能够正常断开连接。

  ```bash
  ./connect-tiproxy.sh
  ```

  ```output
  tidb:tiproxy>
  ```

  ```sql
  show config where name='graceful-wait-before-shutdown';
  exit;
  ```

  ```output
  +------+------------------+-------------------------------+-------+
  | Type | Instance         | Name                          | Value |
  +------+------------------+-------------------------------+-------+
  | tidb | 10.90.1.247:4000 | graceful-wait-before-shutdown | 15    |
  +------+------------------+-------------------------------+-------+
  2 rows in set (0.01 sec)
  Bye
  ```

> [!TIP]
> [graceful-wait-before-shutdown](https://docs.pingcap.com/zh/tidb/stable/tidb-configuration-file/#graceful-wait-before-shutdown-new-in-v50) 的值应比应用中最长事务的持续时间至少大 10 秒，以避免 TiDB 下线时客户端连接被中断。
> 此外，建议在缩容 TiDB 时（尤其是通过负载均衡器连接时）避免运行长事务（如批量处理）。

### 步骤 2. 再次扩容一台 TiDB Server

+ 现在我们再次扩容一台 TiDB Server，后续将进行缩容操作。

  ```bash
  ~/.tiup/bin/tiup cluster scale-out tidb-demo scale-out-tidb-2.yaml \
  --user ec2-user \
  -i ~/.ssh/pe-class-key-${LAB:REGION_NAME}.pem \
  -y
  ```

  ```output
  ...
  Scaled cluster `tidb-demo` in successfully
  ```

下图展示了当前 TiDB 集群的拓扑结构。后续将缩容红色虚线框内的 TiDB Server。

![demo016-m7-tapo-tiproxy.png 85%](https://lab-static.pingcap.com/images/2025/8/15/057327baab2c6324aba3f837ef2c50408670b920.png)

### 步骤 3. 查看集群拓扑

+ 确认现在有两台 `TiDB` 和一台 `TiProxy`。

  ```bash
  ~/.tiup/bin/tiup cluster display tidb-demo
  ```

  ```output
  Cluster type:       tidb
  Cluster name:       tidb-demo
  Cluster version:    v8.5.1
  Deploy user:        ec2-user
  SSH type:           builtin
  Dashboard URL:      http://3.112.81.1:2379/dashboard
  Grafana URL:        http://10.90.4.40:3000
  ID                 Role          Host         Ports        OS/Arch       Status  Data Dir                      Deploy Dir
  --                 ----          ----         -----        -------       ------  --------                      ----------
  10.90.4.40:9093    alertmanager  10.90.4.40   9093/9094    linux/x86_64  Up      /tidb-data/alertmanager-9093  /tidb-deploy/alertmanager-9093
  10.90.4.40:3000    grafana       10.90.4.40   3000         linux/x86_64  Up      -                             /tidb-deploy/grafana-3000
  10.90.2.141:2379   pd            10.90.2.141  2379/2380    linux/x86_64  Up|L    /tidb-data/pd-2379            /tidb-deploy/pd-2379
  10.90.4.40:9090    prometheus    10.90.4.40   9090/12020   linux/x86_64  Up      /tidb-data/prometheus-9090    /tidb-deploy/prometheus-9090
  10.90.1.165:4000   tidb          10.90.1.165  4000/10080   linux/x86_64  Up      -                             /tidb-deploy/tidb-4000
  10.90.1.66:4000    tidb          10.90.1.66   4000/10080   linux/x86_64  Up      -                             /tidb-deploy/tidb-4000
  10.90.3.160:20160  tikv          10.90.3.160  20160/20180  linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
  10.90.3.193:20160  tikv          10.90.3.193  20160/20180  linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
  10.90.3.243:20160  tikv          10.90.3.243  20160/20180  linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
  10.90.4.40:6000    tiproxy       10.90.4.40   6000/3080    linux/x86_64  Up      -                             /tidb-deploy/tiproxy-6000
  ```

### 步骤 4. 运行无重连逻辑的 TiProxy 代码

+ 打开 [终端 1](ssh://monitor1-ssh#1)，运行 `demo_tiproxy_no_retry.py`。
+ 该代码与模块 2 步骤 4 相同，只是连接对象变为 TiProxy，而不是网络负载均衡器。
+ 代码中没有任何重试或重连逻辑。

  ```bash
  python3 demo_tiproxy_no_retry.py
  ```

  ```output
  ...
  Thread 3: Committed. user 4 balance updated to: 2
  Thread 2: Committed. user 3 balance updated to: 0
  ...
  ```

### 步骤 5. 缩容一台 TiDB Server

+ 打开 [终端 2](ssh://monitor1-ssh#2)，执行以下命令：

  ```bash
  ~/.tiup/bin/tiup cluster scale-in tidb-demo --node ${LAB:HOST_DB2_PRIVATE_IP}:4000 -y
  ```

  ```output
  ...
  Scaled cluster `tidb-demo` in successfully
  ```

### 步骤 6. 观察 [终端 1](ssh://monitor1-ssh#3) 的输出

+ 在 [终端 1](ssh://monitor1-ssh#3) 可以看到如下输出：

  ```output
  ...
  Thread 4: Committed. user 5 balance updated to: 1
  Thread 2: Committed. user 1 balance updated to: 5
  ...
  ```

### 步骤 7. 停止压力测试

+ 在 [终端 1](ssh://monitor1-ssh#1) 使用 `ctrl` + `c` 停止压力测试。

### 总结

+ TiProxy 降低了 TiDB 的主动维护操作带来的影响。
