Sharding Made Easy with MginDB in 5 Minutes
Tags: sharding, database, python, tutorial
Published: 2024-05-21 14:23:20

Hello, developers!

Sharding can be a complex task, but with MginDB, it's a breeze. Today, I'll show you how to set up sharding with MginDB in just a few minutes. Whether you're scaling up or down, MginDB handles the heavy lifting for you and manages it automatically.

Let’s get started!

#### Install MginDB

Install MginDB on your master server and on all your shards:
[mgindb.com/documentation.html](https://mgindb.com/documentation.html)

#### Sharding

Sharding is a database architecture pattern that partitions data across multiple servers, enhancing the system's scalability and performance. MginDB will take care of everything automatically when you add or remove a shard and will redistribute the data without having to transfer any data manually nor changing the sharding scheme and keys.

**Setting Up Sharding:**

1. **Configure Sharding Type:**
   Open `config.json` for all your servers and set `SHARDING_TYPE` to `MASTER` for your main server and `SHARDING_TYPE` to `SLAVE` for all your shard servers.
   Or use the CLI command:
   ```bash
   CONFIG SET SHARDING_TYPE MASTER
   CONFIG SET SHARDING_TYPE SLAVE
   ```

2. **Adding a Shard:**
   To add a new server to the shard list, use the following command:
   ```bash
   CONFIG SET SHARDS ADD serverip/domain
   ```
   This command registers `serverip/domain` as a new shard where data can be distributed.

3. **Removing a Shard:**
   To remove an existing server from the shard list, use the command:
   ```bash
   CONFIG SET SHARDS DEL serverip/domain
   ```
   This removes `serverip/domain` from the list of shards, redistributing any data it held to remaining shards.

4. **Activating Sharding:**
   To activate sharding and start distributing data across the configured shards, execute:
   ```bash
   CONFIG SET SHARDING 1
   ```
   Ensure you add the necessary shards before activating sharding to distribute data evenly.

5. **Deactivating Sharding:**
   To deactivate sharding and consolidate all data back to the master server, use:
   ```bash
   CONFIG SET SHARDING 0
   ```
   This command centralizes the data, ceasing its distribution among shards.

**Additional Sharding Commands:**

- **Backup and Rollback:** Data is always backed up before resharding. If an error occurs, it rolls back to the last backup, ensuring data integrity.
- **Sharding Batch Size:** Set the size of commands sent to shards per batch (default: 100):
  ```bash
  CONFIG SET SHARDING_BATCH_SIZE value
  ```

Each of these commands needs to be executed on each server where MginDB is installed, either using the Web CLI (`client.html`) or Server CLI (`mgindb client`).

These settings play a crucial role in managing the distribution of data and the operational scale of your system, allowing for flexibility in handling workload and data management strategies.

Feel free to reach out with any questions or feedback. We're excited to see what you'll build with MginDB!

---

**Link**: [mgindb.com](https://mgindb.com)  
**Documentation**: [mgindb.com/documentation.html](https://mgindb.com/documentation.html)

Happy coding!