Read Write Lock

169 views
Skip to first unread message

megha garg

unread,
Feb 19, 2015, 6:29:16 AM2/19/15
to mongod...@googlegroups.com
Hi,

I have some doubt about mongodb locks. 

I am using mongo 2.6.6 and performing bulk insert on one collection with below command:-

> use test
> for(i=1;i<=20000000;i++) { db.test1.insert({a:i}) }

The above command acquires write lock on test database. According to the documentation's below statement i will not be able to acquire read/write lock on test database till this query is running:-

When a read lock exists, many read operations may use this lock. However, when a write lock exists, a single write operation holds the lock exclusively, and no other read or write operations may share the lock.

But i am able to run my find query during this time period on test1 collection of test database :-

> use test
> db.test1.find()


Also, write on same collection, test1 during this time period is also working:-

> use test
> db.test1.insert({c:33})
WriteResult({ "nInserted" : 1 })


The documentation says write lock will block other read/write locks, so how my above read and write queries are running??

Dhananjay D. Makwana

unread,
Feb 19, 2015, 10:35:34 AM2/19/15
to mongod...@googlegroups.com
I am no expert but I vaguely remember reading somewhere that for long
running write operations like you have, mongo "yields" for others so
other operations can also grab the lock to do something.


Quoting megha garg <megha...@gmail.com>:

> Hi,
>
> I have some doubt about mongodb locks.
>
> I am using mongo 2.6.6 and performing bulk insert on one collection with
> below command:-
>
>> use test
>> for(i=1;i<=20000000;i++) { db.test1.insert({a:i}) }
>
> The above command acquires write lock on test database. According to the
> documentation's below statement i will not be able to acquire read/write
> lock on test database till this query is running:-
>
> When a read lock exists, many read operations may use this lock. However,
> when a write lock exists, a single write operation holds the lock
> exclusively, and no other read *or* write operations may share the lock.
>
> But i am able to run my find query during this time period on test1
> collection of test database :-
>
>> use test
>> db.test1.find()
>
>
> Also, write on same collection, test1 during this time period is also
> working:-
>
>> use test
>> db.test1.insert({c:33})
> WriteResult({ "nInserted" : 1 })
>
>
> The documentation says write lock will block other read/write locks, so how
> my above read and write queries are running??
>
> --
> You received this message because you are subscribed to the Google
> Groups "mongodb-user"
> group.
>
> For other MongoDB technical support options, see:
> http://www.mongodb.org/about/support/.
> ---
> You received this message because you are subscribed to the Google
> Groups "mongodb-user" group.
> To unsubscribe from this group and stop receiving emails from it,
> send an email to mongodb-user...@googlegroups.com.
> To post to this group, send email to mongod...@googlegroups.com.
> Visit this group at http://groups.google.com/group/mongodb-user.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/mongodb-user/bcfbb0c9-34cd-4a7b-bc8a-31a379b2f3c3%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.


-DJ
609-309-5340

Will Berkeley

unread,
Feb 19, 2015, 10:54:25 AM2/19/15
to mongod...@googlegroups.com
In 2.6, the write lock does block other reads and writes on the same database. However, operations can yield the lock, effectively pausing themselves, allowing other operations to take a lock and, from the clients' perspective, run at the same time.

Note also that you're doing 20 million different insert calls to the database. It's also possible that your find query arrived between two of the insert calls and ran without waiting for locks at all.

-Will

Asya Kamsky

unread,
Feb 19, 2015, 3:22:30 PM2/19/15
to mongodb-user
You are not running a single bulk insert at all.

You are calling db.test1.insert() 2M times separately with a single document each time, and each time involves a round trip over the network and the lock is held for the microseconds it takes to actual write the data to RAM.

You can send multiple documents to the server by sending an array of documents, though on the server they are still handled one at a time with yielding in-between each one if other operations are waiting.

Asya


--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/bcfbb0c9-34cd-4a7b-bc8a-31a379b2f3c3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
{ "name" : "Asya Kamsky",
  "place" : [ "New York", "Palo Alto", "Everywhere else" ],
  "email" : "as...@mongodb.com",
  "blog" : "http://www.askasya.com/",
  "twitter": "@asya999" }

megha garg

unread,
Feb 21, 2015, 4:10:55 AM2/21/15
to mongod...@googlegroups.com
Then what will be the case/example of database level lock which will not allow other connection to run read and write?

Asya Kamsky

unread,
Feb 22, 2015, 1:37:43 AM2/22/15
to mongodb-user

I'm not really sure what your question is.

While a single document is being written to memory, no other operations can happen.

Asya



For more options, visit https://groups.google.com/d/optout.

megha garg

unread,
Feb 23, 2015, 1:10:53 AM2/23/15
to mongod...@googlegroups.com
Thanks my doubt is cleared. I was confusing bulk insert with lock.
Reply all
Reply to author
Forward
0 new messages