Map-Reduce Concurrency

The map-reduce operation is composed of many tasks, including readsfrom the input collection, executions of the map function,executions of the reduce function, writes to a temporary collectionduring processing, and writes to the output collection.

During the operation, map-reduce takes the following locks:

  • The read phase takes a read lock. It yields every 100 documents.
  • The insert into the temporary collection takes a write lock for asingle write.
  • If the output collection does not exist, the creation of the outputcollection takes a write lock.
  • If the output collection exists, then the output actions (i.e.merge, replace, reduce) take a write lock. This writelock is global, and blocks all operations on the mongodinstance.

Note

The final write lock during post-processing makes the results appearatomically. However, output actions merge and reduce maytake minutes to process. For the merge and reduce, thenonAtomic flag is available, which releases the lock betweenwriting each outputdocument. Starting in MongoDB 4.2, explicitly setting nonAtomic:false is deprecated. See the db.collection.mapReduce()reference for more information.