# MongoDB教育訓練-20211223-02

- 資料先寫入記憶體 ，
- checkpoint =&gt; 一分鐘( or 資料到達大小) 髒數據寫入DB
- journal log 60ms 寫入 db
- db 最多錯失60 ms 資料
- "OK - committed" =&gt; 寫到哪裡？ write concerns 決定
- 資料遺失的時候？我會知曉？
- knowingly log =&gt; 已知的寫入失敗
- unknowingly log =&gt; 未知的寫入失敗

[![image-1640237795605.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/51ki1KDJEyWkz1kV-image-1640237795605.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/51ki1KDJEyWkz1kV-image-1640237795605.png)

[![image-1640238366642.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/48LANg6oh5A8V3c4-image-1640238366642.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/48LANg6oh5A8V3c4-image-1640238366642.png)

[![image-1640238415549.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/LLjDAcWlqBdgN3Vs-image-1640238415549.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/LLjDAcWlqBdgN3Vs-image-1640238415549.png)

[![image-1640238497608.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/mamGXqUk0sI8jwSt-image-1640238497608.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/mamGXqUk0sI8jwSt-image-1640238497608.png)

當資訊不同步，server 必須 rollback 確保資訊一致 =&gt; { x: 100} (移除)

[![image-1640238562747.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/0mRZdFeDnlJE5hoo-image-1640238562747.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/0mRZdFeDnlJE5hoo-image-1640238562747.png)

[![image-1640238622753.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/BZdfu2R1oiKJs0dI-image-1640238622753.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/BZdfu2R1oiKJs0dI-image-1640238622753.png)

The case for Majority writes

in the previeous scenario:

date is writtten to the primary

primary acknowledges write to application

primary dies before secondary reads data

必須所以資料到達大多數節點(majoruty commit point)，才能稱之為持久化[![image-1640238761360.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/ztBPvEq8CD8CwrRx-image-1640238761360.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/ztBPvEq8CD8CwrRx-image-1640238761360.png)

[![image-1640238911776.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/bHKAnFuE7H5KNaY8-image-1640238911776.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/bHKAnFuE7H5KNaY8-image-1640238911776.png)

### 寫的關注度Write Concern

- OK, commitresd 意義
- (w: 到達多少個節點)(j: 如何才算寫成功 0:記憶體, 1:硬碟)
- w:0 不推薦
- w:1, j:1
- w: "majority" (j: 1)
- w:3 無意義(假設叢集有3節點，萬一1節點丟失，無法滿足)

[![image-1640239010111.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/UKzSJDUyJPnSJ7I8-image-1640239010111.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/UKzSJDUyJPnSJ7I8-image-1640239010111.png)

```shell
var wc = { w: 0 }
totalrecs = 10000
batchsize = 1
nbatches = totalrecs / batchsize
var start = new Date()
for(x=0;x<nbatches;x++) {
	recs = []
	for(y=0;y<batchsize;y++) {recs.push({a:1,b:"hello"})}
	db.test.insertMany(recs,{writeConcern: wc})
}

var end=new Date()
print(`${end-start} milliseconds`)
```

[![image-1640239636788.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/D2tSUBqCfLK9n7Dt-image-1640239636788.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/D2tSUBqCfLK9n7Dt-image-1640239636788.png)

### 讀的關注度 Read Concerns 

RDBS 隔離等級概念

Read local 主節點損毀才是髒讀

Read majority 讀舊一點的資料

Read snapshot :

Read Linearizable: 讀取時會確認資料是否到達大多數節點

[![image-1640240583240.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/iJFj4Cs5o6NUaKKk-image-1640240583240.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/iJFj4Cs5o6NUaKKk-image-1640240583240.png)

[![image-1640239853753.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/2kPdvY1LGbZGS2Ae-image-1640239853753.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/2kPdvY1LGbZGS2Ae-image-1640239853753.png)

### 讀取資料會從哪個節點讀？

- Read from nearest Geographically (從最近的節點 ping 值，最小的)
- Read from a specific set of servers (設定標記好的 tag server )

[![image-1640240657381.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/FaHVQBXp6O5EAO0M-image-1640240657381.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/FaHVQBXp6O5EAO0M-image-1640240657381.png)

### <span style="color: #ff0000;">非必要不使用</span>

[![image-1640240968077.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/7GlKL03YZFWX7du3-image-1640240968077.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/7GlKL03YZFWX7du3-image-1640240968077.png)

[![image-1640241039739.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/BjmFLZk7hGaHyjg9-image-1640241039739.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/BjmFLZk7hGaHyjg9-image-1640241039739.png)