Skip to content

MongoDB Performance Optimization cheat sheet

homepage-banner

Hardware

  • SSD
  • RAID10
  • 10GbE network card

System Parameters

ext4/xfs

Disable NUMA

Add numactl--interleave=all before mongodb-related commands to disable NUMA functionality.

numactl --interleave=all /opt/mongodb/bin/mongod -f /opt/mongodb/conf/mongo.conf

Disable Disk Read Ahead

To further reduce the number of actual I/O operations, the disk performs pre-reading each time, that is, read a certain length of data sequentially and place it in memory while reading data. The premise of disk read-ahead is that most of the data is read in sequence, for example, after reading a part of a video file, the data segment behind it will definitely be read. However, in most scenarios, MongoDB will randomly access the disk, so read-ahead has limited performance benefits and may cause some invalid memory occupation.

## Check
blockdev --report
## Modify
blockdev --setra 0 /dev/sda

Disable Transparent Huge Pages

Since the database generally accesses memory randomly rather than sequentially, Transparent Huge Pages (THP) actually restricts MongoDB’s performance in random access mode.

echo never > /sys/kernel/mm/transparent_hugepage/enable

Disable atime Option

The file system records the access time of each file by default. Since MongoDB may frequently access data files, disabling the access time record can improve performance. Use the noatime option when mounting the data partition in a Linux system.

echo "/dev/sdb /data xfs noatime,nodiratime 0 0" >> /etc/fstab

Modify Resource Limitations

MongoDB dynamically creates connections as needed. In the default network I/O model, each connection uses one thread and contains a file descriptor handle.

Edit the /etc/sysctl.conf file.

Save the settings.

Database Configuration

Enable Journaling

MongoDB adopts a buffer delay write mechanism, and there is a risk of losing data for up to 60 seconds. The Journaling function provides power-off protection, which can reduce the risk of loss to less than 100ms.

Separate Logs and Data

It is recommended to store MongoDB running logs, Journal pre-write logs, and data files on different disks, which is conducive to improving the throughput of overall I/O.

Keep Clocks Synchronized

It is recommended to use NTP service to keep the clocks of nodes synchronized, and the delay should not exceed 1s. MongoDB has done some compatibility with clock deviation, but the clock delay between distributed nodes may still have some unknown effects.

Connection Limit

  • Each connection requires a file handle, as well as an independent read and write buffer for the TCP protocol stack.
  • By default, MongoDB allocates one thread for each connection, and the default thread stack has a maximum space of 1MB.

On the MongoDB server side, configure net.maxIncomingConnections to limit the maximum concurrent connections, which is recommended not to exceed 10,000.

On the client side, the driver sets the connection limit to 100 per remote host by default, and the application can adjust it appropriately.

Reference

  • “MongoDB Advanced and Practice: Microservice Integration, Performance Optimization, Architecture Management” (Tang Zhuozhang)
Leave a message