## Module 5: Scale in/out or scale up/down a TiDB Dedicated Cluster

+ In this module, we show you that you can scale in/out or scale up/down the TiDB Dedicated cluster easily.
+ We also demonstrate that when TiProxy is enabled, your application will NOT experience connection downtime during scaling in, even when your application has no reconnection logic.

### Step 1. Check the `Modify` panel

+ Click `...` on the upper-right corner, then click `Modify`.

  ![80%](https://lab-static.pingcap.com/images/2025/7/24/42a6e12c6e4d5bbb12d85281d2cc6633b1a108b7.png)

+ Check the options for scale in/out or scale up/down a TiDB Dedicated Cluster. Do NOT scale-in your cluster now.

### Step 2. Prepare the schema

+ Go to [Terminal 1](ssh://monitor1-ssh#1), and import some sample data.

  ```sql
  USE test
  CREATE TABLE `users` (
    `id` int,
    `name` varchar(100),
    `balance` int
  );
  INSERT INTO users VALUES (1, 'Alice', 10);
  INSERT INTO users VALUES (2, 'Bob', 20);
  ```

  ```sql
  exit
  ```

### Step 3. Read the code

+ In `demo_tiproxy_no_reconn.py`, 5 connections are initialized. Theoretically, it should be balanced on 3 TiDB Server nodes by TiProxy.
+ The code itself has no logic to handle connection drops, which may happen when scaling in without TiProxy.
+ Since TiProxy is used here, you will not see any error messages printed out when running the program.

### Step 4. Update the connection information

+ In the `demo_tiproxy_no_reconn.py`, update the connection information

  ```output
  conn = connect(
    database="test",
    host="<host_address>",
    port=4000,
    user="root",
    password="<your password>",
    ssl_ca="/home/ubuntu/ca.pem",
    ssl_verify_cert=True,
    ssl_verify_identity=True,
  )
  ```

### Step 5. Install the packages

  ```bash
  sudo apt-get update
  sudo apt install python3.12-venv -y
  python3 -m venv myenv
  source myenv/bin/activate
  pip install --upgrade mysql-connector-python
  ```

  ```output
  ...
  Downloading mysql_connector_python-9.4.0-cp312-cp312-manylinux_2_28_x86_64.whl (33.9 MB)
    ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 33.9/33.9 MB 69.8 MB/s eta 0:00:00
  Installing collected packages: mysql-connector-python
  Successfully installed mysql-connector-python-9.4.0
  ```

### Step 6. Run `demo_tiproxy_no_reconn.py`

  ```bash
  python3 demo_tiproxy_no_reconn.py
  ```

  ```output
  ...
  Thread 1: Committed. Alice balance updated to: 4
  Thread 4: Committed. Alice balance updated to: 5
  Thread 2: Committed. Alice balance updated to: 9
  Thread 5: Committed. Alice balance updated to: 6
  Thread 3: Committed. Alice balance updated to: 9
  Thread 1: Committed. Alice balance updated to: 10
  Thread 4: Committed. Alice balance updated to: 1
  Thread 2: Committed. Alice balance updated to: 3
  ...
  ```

### Step 7. Scale in one TiDB Server

+ Go to the TiDB Cloud Web UI, and scale in the TiDB Server

  ![80%](https://lab-static.pingcap.com/images/2025/7/24/f655b919085c9ed2e3d754c04136cc512cb6d257.png)

+ The scale in may take more than 5 minutes to finish.
+ When scaling in, please observe the terminal output. You should not see any error messages printed out.
+ Please refer to this lab for more details: [Database Scaling In and Scaling Out](/labs/demo_009).

### Step 8. Stop the workload

+ In the [Terminal 1](ssh://monitor1-ssh#1), using `ctrl` + `c`.

### Step 9. Delete `TiDB Cloud Dedicated` cluster

+ Go to `Overview`, click `...`, and then delete your cluster.

  ![80%](https://lab-static.pingcap.com/images/2025/7/24/a319028d778d0e722f741644e79f7e9d655ad37b.png)
