MongoDB Concurrency and lock mechanism

Source: Internet
Author: User
Tags deprecated mongodb monitoring

MongoDB Concurrency and lock mechanism


1. The lock used by MongoDB


MongoDB uses a "readers-writer" lock that can support concurrency but has great limitations when a read lock exists, many

The read operation can use this lock, however, when a write lock exists, a single write operation will exclusively hold the lock while

Other reads, writes cannot be shared with this lock; for example, if there are 10 documents in a collection, multiple update operations cannot

Concurrency on this collection, even if you are updating different documents.



2, the size of the lock

Prior to version 2.2, Mongod had only a global lock; at the beginning of the 2.2 version, most read and write operations locked only one library, relative to the previous version,

This granularity has dropped, for example if there are 5 libraries on a Mongod instance, and if you write to only one collection in a library, that

During the write operation, the library is locked while the other 5 libraries are not affected. This granularity is already very large compared to the RDBMS!


3. How to view the status of a lock

Db.serverstatus ()

Db.currentop ()

Mongotop

Mongostat

The MongoDB monitoring Service (MMS)


4. What operations will generate locks on the database?

The following table lists the locks that are generated by common database operations.

Changed in version 2.2.


Operation Lock Type

Issue a query Read lock

Get more data from a cursor Read lock

Insert data Write lock

Remove data Write lock

Update data Write lock

Map-reduce Read Lock and write lock, unless operations is specified as non-atomic. Portions of Map-reduce jobs can run concurrently.

Create an index Building A index in the foreground, which are the default, locks the database for extended period S of time.


Db.eval ()

Deprecated since version 3.0.

Write lock. The Db.eval () method takes a global write lock while evaluating the JavaScript function. To avoid taking this global write lock, you can use the eval command with Nolock:true.


Eval

Deprecated since version 3.0.

Write lock. By default, eval command takes a global write lock while evaluating the JavaScript function. If used with nolock:true, the eval command does not take a global write lock while evaluating the JavaScript function. However, the logic within the JavaScript function may take write locks for write operations.

Aggregate () Read Lock


5, which database management operations will lock the database?

Some database management operations exclusively lock the database, and the following command requires a exclusively lock and locks for a period of time


Db.collection.ensureIndex (),

ReIndex,

Compact

Db.repairdatabase (),

Db.createcollection (), when creating a very large (i.e. many gigabytes) capped collection,

Db.collection.validate (),

Db.copydatabase (). This operation could lock all databases

The following command requires a exclusively lock, locks the database, but locks it for a short time.

Db.collection.dropIndex (),

Db.collection.getLastError (),

Db.ismaster (),

Rs.status (i.e. Replsetgetstatus,)

Db.serverstatus (),

Db.auth (), and

Db.adduser ().

Note: Some view commands can also lock the library, and in the more busy production library, it will also have an impact.



6, Lock the operation of multiple libraries

The following database operations lock multiple libraries.


Db.copydatabase () Lock the entire Mongod instance

Db.repairdatabase () acquires a global write lock that blocks other operations during operation.

Journaling internal operations, a short time to lock all databases, all databases share a journal.

User authentication requires a read lock on the admin database for deployments using user credentials.

For deployments authentication locks the admin database as well as the database the user is accessing.

All writes to a replica set ' s primary lock both the database receiving the writes and then the local database for a short Time.

The lock for the local database allows the Mongod-to-write to the primary's oplog and accounts for a small portion of the Total time of the operation.


7. Concurrency of JavaScript operations

Changed in version 2.4:the V8 JavaScript engine added in 2.4 allows multiple JavaScript operations to run at the same Tim E.

Prior to 2.4, a single mongod could only run a single JavaScript operation at once.

8. Business

MongoDB does not support multi-document transactions


9. MongoDB Isolation Guarantee

For a single document, read and write operations are atomic, so a single document is always in a consistent state, which means that our read operations do not see a partial update.

However, for multiple documents, MongoDB does not isolate concurrent writes:

Therefore, time-based data snapshots are not guaranteed when querying data. For example, we don't get documents that have been deleted during a query or have been updated.

Reads can see data which may subsequently is rolled back in rare cases such as replica set failover or power loss.

It does not mean this read operations can see documents in a partially written or otherwise inconsistent state.


Other systems refer to these semantics as READ UNCOMMITTED.

Reference: http://liyanblog.cn/articles/2013/01/15/1358218545015.html

Reference: http://docs.mongodb.org/manual/faq/concurrency/

This article is from the "Days together with it" blog, so be sure to keep this source http://raugher.blog.51cto.com/3472678/1678704

MongoDB Concurrency and lock mechanism

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.