1 Connection String
mongodb://mongodb-server-1:27017,mongodb-server-2:27017/admin
?replicaSet=test-replica-set
&ssl=false
&w=3
&j=true
&wtimeoutMS=5000
&readPreference=secondaryPreferred
&readConcernLevel=local
2 Read Write Config
2.1 Write Concern
写操作配置,作用于MongoDB Server
, 用于指示MongoDB Server
写入多少个成员后才会返回给MongoDB Client
。
{ w: <value>, j: <boolean>, wtimeout: <number> }
2.1.1 w
w
可用的选项如下:
选项 | 描述说明 |
---|---|
number | 指定固定的成员数(注意必须是数字,不让会被当做tag)。当成员数不够时,MongoDB Server 会返回CannotSatisfyWriteConcern 错误 |
majority | 写入大多数成员,MongoDB Server 自动决定写入的成员数数量 |
custom-write-concern-name | 写入到具有指定tag的成员中 |
成员数不够指定的w
的数量时的错误信息:
{
"code": 100,
"codeName": "CannotSatisfyWriteConcern",
"errmsg": "Not enough data-bearing nodes"
}
2.1.2 j
j
可用的选项如下:
选项 | 描述说明 |
---|---|
false | 日志写入内存后才返回 |
true | 日志写入硬盘后才返回 |
2.1.3 wtimeout
wtimeout
只适用于大于w>1
的时候,单位是ms
。比如:
db.test_collection.insert(
{ name: "abc"},
{ writeConcern: { w: 3 , wtimeout: 5000 } }
)
如果MongoDB Server
在5000ms
内没有完成写入3个成员的操作,那么就会返回一个错误,但是并不影响数据最终的写入。如果不指定wtimeout
, 则会一直阻塞到满足w=3
的时间点。
2.2 Read Preference
读操作配置,作用于MongoDB Client
, 指示MongoDB Client
使用哪些成员进行读取操作。可用的选项如下:
选项 | 描述说明 |
---|---|
primary | 默认值。使用primary成员进行所有的读取操作。 |
primaryPreferred | primary成员优先,当primary成员不可用时,读secondary成员。 |
secondary | 使用secondary成员进行所有的读取操作。 |
secondaryPreferred | secondary成员优先,当secondary成员不可用时,读primary成员。 |
nearest | MongoDB Client 根据网络延迟来决定使用哪个成员。 |
2.3 Read Concern Level
读操作配置,作用于MongoDB Server
, 用于指示MongoDB Server
返回满足指定的约束的数据。可用的选项如下:
级别 | 描述说明 |
---|---|
local | |
available | |
majority | |
linearizable | |
snapshot |
举例说明(简化假设如下):
Write0
之前的所有写操作已成功复制到所有成员。- Writeprev是
Write0
之前的前一个写。 Write0
之后没有发生其他写操作。
时间线图例。
时间 | 事件 | 最新的写 | 最新的w: "majority" 写? |
---|---|---|---|
t0 | Primary applies Write0 | Primary: Write0 Secondary1: Writeprev Secondary2: Writeprev | Primary: Writeprev Secondary1: Writeprev Secondary2: Writeprev |
t1 | Secondary1 applies Write0 | Primary: Write0 Secondary1: Write0 Secondary2: Writeprev | Primary: Writeprev Secondary1: Writeprev Secondary2: Writeprev |
t2 | Secondary2 applies Write0 | Primary: Write0 Secondary1: Write0 Secondary2: Write0 | Primary: Writeprev Secondary1: Writeprev Secondary2: Writeprev |
t3 | Primary得知Secondary1的同步成功,返回到MongoDB Client 。 | Primary: Write0 Secondary1: Write0 Secondary2: Write0 | Primary: Write0 Secondary1: Writeprev Secondary2: Writeprev |
t4 | Primary得知Secondary2的同步成功。 | Primary: Write0 Secondary1: Write0 Secondary2: Write0 | Primary: Write0 Secondary1: Writeprev Secondary2: Writeprev |
t5 | Secondary1得知Write0 已满足readConcernLevel=majority 的要求。 | Primary: Write0 Secondary1: Write0 Secondary2: Write0 | Primary: Write0 Secondary1: Write0 Secondary2: Writeprev |
t6 | Secondary2得知Write0 已满足readConcernLevel=majority 的要求。 | Primary: Write0 Secondary1: Write0 Secondary2: Write0 | Primary: Write0 Secondary1: Write0 Secondary2: Write0 |
2.3.1 local
local读取 | 时间 | 数据 |
---|---|---|
Primary | t0后 | Write0 |
Secondary1 | t1前 | Writeprev |
Secondary1 | t1后 | Write0 |
Secondary2 | t2前 | Writeprev |
Secondary2 | t2后 | Write0 |
2.3.2 available
available读取 | 时间 | 数据 |
---|---|---|
Primary | t0后 | Write0 |
Secondary1 | t1前 | Writeprev |
Secondary1 | t1后 | Write0 |
Secondary2 | t2前 | Writeprev |
Secondary2 | t2后 | Write0 |
2.3.3 majority
majority读取 | 时间 | 数据 |
---|---|---|
Primary | t3前 | Writeprev |
Primary | t3后 | Write0 |
Secondary1 | t5前 | Writeprev |
Secondary1 | t5后 | Write0 |
Secondary2 | t6前 | Writeprev |
Secondary2 | t6后 | Write0 |
3 Reference
- https://docs.mongodb.com/manual/reference/glossary/
- https://docs.mongodb.com/manual/reference/command/nav-crud/
- https://docs.mongodb.com/manual/reference/connection-string/
- https://docs.mongodb.com/manual/reference/write-concern/
- https://docs.mongodb.com/manual/reference/read-concern/
- https://docs.mongodb.com/manual/reference/method/js-replication/
3.1 Replica Set
- https://docs.mongodb.com/manual/replication/
- https://docs.mongodb.com/manual/core/replica-set-hidden-member/
- https://docs.mongodb.com/manual/core/replica-set-sync/
- https://docs.mongodb.com/manual/core/replica-set-oplog/
- https://docs.mongodb.com/manual/applications/replication/
- https://docs.mongodb.com/manual/core/replica-set-write-concern/
- https://docs.mongodb.com/manual/core/read-preference/
- https://docs.mongodb.com/manual/reference/read-concern-majority/