gpt4 book ai didi

mysql - MongoDB 读/写锁

转载 作者:可可西里 更新时间:2023-11-01 07:20:12 26 4
gpt4 key购买 nike

我打算使用 nodejs 创建一个应用程序,用户可以对产品进行评分。据我所知,mongodb 中的锁与 mySql 中的锁不同。

我担心如果有 10 个用户同时对一个产品进行投票,Mongodb 无法处理,其他用户看到的结果不正确。

我看了类似的问题,但我还是很困惑! mongodb 是一个不错的选择还是我应该只使用 Mysql?我很感激你的帮助

最佳答案

Concurrency FAQ 中描述了锁定在 MongoDB 文档中。特别是:

MongoDB uses a readers-writer lock that allows concurrent reads access to a database but gives exclusive access to a single write operation.

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.

Locks are “writer greedy,” which means write locks have preference over reads. When both a read and write are waiting for a lock, MongoDB grants the lock to the write.

所以关于你的声明:

Mongodb can't handle it and the result other users see is not correct.

MongoDB 使用数据库范围的锁来防止在数据库某处执行写操作时进行读/写。看this JIRA item ,这将在未来减少为集合级别的锁(目前它只是在开发版本中)。此外,由于“编写器贪婪”方法,如果多个写入等待执行,它们将在任何读取之前完成,因此您的担忧不应该成为问题。

关于mysql - MongoDB 读/写锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26638744/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com