What are intention shared and intention exclusive locks in mongodb?
Asked Answered
L

1

15

Can anyone explain intention shared and intention exclusive locks in mongodb by examples?

I read about their functionality but I can't figure out what are their actual usage in real db examples.

Update: (More information)

Assumption: mongodb version is above 3.0.0

What happens when a document is created? Which locks are acquired in different layer:DB, Collection or document?(S, X, IS or IX)

Libava answered 5/1, 2016 at 13:37 Comment(1)
What storage engine are you using ?Hypothermia
H
16

Intent locks are higher level locks acquired before lower level locks are put in place and are called this way as they signal the intent of these lower level locks. Regarding the different types, intent shared locks are put in place for read operations that do not change or update data, such as a find() query whereas intend exclusive locks are put in place for data-modification operations, such as save(), update and remove.

To illustrate this, take this example collection:

db.data.save({"name":"John"})
db.data.save({"name":"Jane"})

if you run the find query below, an intent shared lock is requested on the Jane record. As this query does not update the data, other users can run the same query or with other words, there can be multiple intent shared locks on the same record.

db.data.find({"name":"Jane"})

However, if you now run this query:

db.data.update({name:"Jane"},{name:"Janet"})

and intent exclusive lock is requested as this operation changes the record. This lock can't be put in place until all other locks have been released on this record (or collection). While the exclusive lock is in place, no other intent locks can be applied on the record (or collection). This means, that if the update operation would take a considerable amount of time, any find operation would be blocked during the duration of the exclusive lock is in place.

Please note that the locking behaviour has significantly improved since version 2.4. In version 2.4 intent exclusive locks where applied at the database level, whereas for version 3.0 locks are only at collection level when using the MMAPv1 storage engine and at record level when using the WiredTiger storage engine (default in 3.2).

Regarding the difference between the intent locks IS and IX and the lower level locks S and X, the intent locks are high level locks that act as a traffic signal. Once the intent locks are in place (e.g. at collection level), the lower level locks are put in place (e.g. at document level). This design reduces the processing needed for managing locks as a concurrent sessions only has to read the intent locks and not all the lower level locks.

Hypothermia answered 5/1, 2016 at 13:57 Comment(7)
Thanks for your explanation but why db uses IS and IX when it can just use S and X? What is the added value of IS and IX?Libava
How does this design help to reduce the process of lock management? After finding the desired document, we just need to check that document's lock not all the other locks in the collection.Libava
If there is an intent exclusive lock at collection level, there is no need to look for the desired document until that lock has been released. Intent locks are at the top of the hierarchy and their purpose is to prevent unnecessary checking of low level locks where possible.Hypothermia
If collection has IX lock, does it mean that all of the documents are updating? Isn't it against the document level locks philosophy?Libava
When using the MMAPv1 engine a collection level IX lock is taken when updating a record. When using the WiredTiger engine IX locks are only taken at document level. You can still have an IX lock with WiredTiger though when you drop the entire collection.Hypothermia
Thank you, now I know how it works in different engines but its still quite ambiguous for me that why in MMAPv1 engine, IX lock is taken when update occurred? Why instead of IX lock, X lock is not taken?Libava
IX locks are at the top level (collection), whereas X locks are lower in the hierarchy. For a one record update the IX lock is at collection level and the X lock at record level.Hypothermia

© 2022 - 2024 — McMap. All rights reserved.