Disk space utilization is a critical metric for ApsaraDB for MongoDB instances. If an instance runs out of disk space, it becomes unavailable. Learn how to check disk space usage, identify causes of high utilization, and apply optimization strategies.
Background information
If disk space utilization reaches 80% or higher, reduce the database's actual space consumption or expand the storage capacity to prevent the instance from becoming full.
Check space usage
Replica set architecture
If your ApsaraDB for MongoDB instance uses a replica set architecture, log on to the ApsaraDB for MongoDB console and use the following methods to check its space usage:
-
Overview
On the Basic Information page, go to the Specification Information section to view the instance's Disk space and Usage.
-
Monitoring chart analysis
In the left-side navigation pane, click Monitoring Information. Select a target node to view its Disk Usage (Bytes) and Disk Usage (%).
An ApsaraDB for MongoDB replica set instance consists of a primary node for read and write operations, one or more secondary nodes for high availability, a hidden node, and optional read-only nodes. The space usage of each node consists of
data_sizeandlog_size, whereins_size = data_size + log_size:-
data_size: The disk space used by data, excluding thelocaldatabase. This includes data files (prefixed withcollection), index files (prefixed withindex), and metadata files such asWiredTiger.wt. -
log_size: The physical size of thelocaldatabase, the size of MongoDB runtime logs, and the size of some audit logs.
-
-
Detailed analysis
Use the following methods to perform a detailed analysis of space usage:
-
Use MongoDB's native commands
db.stats()anddb.$collection_name.stats().For more information about these commands, see the following MongoDB documentation:
-
Use the page.
On the page, you can view the following information:
-
An overview of space usage for databases and collections, their average daily growth, and the predicted number of days until storage is full.
-
Space usage of anomalous databases and tables.
-
Detailed space usage for business collections, including index file size, data file size, compression ratio analysis, and average document size.
-
-
Sharded cluster architecture
If your ApsaraDB for MongoDB instance uses a sharded cluster architecture, log on to the ApsaraDB for MongoDB console and use the following methods to check its space usage:
-
Monitoring chart analysis
On the Monitoring Information page, select a target node to view its Disk Usage (Bytes) and Disk Usage (%).
-
Detailed analysis
Sequentially analyze the space usage of each node by using MongoDB's native commands
db.stats()anddb.$collection_name.stats().
High space usage due to data fragmentation
Impact on the instance during compaction
The compact operation can be time-consuming because its duration is proportional to the collection size. To avoid affecting read and write operations, run compact during off-peak hours.
The compact method
To minimize impact on your business, first run the db.runCommand({compact:"collectionName"}) command on a secondary node, and then perform a primary/secondary switchover. Replace collectionName with the name of your collection.
For more information about how to use the compact command, see Defragment disks to improve disk utilization.
Increased space usage due to large logs
Space gap from large journal logs
In ApsaraDB for MongoDB versions earlier than 4.0, if the number of open files on the host machine reaches its limit, the internal log server cleanup thread is interrupted and the journal log grows indefinitely. If you see log entries similar to the following in the MongoDB runtime logs, upgrade the kernel to ApsaraDB for MongoDB 4.0 or later. As a temporary fix, restart the mongod process. For more details, see log-server thread exit quietly on error while the mongodb process still running.
2019-08-25T09:45:16.867+0800 I NETWORK [thread1] Listener: accept() returns -1 Too many open files in system
2019-08-25T09:45:17.000+0800 I - [ftdc] Assertion: 13538:couldn't open [/proc/55692/stat] Too many open files in system src/mongo/util/processinfo_linux.cpp 74
2019-08-25T09:45:17.002+0800 W FTDC [ftdc] Uncaught exception in 'Location13538: couldn't open [/proc/55692/stat] Too many open files in system' in full-time diagnostic data capture subsystem. Shutting down the full-time diagnostic data capture subsystem.
Log growth from replication lag and backups
When an ApsaraDB for MongoDB instance experiences replication lag, the usable size of the oplog is no longer limited by the fixed-size collection defined in the configuration file. In theory, it can grow to 20% of the provisioned disk capacity. After the lag is resolved, the extra space occupied by the oplog is not automatically reclaimed.
During a physical backup, an ApsaraDB for MongoDB instance on a hidden node generates numerous checkpoints, consuming more data and log space.
For both scenarios, you can resolve the issue by running a compact operation on the oplog:
All write operations are blocked during the compact operation.
db.grantRolesToUser("root", [{db: "local", role: "dbAdmin"}])
use local
db.runCommand({ compact: "oplog.rs", force: true })
Data imbalance from improper sharding
Inappropriate shard key type
In a sharded cluster, the choice of shard key type is critical. The two main types are hashed sharding and ranged sharding. For disk balancing, hashed sharding is often a better strategy than ranged sharding because ApsaraDB for MongoDB uses an internal hash function to evenly distribute data across shards based on key values. In contrast, ranged sharding distributes data based on key value ranges, which can concentrate new data in a "hot" chunk, causing high disk I/O on that shard and short-term data imbalance.
For more information about shard key types, see sharding-shard-key, hashed-sharding, and ranged-sharding.
Inappropriate shard key field
Although the number of chunks on each shard may be similar, most of the data is concentrated in a few chunks. As a result, the shards containing these "hot" chunks store significantly more data than others. When you run the sh.status() command and check the ApsaraDB for MongoDB runtime logs, you may see warning messages similar to the following:
2019-08-27T13:31:22.076+0800 W SHARDING [conn12681919] possible low cardinality key detected in superHotItemPool.haodanku_all - key is { batch: "201908260000" }
2019-08-27T13:31:22.076+0800 W SHARDING [conn12681919] possible low cardinality key detected in superHotItemPool.haodanku_all - key is { batch: "201908260200" }
2019-08-27T13:31:22.076+0800 W SHARDING [conn12681919] possible low cardinality key detected in superHotItemPool.haodanku_all - key is { batch: "201908260230" }
The mongos balancer primarily considers chunk counts on each shard to determine whether data is balanced. This can lead to a scenario where chunk counts are balanced but actual data distribution is heavily skewed. This happens because shard key values within a chunk are almost identical. When the chunk reaches the 64 MB split threshold, MongoDB splits it and creates an empty chunk. Over time, although chunk counts increase and migrations occur, the migrated chunks are often empty—resulting in balanced chunk counts but imbalanced data. To fix this, redesign your sharding strategy to use a high-cardinality field as the shard key.
For more information about splitting, see sharding-data-partitioning and split-chunks-in-sharded-cluster.
Unsharded databases in a sharded cluster
An ApsaraDB for MongoDB sharded cluster instance supports both sharded and unsharded databases. All data from an unsharded database resides on a single primary shard, so if that database is large, its primary shard may store significantly more data than other shards.
This issue can also occur when you import data from a source mongos cluster to a new one but neglect to configure sharding on the destination cluster before the import.
To address these issues, we recommend the following:
-
If you are initializing a destination cluster with an import, design and enable sharding before you start.
-
If you have many unsharded databases of roughly equal size, use the
movePrimarycommand to move specific databases to different shards. -
If a very large unsharded database exists, we recommend either sharding it or migrating it to a standalone replica set instance.
-
If this situation occurs but you have sufficient disk space, you can choose to ignore the issue.
Uneven disk usage from moveChunk operations
A moveChunk operation writes data to a destination shard and then removes it from the source shard. By default, the remove operation does not release disk space. In the WiredTiger engine, each collection has separate data and index files, and total disk space does not shrink unless these files are deleted. This problem is most common when sharding is enabled on a cluster that has been running for some time without a sharding design.
Therefore, after many moveChunk or document removal operations, run a compact operation on the affected shard to reclaim the fragmented space.
For more information about moveChunk, see migrate-chunks-in-sharded-cluster and manage-sharded-cluster-balancer.