codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🍃

MongoDB

45 / 65 topics
35Backup and Restore36mongodump and mongorestore37Replica Set Backup38Sharded Cluster Backup39Monitoring MongoDB40MongoDB Logs41Performance Tuning42Memory Management43Disk Space Optimization44Replica Set Maintenance45Sharded Cluster Maintenance
Tutorials/MongoDB/Sharded Cluster Maintenance
🍃MongoDB

Sharded Cluster Maintenance

Updated 2026-04-20
2 min read

Introduction

Sharding is a method for distributing data across multiple machines, or shards, to improve performance and reliability. In this tutorial, we will cover the essential aspects of maintaining a sharded cluster in MongoDB, including monitoring, backup, recovery, and scaling.

Monitoring Sharded Clusters

Monitoring is crucial for ensuring that your sharded cluster operates efficiently and remains healthy. MongoDB provides several tools and methods to monitor your cluster:

1. MongoDB Atlas

MongoDB Atlas offers built-in monitoring with real-time dashboards, alerts, and performance metrics. It helps you track the health of your shards, replica sets, and other components.

// Example of setting up an alert in MongoDB Atlas
import { Alert } from 'mongodb-atlas';

const alert = new Alert({
  name: "High CPU Usage",
  condition: {
    metric: "cpu_usage",
    operator: ">",
    threshold: 80,
    period: 300 // seconds
  },
  notification: {
    type: "email",
    recipients: ["admin@example.com"]
  }
});

alert.create();

2. MongoDB Ops Manager

Ops Manager is a comprehensive management platform for MongoDB deployments, including sharded clusters. It provides monitoring, backup, and automation capabilities.

// Example of configuring monitoring in Ops Manager
import { Monitoring } from 'mongodb-ops-manager';

const monitoring = new Monitoring({
  host: "localhost",
  port: 27017,
  username: "admin",
  password: "password"
});

monitoring.start();

3. MongoDB Shell and mongostat

mongostat is a command-line tool that provides real-time statistics about the performance of your MongoDB instance.

# Example usage of mongostat
mongostat --uri=mongodb://admin:password@localhost:27017/admin --interval=5

Backup and Recovery

Regular backups are essential for data recovery in case of failures. MongoDB offers several backup solutions:

1. MongoDB Atlas Backups

MongoDB Atlas provides automated, continuous backups with point-in-time recovery.

// Example of restoring a backup in MongoDB Atlas
import { Backup } from 'mongodb-atlas';

const backup = new Backup({
  clusterName: "myCluster",
  snapshotId: "snapshot-1234567890"
});

backup.restore();

2. MongoDB Ops Manager Backups

Ops Manager also provides automated backups and supports various storage options.

// Example of configuring a backup job in Ops Manager
import { BackupJob } from 'mongodb-ops-manager';

const backupJob = new BackupJob({
  host: "localhost",
  port: 27017,
  username: "admin",
  password: "password",
  storageEngine: "gridfs"
});

backupJob.schedule();

3. mongodump and mongorestore

These command-line tools allow you to manually back up and restore your data.

# Example usage of mongodump and mongorestore
mongodump --uri=mongodb://admin:password@localhost:27017/admin --out=/backup

mongorestore --uri=mongodb://admin:password@localhost:27017/admin /backup

Scaling Sharded Clusters

Scaling your sharded cluster involves adding more shards or increasing the resources of existing ones.

1. Adding New Shards

To add a new shard, you need to configure it and then add it to the sharding configuration.

// Example of adding a new shard in MongoDB Shell
sh.addShard("new-shard-replica-set/shard0:27018,shard0:27019");

2. Resharding

Resharding is the process of redistributing data across shards to balance the load.

// Example of initiating a resharding operation in MongoDB Shell
sh.enableSharding("myDatabase");

sh.shardCollection("myDatabase.myCollection", { shardKey: "field" });

sh.reshardCollection("myDatabase.myCollection", {
  key: { newShardKey: 1 },
  unique: true,
  chunks: [
    { min: { newShardKey: MinKey }, max: { newShardKey: MaxKey } }
  ]
});

3. Scaling Resources

You can scale your shards by adding more nodes to replica sets or upgrading the hardware of existing nodes.

// Example of adding a new node to a replica set in MongoDB Shell
rs.add("new-node:27018");

Best Practices for Sharded Cluster Maintenance

  • Regular Monitoring: Continuously monitor your cluster using tools like Atlas, Ops Manager, or mongostat.
  • Automated Backups: Use automated backup solutions to ensure data recovery is possible.
  • Proactive Scaling: Plan and execute scaling operations based on monitoring data to maintain performance.
  • Testing Recovery Procedures: Regularly test your backup and recovery procedures to ensure they work as expected.
  • Documentation: Keep detailed documentation of your cluster configuration, maintenance tasks, and any changes made.

Conclusion

Maintaining a sharded MongoDB cluster requires careful planning and execution. By following the guidelines in this tutorial, you can ensure that your cluster remains healthy, performs well, and is ready for any challenges that come its way.


PreviousReplica Set MaintenanceNext MongoDB Driver Basics

Recommended Gear

Replica Set MaintenanceMongoDB Driver Basics