Monitoring the MongoDB shard balancer status
CEO & Founder of Server Density.
Published on the 25th October, 2012.
Update: We recently wrote another article on how to monitor MongoDB. You might like to read that instead.
When you are using sharding built into MongoDB, it automatically manages moving data around and keeping things balanced. You can view the current status of the cluster to see where all the chunks are located by running the
db.printShardingStatus() (docs) command however this is a fairly manual process and if you have a lot of chunks/collections, it can be difficult to visualise whether your cluster is balanced or not.
The consequences of an unbalanced cluster can mean hotspots on certain nodes. Sharding is designed to help scale writes but it defeats the point if you’re sending most of your writes to a single node!
To keep on top of this, I’ve written a simple Python script – available on Github – which can be run to quickly see whether your collections are balanced or not.
Executed on the console, this provides a list of all your collections with a chunk count and visual colour representation of whether the shard is balanced or not.
david@sdapp-web1 ~/mongodb-balance-check: python check.py
sdapp1 balanced (9)
sdapp2 balanced (9)
sdapp1 unbalanced (62)
sdapp2 unbalanced (89)
sdapp1 balanced (12)
sdapp2 balanced (12)
Programmatic Output + Server Density plugin
In addition, it can be imported in your own Python scripts and there is also a wrapper so it can be installed as a plugin to your Server Density monitoring agent so you can graph the distribution and get alerts when things become unbalanced.
result = balanced.is_balanced()
The code for this is released as an open source project on Github.