设为首页 收藏本站
查看: 1048|回复: 0

[经验分享] Mongodb并发与锁机制

[复制链接]

尚未签到

发表于 2018-10-26 09:36:03 | 显示全部楼层 |阅读模式
  Mongodb并发与锁机制
  1、 MongoDB 使用的锁
  MongoDB 使用的是“readers-writer”锁, 可以支持并发但有很大的局限性,当一个读锁存在,许多
  读操作可以使用这把锁,然而, 当一个写锁的存在,一个单一的写操作会 exclusively 持有该锁,同时
  其它读,写操作不能使用共享这个锁;举个例子,假设一个集合里有 10 个文档,多个 update 操作不能
  并发在这个集合上,即使是更新不同的文档。
  2、 锁的粒度
  在 2.2 版本以前,mongod 只有全局锁;在 2.2 版本开始,大部分读写操作只锁一个库,相对之前版本,
  这个粒度已经下降,例如如果一个 mongod 实例上有 5 个库,如果只对一个库中的一个集合执行写操作,那
  么在写操作过程中,这个库被锁;而其它 5 个库不影响。相比 RDBMS 来说,这个粒度已经算很大了!
  3、 如何查看锁的状态
  db.serverStatus()
  db.currentOp()
  mongotop
  mongostat
  the MongoDB Monitoring Service (MMS)
  4、 哪些操作会对数据库产生锁?
  下表列出了常见数据库操作产生的锁。
  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 are specified as non-atomic. Portions of map-reduce jobs can run concurrently.
  Create an index Building an index in the foreground, which is the default, locks the database for extended periods 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、 哪些数据库管理操作会锁数据库?
  某些数据库管理操作会 exclusively 锁住数据库,以下命令需要申请 exclusively 锁,并锁定一段时间
  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 may lock all databases
  以下命令需要申请 exclusively 锁,锁定数据库,但锁定很短时间。
  db.collection.dropIndex(),
  db.collection.getLastError(),
  db.isMaster(),
  rs.status() (i.e. replSetGetStatus,)
  db.serverStatus(),
  db.auth(), and
  db.addUser().
  备注:可见,一些查看命令也会锁库,在比较繁忙的生产库中,也会有影响的。
  6、 锁住多个库的操作
  以下数据库操作会锁定多个库。
  db.copyDatabase() 锁定整个mongod实例
  db.repairDatabase() 会获取全局写锁,运行期间会阻塞其它操作。
  Journaling 内部操作,短时间锁定所有数据库,所有的数据库共享一个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、 JavaScript 操作的并发
  Changed in version 2.4: The V8 JavaScript engine added in 2.4 allows multiple JavaScript operations to run at the same time.
  Prior to 2.4, a single mongod could only run a single JavaScript operation at once.
  8、事务
  Mongodb不支持多文档的事务
  9、Mongodb的隔离保证
  对于单一文档来说,读与写操作为原子操作,所以单一文档总是处于一致状态,这表示我们的读取操作,不会看到部分更新。
  但是对于多文档来说,mongodb不会对并发写进行隔离:
  因此,查询数据时,基于时间的数据快照是不能保证的。比如,我们获取不到查询过程当中已经删除的文档,或已经更新的文档。
  Reads can see data which may subsequently be rolled back in rare cases such as replica set failover or power loss.
  It does not mean that read operations can see documents in a partially written or otherwise inconsistent state.
  Other systems refer to these semantics asread uncommitted.
  参考:http://liyanblog.cn/articles/2013/01/15/1358218545015.html
  参考:http://docs.mongodb.org/manual/faq/concurrency/


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-626608-1-1.html 上篇帖子: mongodb 嵌套查询 下篇帖子: mongodb搭建
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表