This page details system configurations that affect MongoDB, especially in production.
MongoDB Management Service (MMS) is a hosted service that provides monitoring, backup, and automated deployment of MongoDB instances. See the MMS Website and the MMS documentation for more information.
Be sure you have the latest stable release. All releases are available on the Downloads page. This is a good place to verify what is current, even if you then choose to install via a package manager.
Always use 64-bit builds for production. The 32-bit build MongoDB offers for test and development environments is not suitable for production deployments as it can store no more than 2GB of data. See the 32-bit limitations page for more information.
32-bit builds exist to support use on development machines.
MongoDB distributions are currently available for Mac OS X, Linux, Windows Server 2008 R2 64bit, Windows 7 (32 bit and 64 bit), Windows Vista, and Solaris platforms.
MongoDB uses the GNU C Library (glibc) if available on a system. MongoDB requires version at least glibc-2.12-1.2.el6 to avoid a known bug with earlier versions. For best results use at least version 2.13.
Concurrency and Storage¶
New in version 3.0.0: MongoDB includes support for two storage engines: MMAPv1, the storage engine available in previous versions of MongoDB, and WiredTiger. In 3.0, by default MongoDB uses the MMAPv1 engine.
Beginning with MongoDB 3.0, MMAPv1 provides collection-level locking: All collections have a unique readers-writer lock that allows multiple clients to modify documents in different collections at the same time.
Between MongoDB 2.2 and 2.6, each database has a readers-writer lock that allows concurrent read access to a database, but gives exclusive access to a single write operation per database. See the Concurrency page for more information. In earlier versions of MongoDB, all write operations contended for a single readers-writer lock for the entire mongod instance.
WiredTiger supports concurrent access by readers and writers to the documents in a collection. Clients can read documents while write operations are in progress, and multiple clients can modify different documents in a collection at the same time.
In most respects, recommendations for managing production MongoDB systems that use the WiredTiger storage engine are the same as managing any other MongoDB instance.
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 the wiredTiger.engineConfig.cacheSizeGB to a value less than the amount of RAM available in the container.
The WiredTiger section has more information about WiredTiger in MongoDB.
MongoDB uses write ahead logging to an on-disk journal to guarantee that MongoDB is able to quickly recover write operations that were not written to data files before the mongod terminated as a result of a crash or other serious failure.
Use Trusted Networking Environments¶
Always run MongoDB in a trusted environment, with network rules that prevent access from all unknown machines, systems, and networks. As with any sensitive system dependent on network access, your MongoDB deployment should only be accessible to specific systems that require access, such as application servers, monitoring services, and other MongoDB components.
See documents in the Security Section for additional information, specifically:
For Windows users, consider the Windows Server Technet Article on TCP Configuration when deploying MongoDB on Windows.
MongoDB is designed specifically with commodity hardware in mind and has few hardware requirements or limitations. MongoDB’s core components run on little-endian hardware, primarily x86/x86_64 processors. Client libraries (i.e. drivers) can run on big or little endian systems.
Hardware Requirements and Limitations¶
When allocating hardware for an effective MongoDB deployment, consider the following:
Allocate Sufficient RAM and CPU¶
In general, databases are not CPU bound. As such, increasing the number of cores can help, but does not provide significant marginal return.
Use Solid State Disks (SSDs)¶
MongoDB has good results and a good price-performance ratio with SATA SSD (Solid State Disk).
Use SSD if available and economical. Spinning disks can be performant, but SSDs’ capacity for random I/O operations works well with the update model of mongod.
Commodity (SATA) spinning drives are often a good option, as the random I/O performance increase with more expensive spinning drives is not that dramatic (only on the order of 2x). Using SSDs or increasing RAM may be more effective in increasing I/O throughput.
MongoDB and NUMA Hardware¶
Running MongoDB on a system with Non-Uniform Access Memory (NUMA) can cause a number of operational problems, including slow performance for periods of time and high system process usage.
When running MongoDB servers and clients on NUMA hardware, you should configure a memory interleave policy so that the host behaves in a non-NUMA fashion. MongoDB checks NUMA settings on start up when deployed on Linux (since version 2.0) and Windows (since version 2.6) machines, and prints a warning if the NUMA configuration may degrade performance.
See The MySQL “swap insanity” problem and the effects of NUMA post, which describes the effects of NUMA on databases. This blog post addresses the impact of NUMA for MySQL, but the issues for MongoDB are similar. The post introduces NUMA and its goals, and illustrates how these goals are not compatible with production databases.
Configuring NUMA on Windows¶
On Windows, memory interleaving must be enabled through the machine’s BIOS. Please consult your system documentation for details.
Configuring NUMA on Linux¶
numactl --interleave=all <path>
where <path> is the path to the program you are starting. Then, disable zone reclaim in the proc settings using the following command:
echo 0 > /proc/sys/vm/zone_reclaim_mode
To fully disable NUMA behavior, you must perform both operations. For more information, see the Documentation for /proc/sys/vm/*.
Disk and Storage Systems¶
Assign swap space for your systems. Allocating swap space can avoid issues with memory contention and can prevent the OOM Killer on Linux systems from killing mongod.
For the MMAPv1 storage engine, the method mongod uses to map memory files to memory ensures that the operating system will never store MongoDB data in swap space.
Most MongoDB deployments should use disks backed by RAID-10.
RAID-5 and RAID-6 do not typically provide sufficient performance to support a MongoDB deployment.
Avoid RAID-0 with MongoDB deployments. While RAID-0 provides good write performance, it also provides limited availability and can lead to reduced performance on read operations, particularly when using Amazon’s EBS volumes.
The Network File System protocol (NFS) is not recommended for use with MongoDB as some versions perform poorly.
Performance problems arise when both the data files and the journal files are hosted on NFS. You may experience better performance if you place the journal on local or iscsi volumes. If you must use NFS, add the following NFS options to your /etc/fstab file: bg, nolock, and noatime.
Separate Components onto Different Storage Devices¶
For improved performance, consider separating your database’s data, journal, and logs onto different storage devices, based on your application’s access and write pattern.
This will affect your ability to create snapshot-style backups of your data, since the files will be on different devices and volumes.
Scheduling for Virtual Devices¶
Local block devices attached to virtual machine instances via the hypervisor should use a noop scheduler for best performance. The noop scheduler allows the operating system to defer I/O scheduling to the underlying hypervisor.
Write concern describes the guarantee that MongoDB provides when reporting on the success of a write operation. The strength of the write concerns determine the level of guarantee. When inserts, updates and deletes have a weak write concern, write operations return quickly. In some failure cases, write operations issued with weak write concerns may not persist. With stronger write concerns, clients wait after sending a write operation for MongoDB to confirm the write operations.
MongoDB provides different levels of write concern to better address the specific needs of applications. Clients may adjust write concern to ensure that the most important operations persist successfully to an entire MongoDB deployment. For other less critical operations, clients can adjust the write concern to ensure faster performance rather than ensure persistence to the entire deployment.
See the Write Concern document for more information about choosing an appropriate write concern level for your deployment.
MongoDB on Linux¶
Kernel and File Systems¶
When running MongoDB in production on Linux, it is recommended that you use Linux kernel version 2.6.36 or later.
With the MMAPv1 storage engine, MongoDB preallocates its database files before using them and often creates large files. As such, you should use the Ext4 and XFS file systems:
In general, if you use the Ext4 file system, use at least version 2.6.23 of the Linux Kernel.
In general, if you use the XFS file system, use at least version 2.6.25 of the Linux Kernel.
Some Linux distributions require different versions of the kernel to support using ext4 and/or xfs:
Linux Distribution Filesystem Kernel Version CentOS 5.5 ext4, xfs 2.6.18-194.el5 CentOS 5.6 ext4, xfs 2.6.18-3.0.el5 CentOS 5.8 ext4, xfs 2.6.18-308.8.2.el5 CentOS 6.1 ext4, xfs 2.6.32-131.0.15.el6.x86_64 RHEL 5.6 ext4 2.6.18-3.0 RHEL 6.0 xfs 2.6.32-71 Ubuntu 10.04.4 LTS ext4, xfs 2.6.32-38-server Amazon Linux AMI release 2012.03 ext4 3.2.12-3.2.4.amzn1.x86_64
MongoDB requires a filesystem that supports fsync() on directories. For example, HGFS and Virtual Box’s shared folders do not support this operation.
For the MMAPv1 storage engine, consider the following recommendations:
- Turn off atime for the storage volume containing the database files.
- Set the file descriptor limit, -n, and the user process limit (ulimit), -u, above 20,000, according to the suggestions in the ulimit document. A low ulimit will affect MongoDB when under heavy use and can produce errors and lead to failed connections to MongoDB processes and loss of service.
- Disable transparent huge pages as MongoDB performs better with normal (4096 bytes) virtual memory pages.
- Disable NUMA in your BIOS. If that is not possible see MongoDB on NUMA Hardware.
Ensure that readahead settings for the block devices that store the database files are appropriate. For random access use patterns, set low readahead values. A readahead of 32 (16kb) often works well.
For a standard block device, you can run sudo blockdev --report to get the readahead settings and sudo blockdev --setra <value> <device> to change the readahead settings. Refer to your specific operating system manual for more information.
For all MongoDB deployments:
- Use the Network Time Protocol (NTP) to synchronize time among your hosts. This is especially important in sharded clusters.
MongoDB Enterprise and SSL Libraries¶
On Linux platforms, you may observe one of the following statements in the MongoDB log:
<path to SSL libs>/libssl.so.<version>: no version information available (required by /usr/bin/mongod) <path to SSL libs>/libcrypto.so.<version>: no version information available (required by /usr/bin/mongod)
These warnings indicate that the system’s SSL libraries are different from the SSL libraries that the mongod was compiled against. Typically these messages do not require intervention; however, you can use the following operations to determine the symbol versions that mongod expects:
objdump -T <path to mongod>/mongod | grep " SSL_" objdump -T <path to mongod>/mongod | grep " CRYPTO_"
These operations will return output that resembles one the of the following lines:
0000000000000000 DF *UND* 0000000000000000 libssl.so.10 SSL_write 0000000000000000 DF *UND* 0000000000000000 OPENSSL_1.0.0 SSL_write
The last two strings in this output are the symbol version and symbol name. Compare these values with the values returned by the following operations to detect symbol version mismatches:
objdump -T <path to SSL libs>/libssl.so.1* objdump -T <path to SSL libs>/libcrypto.so.1*
This procedure is neither exact nor exhaustive: many symbols used by mongod from the libcrypto library do not begin with CRYPTO_.
MongoDB on Windows¶
MongoDB 2.6.6 and Later Using MMAPv1¶
Microsoft has released a hotfix for Windows 7 and Windows Server 2008 R2, KB2731284, that repairs a bug in these operating systems’ use of memory-mapped files that adversely affects the performance of MongoDB using the MMAPv1 storage engine.
Install this hotfix to obtain significant performance improvements on MongoDB 2.6.6 and later releases in the 2.6 series, which use MMAPv1 exclusively, and on 3.0 and later when using MMAPv1 as the storage engine.
MongoDB on Virtual Environments¶
This section describes considerations when running MongoDB in some of the more common virtual environments.
For all platforms, consider Scheduling for Virtual Devices.
MongoDB is compatible with EC2 and requires no configuration changes specific to the environment.
You may alternately choose to obtain a set of Amazon Machine Images (AMI) that bundle together MongoDB and Amazon’s Provisioned IOPS storage volumes. Provisioned IOPS can greatly increase MongoDB’s performance and ease of use. For more information, see this blog post.
This applies to all Azure deployments, using any guest operating system.
If your volumes have inappropriate cache settings, MongoDB may eventually shut down with the following error:
[DataFileSync] FlushViewOfFile for <data file> failed with error 1 ... [DataFileSync] Fatal Assertion 16387
The performance characteristics of MongoDB may change with READ/WRITE caching enabled.
The TCP keepalive on the Azure load balancer is 240 seconds by default, which can cause it to silently drop connections if the TCP keepalive on your Azure systems is greater than this value. You should set tcp_keepalive_time to 120 to ameliorate this problem.
On Linux systems you can use the following operation to check the value of tcp_keepalive_time:
The value is measured in seconds. You can change the tcp_keepalive_time value with the following operation:
echo <value> > /proc/sys/net/ipv4/tcp_keepalive_time
For Windows systems, issue the following command to view the keep alive setting:
reg query HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters /v KeepAliveTime
The registry value is not present by default. The system default, used if the value is absent, is 7200000 milliseconds or 0x6ddd00 in hexadecimal. To set a shorter keep alive period use the following invocation in an Administrator Command Prompt, where <value> is expressed in hexadecimal (e.g. 0x0124c0 is 120000):
reg add HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\ /v KeepAliveTime /d <value>
Windows users should consider the Windows Server Technet Article on KeepAliveTime for more information on setting keep alive for MongoDB deployments on Windows systems.
MongoDB is compatible with VMWare. As some users have run into issues with VMWare’s memory overcommit feature, disabling the feature is recommended.
It is possible to clone a virtual machine running MongoDB. You might use this function to spin up a new virtual host to add as a member of a replica set. If you clone a VM with journaling enabled, the clone snapshot will be valid. If not using journaling, first stop mongod, then clone the VM, and finally, restart mongod.
On Linux, use the iostat command to check if disk I/O is a bottleneck for your database. Specify a number of seconds when running iostat to avoid displaying stats covering the time since server boot.
For example, the following command will display extended statistics and the time for each displayed report, with traffic in MB/s, at one second intervals:
iostat -xmt 1
Key fields from iostat:
- %util: this is the most useful field for a quick check, it indicates what percent of the time the device/drive is in use.
- avgrq-sz: average request size. Smaller number for this value reflect more random IO operations.