Monitoring the MongoDB shard balancer status

By David Mytton,
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.

Console output

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.

[sourcecode]
david@sdapp-web1 ~/mongodb-balance-check: python check.py
sd.servers
sdapp1 balanced (9)
sdapp2 balanced (9)
sd.users
sdapp1 unbalanced (62)
sdapp2 unbalanced (89)
sd.alerts
sdapp1 balanced (12)
sdapp2 balanced (12)
…[/sourcecode]

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.

Server Density graph showing collection chunks

[sourcecode language=”python”]
import balanced
result = balanced.is_balanced()
print result
{
‘chunks’: {
u’sd.servers’: {
u’sdapp1′: 9,
u’sdapp2′: 9
},
u’sd.users’: {
u’sdapp1′: 62,
u’sdapp2′: 89
},
u’sd.alerts’: {
u’sdapp1′: 12,
u’sdapp2′: 12
}
},
‘balanceStatus’: {
u’sd.servers’: True,
u’sd.users’: False,
u’sd.alerts’: True
}
‘isBalanced’: False
}[/sourcecode]

Download

The code for this is released as an open source project on Github.

Articles you care about. Delivered.

Help us speak your language. What is your primary tech stack?

Maybe another time