Timetombs

泛义的工具是文明的基础,而确指的工具却是愚人的器物

66h / 118a
,更新于 2024-11-10T12:35:26Z+08:00 by   330589a

[MongoDB] Best Practice

版权声明 - CC BY-NC-SA 4.0

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> }
Write Concern : w=majority

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 Server5000ms内没有完成写入3个成员的操作,那么就会返回一个错误,但是并不影响数据最终的写入。如果不指定wtimeout, 则会一直阻塞到满足w=3的时间点。

2.2 Read Preference

读操作配置,作用于MongoDB Client, 指示MongoDB Client使用哪些成员进行读取操作。可用的选项如下:

选项描述说明
primary默认值。使用primary成员进行所有的读取操作。
primaryPreferredprimary成员优先,当primary成员不可用时,读secondary成员。
secondary使用secondary成员进行所有的读取操作。
secondaryPreferredsecondary成员优先,当secondary成员不可用时,读primary成员。
nearestMongoDB Client根据网络延迟来决定使用哪个成员。
Read Preference

2.3 Read Concern Level

读操作配置,作用于MongoDB Server, 用于指示MongoDB Server返回满足指定的约束的数据。可用的选项如下:

级别描述说明
local
available
majority
linearizable
snapshot

举例说明(简化假设如下):

  1. Write0之前的所有写操作已成功复制到所有成员。
  2. Writeprev是Write0之前的前一个写。
  3. Write0之后没有发生其他写操作。

时间线图例。
Read Concern Write Timeline

时间事件最新的写最新的w: "majority"写?
t0Primary applies Write0Primary: Write0
Secondary1: Writeprev
Secondary2: Writeprev
Primary: Writeprev
Secondary1: Writeprev
Secondary2: Writeprev
t1Secondary1 applies Write0Primary: Write0
Secondary1: Write0
Secondary2: Writeprev
Primary: Writeprev
Secondary1: Writeprev
Secondary2: Writeprev
t2Secondary2 applies Write0Primary: Write0
Secondary1: Write0
Secondary2: Write0
Primary: Writeprev
Secondary1: Writeprev
Secondary2: Writeprev
t3Primary得知Secondary1的同步成功,返回到MongoDB ClientPrimary: Write0
Secondary1: Write0
Secondary2: Write0
Primary: Write0
Secondary1: Writeprev
Secondary2: Writeprev
t4Primary得知Secondary2的同步成功。Primary: Write0
Secondary1: Write0
Secondary2: Write0
Primary: Write0
Secondary1: Writeprev
Secondary2: Writeprev
t5Secondary1得知Write0已满足readConcernLevel=majority的要求。Primary: Write0
Secondary1: Write0
Secondary2: Write0
Primary: Write0
Secondary1: Write0
Secondary2: Writeprev
t6Secondary2得知Write0已满足readConcernLevel=majority的要求。Primary: Write0
Secondary1: Write0
Secondary2: Write0
Primary: Write0
Secondary1: Write0
Secondary2: Write0

2.3.1 local

local读取时间数据
Primaryt0后Write0
Secondary1t1前Writeprev
Secondary1t1后Write0
Secondary2t2前Writeprev
Secondary2t2后Write0

2.3.2 available

available读取时间数据
Primaryt0后Write0
Secondary1t1前Writeprev
Secondary1t1后Write0
Secondary2t2前Writeprev
Secondary2t2后Write0

2.3.3 majority

majority读取时间数据
Primaryt3前Writeprev
Primaryt3后Write0
Secondary1t5前Writeprev
Secondary1t5后Write0
Secondary2t6前Writeprev
Secondary2t6后Write0

3 Reference

  1. https://docs.mongodb.com/manual/reference/glossary/
  2. https://docs.mongodb.com/manual/reference/command/nav-crud/
  3. https://docs.mongodb.com/manual/reference/connection-string/
  4. https://docs.mongodb.com/manual/reference/write-concern/
  5. https://docs.mongodb.com/manual/reference/read-concern/
  6. https://docs.mongodb.com/manual/reference/method/js-replication/

3.1 Replica Set

  1. https://docs.mongodb.com/manual/replication/
  2. https://docs.mongodb.com/manual/core/replica-set-hidden-member/
  3. https://docs.mongodb.com/manual/core/replica-set-sync/
  4. https://docs.mongodb.com/manual/core/replica-set-oplog/
  5. https://docs.mongodb.com/manual/applications/replication/
  6. https://docs.mongodb.com/manual/core/replica-set-write-concern/
  7. https://docs.mongodb.com/manual/core/read-preference/
  8. https://docs.mongodb.com/manual/reference/read-concern-majority/