We are excited to announce Automated Online Compaction on the ObjectRocket platform for MongoDB.
Automated Online Compaction allows MongoDB instances to be compacted online and in the background on the ObjectRocket platform. The application will only experience a replica set election in order to start using the newly compacted slave. Without this feature, applications experience extended downtime when a collection is compacted or when a database is repaired.
Compactions can be scheduled, and windows defined for when the final stepDown() takes place. Users can turn on the feature and not have to worry about MongoDB fragmenting over time. The instance is kept in a nice tidy form and it’s all automated.
All databases fragment over time, some worse than others depending on the underlaying design. In a generic sense; fragmentation occurs when deletes create spaces that new data or updated data can’t reuse. MongoDB fragments just like most popular databases. Even when using Powerof2Sizes we found that we spent a large amount of our DBA time working to keep customers databases compact. We felt that if we charge base on disk space footprint, it’s only right to help customers keep that footprint tidy. But the stock commands didn’t work for us because of they require service interruptions. Anything we built had to be automated, online, work in parallel at scale, and present a minimal impact to customers.
To this end, we have been working over the last few months to build this feature, and had to release a couple other components in order to make this possible. First, we built a component that allows the user to specify a window when they would like a stepDown() to be performed. Then we needed to build out a complete state machine for MongoDB replica sets. In order for this feature to work properly, our code needed to understand the state of all replica sets in a cluster at any given point in time, understand failures, and understand how to recover. We also needed a scheduler component to allow the scheduled stepDown and compactions. We needed to ensure we took into account backups being run, balancer activity, and overall availability impact to the cluster.
With that work done, we then could build a component that performed complete compactions of a replica set in the background, and almost 100% transparent to the user and calling application. The new feature is called Automated Online Compaction for MongoDB.
Here is how it works:
-
- User requests a compaction manually or they have set a compaction schedule.
- Per shard, a SECONDARY is selected for compaction, and compaction starts.
- Wait until all shards are done.
- Wait for Step Down window, an election takes place and the PRIMARY becomes a SECONDARY. The PRIMARY is compacted at this point. is compacted at this point.
- Finish up by compacting the previous PRIMARY.
It should be reiterated, while the compaction is done in the background on a SECONDARY, in order to rotate it to PRIMARY an election must take place. Users must be aware of this fact, it’s best practice anyway.
Under the covers, here is what we are doing:
We track the Step Down window (if defined) for the instance:
We keep track of each replica set member and it’s state. We update the metadata so we know the state of every slave. State is “syncing” then once completed is “compressed”.
Once all the SECONDARY slaves are {state: “compressed”} we wait for the scheduled Step Down window:
And lastly, we compact the remaining SECONDARY (the previous PRIMARY):
In order to get started with Automated Online Compaction, navigate to your instances view and select the compaction button, then schedule a stepdown window on the settings page. You can optionally choose to run the compaction weekly as well.