Write Operation Performance

Indexes

Each index on a collection adds some amount of overhead to theperformance of write operations.

For each insert or delete write operationon a collection, MongoDB either inserts or removes the correspondingdocument keys from each index in the target collection. Anupdate operation may result in updates to a subset ofindexes on the collection, depending on the keys affected by the update.

Note

MongoDB only updates a sparse orpartial index if the documents involvedin a write operation are included in the index.

In general, the performance gains that indexes provide for readoperations are worth the insertion penalty. However, in order tooptimize write performance when possible, be careful when creating newindexes and evaluate the existing indexes to ensure that your queriesactually use these indexes.

For indexes and queries, see Query Optimization. For moreinformation on indexes, see Indexes andIndexing Strategies.

Storage Performance

Hardware

The capability of the storage system creates some important physicallimits for the performance of MongoDB’s write operations. Many uniquefactors related to the storage system of the drive affect writeperformance, including random access patterns, disk caches,disk readahead and RAID configurations.

Solid state drives (SSDs) can outperform spinning hard disks (HDDs) by100 times or more for random workloads.

See

Production Notes for recommendationsregarding additional hardware and configuration options.

Journaling

To provide durability in the event of a crash, MongoDB uses writeahead logging to an on-disk journal. MongoDB writes thein-memory changes first to the on-disk journal files. If MongoDB shouldterminate or encounter an error before committing the changes to thedata files, MongoDB can use the journal files to apply the writeoperation to the data files.

While the durability assurance provided by the journal typicallyoutweigh the performance costs of the additional write operations,consider the following interactions between the journal andperformance:

  • If the journal and the data file reside on the same block device,the data files and the journal may have to contend for a finitenumber of available I/O resources. Moving the journal to aseparate device may increase the capacity for write operations.
  • If applications specify write concernsthat include the j option,mongod will decrease the duration between journalwrites, which can increase the overall write load.
  • The duration between journal writes is configurable using thecommitIntervalMs run-time option. Decreasing theperiod between journal commits will increase the number of writeoperations, which can limit MongoDB’s capacity for writeoperations. Increasing the amount of time between journal commits maydecrease the total number of write operation, but also increases thechance that the journal will not record a write operation in theevent of a failure.

For additional information on journaling, seeJournaling.