Should you compact your MongoDB frequently

mongodb

I have read a post from Parse [1] that you can run compact in order to save the index size on the MongoDB.

Our total index size is 10GB, and our total memory memory is 12GB, as you might know the MongoDB perform badly when index size is near the size of memory, so we tried to run compact and reduced the size to become to 7GB.

However, after 1 week, the size grown back to 10GB, so currently we have a schedule job to run the compact commands.

I am thinking if this really help, and why the index grow so quickly and stabilized when it reach the 10GB?

[1] http://blog.parse.com/2013/03/26/always-be-compacting/

Best Answer

Parse has a unique issue -- they don't know how people are using the MongoDBs they provide. Most people relying on a Parse as a use case for managing their MongoDB will mostly read about how Parse tries to fix poor usage by their customers.

That being said, you shouldn't always be compacting. In fact, if you are having to compact regularly, it means you are using MongoDB inefficiently. The reasons your MongoDB may become "defragmented" are:

  • Regularly deleting documents
  • Updating documents and making the document larger continually (does not include atomic updates)

If you are regularly deleting documents, then you should consider using TTL or capped collections.

If you are updating documents and making the document larger (i.e. continuously extending an array), then you should consider redesigning schema to limit the unlimited document growth. Or, you can consider adding padding as placeholders for the future updates.

Either way, if you are having to compact regularly and often, it is a symptom of not using MongoDB correctly, and you should consider redesigning your schema. Compactions are blocking commands and on datasizes larger than a few gigs can take a while to complete.

Related Topic