- Frequently Asked Questions >
- FAQ: MongoDB Storage
FAQ: MongoDB Storage¶
On this page
This document addresses common questions regarding MongoDB’s storage system.
Storage Engine Fundamentals¶
What is a storage engine?¶
A storage engine is the part of a database that is responsible for managing how data is stored, both in memory and on disk. Many databases support multiple storage engines, where different engines perform better for specific workloads. For example, one storage engine might offer better performance for read-heavy workloads, and another might support a higher-throughput for write operations.
See also
Can you mix storage engines in a replica set?¶
Yes. You can have a replica set members that use different storage engines.
When designing these multi-storage engine deployments consider the following:
- the oplog on each member may need to be sized differently to account for differences in throughput between different storage engines.
- recovery from backups may become more complex if your backup captures data files from MongoDB: you may need to maintain backups for each storage engine.
WiredTiger Storage Engine¶
Can I upgrade an existing deployment to a WiredTiger?¶
Yes. See:
How much compression does WiredTiger provide?¶
The ratio of compressed data to uncompressed data depends on your data and the compression library used. By default, collection data in WiredTiger use Snappy block compression; zlib compression is also available. Index data use prefix compression by default.
To what size should I set the WiredTiger cache?¶
With WiredTiger, MongoDB utilizes both the WiredTiger cache and the filesystem cache.
Changed in version 3.2: Starting in MongoDB 3.2, the WiredTiger cache, by default, will use the larger of either:
- 60% of RAM minus 1 GB, or
- 1 GB.
For systems with up to 10 GB of RAM, the new default setting is less than or equal to the 3.0 default setting (For MongoDB 3.0, the WiredTiger cache uses either 1 GB or half of the installed physical RAM, whichever is larger).
For systems with more than 10 GB of RAM, the new default setting is greater than the 3.0 setting.
Via the filesystem cache, MongoDB automatically uses all free memory that is not used by the WiredTiger cache or by other processes. Data in the filesystem cache is compressed.
To adjust the size of the WiredTiger cache, see storage.wiredTiger.engineConfig.cacheSizeGB and --wiredTigerCacheSizeGB. Avoid increasing the WiredTiger cache size above its default value.
Note
The storage.wiredTiger.engineConfig.cacheSizeGB only limits the size of the WiredTiger cache, not the total amount of memory used by mongod. The WiredTiger cache is only one component of the RAM used by MongoDB. MongoDB also automatically uses all free memory on the machine via the filesystem cache (data in the filesystem cache is compressed).
In addition, the operating system will use any free RAM to buffer filesystem blocks.
To accommodate the additional consumers of RAM, you may have to decrease WiredTiger cache size.
The default WiredTiger cache size value assumes that there is a single mongod instance per node. If a single node contains multiple instances, then you should decrease the setting to accommodate the other mongod instances.
If you run mongod in a container (e.g. lxc, cgroups, Docker, etc.) that does not have access to all of the RAM available in a system, you must set storage.wiredTiger.engineConfig.cacheSizeGB to a value less than the amount of RAM available in the container. The exact amount depends on the other processes running in the container.
To view statistics on the cache and eviction rate, see the wiredTiger.cache field returned from the serverStatus command.
How frequently does WiredTiger write to disk?¶
MongoDB configures WiredTiger to create checkpoints (i.e. write the snapshot data to disk) at intervals of 60 seconds or 2 gigabytes of journal data.
For journal data, MongoDB writes to disk according to the following intervals or condition:
- Every 100 milliseconds.
- MongoDB sets checkpoints to occur in WiredTiger on user data at an interval of 60 seconds or when 2 GB of journal data has been written, whichever occurs first.
- If the write operation includes a write concern of j: true, WiredTiger forces a sync of the WiredTiger log files.
- Because MongoDB uses a log file size limit of 100 MB, WiredTiger creates a new journal file approximately every 100 MB of data. When WiredTiger creates a new journal file, WiredTiger syncs the previous journal file.
MMAPv1 Storage Engine¶
What are memory mapped files?¶
A memory-mapped file is a file with data that the operating system places in memory by way of the mmap() system call. mmap() thus maps the file to a region of virtual memory. Memory-mapped files are the critical piece of the MMAPv1 storage engine in MongoDB. By using memory mapped files, MongoDB can treat the contents of its data files as if they were in memory. This provides MongoDB with an extremely fast and simple method for accessing and manipulating data.
How do memory mapped files work?¶
MongoDB uses memory mapped files for managing and interacting with all data.
Memory mapping assigns files to a block of virtual memory with a direct byte-for-byte correlation. MongoDB memory maps data files to memory as it accesses documents. Unaccessed data is not mapped to memory.
Once mapped, the relationship between file and memory allows MongoDB to interact with the data in the file as if it were memory.
How frequently does MMAPv1 write to disk?¶
In the default configuration for the MMAPv1 storage engine, MongoDB writes to the data files on disk every 60 seconds and writes to the journal files roughly every 100 milliseconds.
To change the interval for writing to the data files, use the storage.syncPeriodSecs setting. For the journal files, see storage.journal.commitIntervalMs setting.
These values represent the maximum amount of time between the completion of a write operation and when MongoDB writes to the data files or to the journal files. In many cases MongoDB and the operating system flush data to disk more frequently, so that the above values represents a theoretical maximum.
Why are the files in my data directory larger than the data in my database?¶
The data files in your data directory, which is the /data/db directory in default configurations, might be larger than the data set inserted into the database. Consider the following possible causes:
Preallocated data files¶
MongoDB preallocates its data files to avoid filesystem fragmentation, and because of this, the size of these files do not necessarily reflect the size of your data.
The storage.mmapv1.smallFiles option will reduce the size of these files, which may be useful if you have many small databases on disk.
The oplog¶
If this mongod is a member of a replica set, the data directory includes the oplog.rs file, which is a preallocated capped collection in the local database.
The default allocation is approximately 5% of disk space on 64-bit installations. In most cases, you should not need to resize the oplog. See Oplog Sizing for more information.
The journal¶
The data directory contains the journal files, which store write operations on disk before MongoDB applies them to databases. See Journaling.
Empty records¶
MongoDB maintains lists of empty records in data files as it deletes documents and collections. MongoDB can reuse this space, but will not, by default, return this space to the operating system.
To allow MongoDB to more effectively reuse the space, you can de-fragment your data. To de-fragment, use the compact command. The compact requires up to 2 gigabytes of extra disk space to run. Do not use compact if you are critically low on disk space. For more information on its behavior and other considerations, see compact.
compact only removes fragmentation from MongoDB data files within a collection and does not return any disk space to the operating system. To return disk space to the operating system, see How do I reclaim disk space?.
How do I reclaim disk space?¶
The following provides some options to consider when reclaiming disk space.
Note
You do not need to reclaim disk space for MongoDB to reuse freed space. See Empty records for information on reuse of freed space.
repairDatabase¶
You can use repairDatabase on a database to rebuilds the database, de-fragmenting the associated storage in the process.
repairDatabase requires free disk space equal to the size of your current data set plus 2 gigabytes. If the volume that holds dbpath lacks sufficient space, you can mount a separate volume and use that for the repair. For additional information and considerations, see repairDatabase.
Warning
Do not use repairDatabase if you are critically low on disk space.
repairDatabase will block all other operations and may take a long time to complete.
You can only run repairDatabase on a standalone mongod instance.
You can also run the repairDatabase operation for all databases on the server by restarting your mongod standalone instance with the --repair and --repairpath options. All databases on the server will be unavailable during this operation.
Resync the Member of the Replica Set¶
For a secondary member of a replica set, you can perform a resync of the member by: stopping the secondary member to resync, deleting all data and subdirectories from the member’s data directory, and restarting.
For details, see Resync a Member of a Replica Set.
What is the working set?¶
Working set represents the total body of data that the application uses in the course of normal operation. Often this is a subset of the total data size, but the specific size of the working set depends on actual moment-to-moment use of the database.
If you run a query that requires MongoDB to scan every document in a collection, the working set will expand to include every document. Depending on physical memory size, this may cause documents in the working set to “page out,” or to be removed from physical memory by the operating system. The next time MongoDB needs to access these documents, MongoDB may incur a hard page fault.
For best performance, the majority of your active set should fit in RAM.
What are page faults?¶
With the MMAPv1 storage engine, page faults can occur as MongoDB reads from or writes data to parts of its data files that are not currently located in physical memory. In contrast, operating system page faults happen when physical memory is exhausted and pages of physical memory are swapped to disk.
If there is free memory, then the operating system can find the page on disk and load it to memory directly. However, if there is no free memory, the operating system must:
- find a page in memory that is stale or no longer needed, and write the page to disk.
- read the requested page from disk and load it into memory.
This process, on an active system, can take a long time, particularly in comparison to reading a page that is already in memory.
See Page Faults for more information.
What is the difference between soft and hard page faults?¶
Page faults occur when MongoDB, with the MMAP storage engine, needs access to data that isn’t currently in active memory. A “hard” page fault refers to situations when MongoDB must access a disk to access the data. A “soft” page fault, by contrast, merely moves memory pages from one list to another, such as from an operating system file cache.
See Page Faults for more information.
Data Storage Diagnostics¶
How can I check the size of a collection?¶
To view the statistics for a collection, including the data size, use the db.collection.stats() method from the mongo shell. The following example issues db.collection.stats() for the orders collection:
db.orders.stats();
MongoDB also provides the following methods to return specific sizes for the collection:
- db.collection.dataSize() to return data size in bytes for the collection.
- db.collection.storageSize() to return allocation size in bytes, including unused space.
- db.collection.totalSize() to return the data size plus the index size in bytes.
- db.collection.totalIndexSize() to return the index size in bytes.
The following script prints the statistics for each database:
db._adminCommand("listDatabases").databases.forEach(function (d) {
mdb = db.getSiblingDB(d.name);
printjson(mdb.stats());
})
The following script prints the statistics for each collection in each database:
db._adminCommand("listDatabases").databases.forEach(function (d) {
mdb = db.getSiblingDB(d.name);
mdb.getCollectionNames().forEach(function(c) {
s = mdb[c].stats();
printjson(s);
})
})
How can I check the size of indexes for a collection?¶
To view the size of the data allocated for an index, use the db.collection.stats() method and check the indexSizes field in the returned document.
How can I get information on the storage use of a database?¶
The db.stats() method in the mongo shell returns the current state of the “active” database. For the description of the returned fields, see dbStats Output.
Thank you for your feedback!
We're sorry! You can Report a Problem to help us improve this page.