简体   繁体   English

如何在MongoDB中管理并发?

[英]How to manage concurrency in MongoDB?

I am new to MongoDB database and for one of my application planning some portion of it to move to MongoDB. 我是MongoDB数据库的新手,对于我的应用程序之一,我计划将其中的一部分迁移到MongoDB。 Where we need to handle optimistic concurrency. 我们需要处理乐观并发的地方。 What are the best practices available with MongoDB. MongoDB提供了哪些最佳实践。

Is MongoDB right choice for application which requires concurrency? MongoDB是否是需要并发的应用程序的正确选择?

Yes MongoDB would be right choice for concurrency. 是的,MongoDB是并发的正确选择。

MongoDB Locking is Different than the locking in RDBMS. MongoDB锁定与RDBMS中的锁定不同。

MongoDB uses multi-granularity locking(see wired tiger) that allows operations to lock at the global, database or collection level, and allows for individual storage engines to implement their own concurrency control below the collection level (eg, at the document-level in WiredTiger). MongoDB使用多粒度锁定(请参见“有线老虎”),该锁定允许操作锁定在全局,数据库或集合级别,并允许各个存储引擎在集合级别(例如,文档级别的以下级别)中实现其自己的并发控制。 WiredTiger)。

MongoDB uses reader-writer locks that allow concurrent readers shared access to a resource, such as a database or collection, but in MMAPv1, give exclusive access to a single write operation. MongoDB使用读取器-写入器锁,允许并发的读取器共享对资源(例如数据库或集合)的访问,但是在MMAPv1中,它授予对单个写入操作的独占访问权限。

WiredTiger uses optimistic concurrency control. WiredTiger使用乐观并发控制。 WiredTiger uses only intent locks at the global, database and collection levels. WiredTiger仅在全局,数据库和集合级别使用意图锁。 When the storage engine detects conflicts between two operations, one will incur a write conflict causing MongoDB to transparently retry that operation. 当存储引擎检测到两个操作之间存在冲突时,将引发写冲突,从而导致MongoDB透明地重试该操作。

MongoDB has a reader/writer latch for each database. MongoDB为每个数据库都有一个读取器/写入器闩锁。

The latch is multiple-reader, single-writer, and it is writer-greedy, so we can have a unlimited number of simultaneous readers on a database, but there can be only one writer at a time on any collection in any one database. 锁存器是多个读取器,是单个写入器,并且它是写入器贪婪的,因此我们在数据库中可以同时具有无限数量的同时读取器,但是在任何一个数据库中的任何集合上一次只能有一个写入器。

"writer-greedy", gives priority to write, so when we get a write request, all the read requests are blocked until the write is completed. “ writer-greedy”赋予写入优先级,因此当我们收到写入请求时,所有读取请求都将被阻塞,直到写入完成。

The lock here is called as latch since it's lighter than a lock and it performs the job within microseconds. 此处的锁称为“闩锁”,因为它比锁轻,并且可以在几微秒内完成工作。

MongoDB is capable of running as many simultaneous queries. MongoDB能够运行尽可能多的同时查询。

Hope it Helps!! 希望能帮助到你!!

References 参考文献

https://docs.mongodb.com/manual/faq/concurrency/ https://docs.mongodb.com/manual/faq/concurrency/

https://docs.mongodb.com/manual/reference/command/findAndModify/ https://docs.mongodb.com/manual/reference/command/findAndModify/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM