DEV Community

John JustGo
John JustGo

Posted on

Sharding Made Easy with MginDB in 5 Minutes

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

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:
   CONFIG SET SHARDING_TYPE MASTER
   CONFIG SET SHARDING_TYPE SLAVE
Enter fullscreen mode Exit fullscreen mode
  1. Adding a Shard: To add a new server to the shard list, use the following command:
   CONFIG SET SHARDS ADD serverip/domain
Enter fullscreen mode Exit fullscreen mode

This command registers serverip/domain as a new shard where data can be distributed.

  1. Removing a Shard: To remove an existing server from the shard list, use the command:
   CONFIG SET SHARDS DEL serverip/domain
Enter fullscreen mode Exit fullscreen mode

This removes serverip/domain from the list of shards, redistributing any data it held to remaining shards.

  1. Activating Sharding: To activate sharding and start distributing data across the configured shards, execute:
   CONFIG SET SHARDING 1
Enter fullscreen mode Exit fullscreen mode

Ensure you add the necessary shards before activating sharding to distribute data evenly.

  1. Deactivating Sharding: To deactivate sharding and consolidate all data back to the master server, use:
   CONFIG SET SHARDING 0
Enter fullscreen mode Exit fullscreen mode

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):
  CONFIG SET SHARDING_BATCH_SIZE value
Enter fullscreen mode Exit fullscreen mode

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

Documentation: mgindb.com/documentation.html

Happy coding!

Top comments (0)