In the previously posted blog, Compaction in Percona Server for MongoDB (PSMDB), we discussed how compact works before version 4.4. In this blog, we will see how compact works on PSMDB 6.0.

I recommend reading the blog post linked above to understand what compact does, how to check dataSize, and how much space we can reclaim.

Below is a collection name demo under the test database, from which we have deleted the data. Check the count before and after the delete:

When a large chunk of data is deleted from a collection, and the collection never uses the deleted space for new documents, this space needs to be returned to the operating system to be used by your other databases or collections. Use the below command to check how much data we can reclaim in the demo collection:

Note: The above command output will come in bytes.

Prerequisites

Once you have the details and have decided which collection you want to compact and reclaim space, please keep in mind the following before running the compact command:

  1. Always take the full backup of the database.
  2. The user should have the required privilege to run the compact command.
  3. Always run compact on the secondary nodes/hidden nodes or nodes with low priority, following the Primary node in last after stepping it down.
  4. In a replica set, the compact command must be run on each node.
  5. In a shard cluster, the compact command needs to be run on each node of every shard. Compact cannot be run against the mongos. 
  6. In PSMDB 4.4 or newer, compact will only block the metadata operations like dropping collection, dropping index, creating a new index.
  7. From PSMDB 5.0.12 and 6.0.2, compact running on secondary nodes can replicate the data and reads are permitted on that secondary node.

We have used PSMDB 6.0.14 in this blog. Now, we will run the compact command on the demo collection on a secondary node, start writing the same data to the collection demo from the primary, and read the data from the same collection on the secondary node when compact is running.

First, we will start a script to insert the sample data in the demo collection:

Now, we will compact the collection on the secondary node:

Above, we can see that the compact has reclaimed the disk space. In the logs of the secondary node as well we can see the start and end time of the compact command:

When the compact command was running, we could see the data was being replicated to the secondary node. Below is the output from the secondary node oplog:

We also tried to read the data and were able to successfully read it when compact was running:

You can check the progress of compaction in the Mongo logs or by running the db.currentOp() command in another shell instance.

Once the collections are compacted, please check the reclaimed space using the same command we used to determine how much space we can reclaim. You can also check the disk space on the OS level.

Conclusion

Sometimes, when a large collection is compacted, the compact command immediately returns OK, but in reality, the physical space of the collection remains unchanged. This is because WiredTiger deems that the collection does not need to be compacted. To overcome this, you need to run the compact command again until it releases the space.

Before PSMDB 4.4, it was always advisable to run compact in a scheduled maintenance window due to the nature of the command, which blocks all the read/write operations. Starting from the PSMDB 4.4 version, you can plan to run it at any time.

Percona Server for MongoDB is an open source replacement for MongoDB Community Edition that combines all of the features and benefits of MongoDB Community Edition with enterprise-class features developed by Percona: LDAP Authentication and Authorization, Audit Logging, Kerberos Authentication, and hot backups. To learn more about the enterprise-grade features available in the vendor lock-in-free Percona Server for MongoDB, we recommend reading our blog MongoDB: Why Pay for Enterprise When Open Source Has You Covered. 

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments