MongoDB 基础系列十五:增删查改 CRUD 之 Query - Write Concern

前言

此篇博文是 Mongdb 基础系列之一;

本文为作者的原创作品,转载需注明出处;

简介

Write concern describes the level of acknowledgement requested from MongoDB for write operations to a standalone mongod or to replica sets or to sharded clusters. In sharded clusters, mongos instances will pass the write concern on to the shards.

Write concern 描述了当 MongoDB 在对一个独立的 mongod 或者是一个主从复制的集群或者是一个分片集群上执行写操作的时候响应请求的级别;

是的,看了官网上的简介,你应该是似懂非懂,应该说完全不懂,什么是 acknowledgement requested?响应请求什么东西?官网在它的开始处并没有进行更深一步的描述,而且连一笔带过的解释都省略了,这就是为什么官网难读,难懂的地方了;好吧,笔者在对官网的内容进行了整体的理解以后,在这里大致说明一下这个 acknowledgement requested 到底指的是什么东西;

从字面上的意思可以很直观的知道,它是一次响应式的请求,那相应的是什么呢?其实,在 MongoDB 写入一个 document 的时候,并不是直接写入 MongoDB 的数据库中,而是直接写入日志中;所以,这里的 acknowledgement 所对应的就是,该写入操作是否成功?若成功则响应,该响应既是指的该 acknowledgement;

Write Concern 描述

1
{ w: <value>, j: <boolean>, wtimeout: <number> }
  • w 选项表示,表示有多少个 mongod instances 需要根据指定的 tags 来执行请求响应;响应对应的就是日志成功写入与否?
  • j 是一个 boolean 值,表示是否对日志写入成功这个动作做响应;
  • wtimeout 写入响应超时时间设置;

w 选项

表示当前有多少个 mongod 实例需要根据指定的 tags 进行响应请求,

  • 直接通过数值进行设置
    通过w : <value>进行设置,
    w: 1
    如果是单机,那么只要单机响应陈宫写入请求即可;如果是集群,那么只要 primary node 响应请求即可;

    w: 0
    不需要响应成功写入请求;但是,仍然会返回一些异常信息,比如 socket 异常或者是网络异常等;

    特殊情况,如果设置为 w:0,但是又设置了 j:true,表示仍然要对集群中的 prmiary 或者是或者是单机的情况下进行写入反馈;这个其实就等价于 w: 1 了;

  • 设置为 majority

    Requests acknowledgement that write operations have propagated to the majority of voting nodes [2], including the primary.

    与 通过数字设置的方式不同,这里只需要大部分参与投票的节点通过,包含 primary,即可返回;

    After the write operation returns with a w: “majority” acknowledgement to the client, the client can read the result of that write with a “majority” readConcern.

    当写入操作成功,并返回了 w: “majority” 响应给客户端,然后客户端就可以发起一个 “majority” readConcern 的读请求;

  • <tag set>

    Requests acknowledgement that the write operations have propagated to a replica set member with the specified tag. See Acknowledgement Behavior for when mongod instances acknowledge the write.

j 选项

j 选项表示,当写入操作成功将数据写入日志 (journal) 以后然后响应;

If j: true, requests acknowledgement that the mongod instances, as specified in the w: <value>, have written to the on-disk journal. j: true does not by itself guarantee that the write will not be rolled back due to replica set primary failover.

注意,j: true并不会保证当 primary 失败以后的写入数据不会被回滚;

wtimeout 选项

This option specifies a time limit, in milliseconds, for the write concern. wtimeout is only applicable for w values greater than 1.

写入超时选项设置,当且仅当 w 的值大于 1 才会生效;

If you do not specify the wtimeout option and the level of write concern is unachievable, the write operation will block indefinitely. Specifying a wtimeout value of 0 is equivalent to a write concern without the wtimeout option.

注意,如果在使用 write concern 的时候没有设置 wtimeout 的话,那么当服务器没有响应的时候该写入操作将会永久的被阻塞;

响应行为

The w option and the j option determine when mongod instances acknowledge write operations.

单机

使用单机版的 mongod 的时候,无论写入操作是写入内存还是写入磁盘日志都会响应;下面这张表格描述了根据不同的配置对应的不同的响应行为;

集群

当写入操作写入时,无论当集群中所规定数量的 instances 写入到日志文件中还是写入内存中,都会有所对应的响应返回;