# Topic discussion Phase 2
## Prepare
### 1. How does Redis avoid data loss? What is AOF?
架構:
How caould you lose data in Redis?
- RAM(Random-access memory) is volatile
What you can do to prevent it?
AOF (Append file only):
存到檔案(使用FS)
- with rewrite -> figure out 最精簡的,能還原data的log
- rebuild the AOF in the background
PROs:
更安全(durable):
append-only
rewrite in background
call fork() less frequently
CONs
append-only
RDB (snapshotting)
存到disk, compact **single-file** of Redis data
PROs:
RDB maximizes Redis **performances**
->faster restarts with big datasets
fork(), 用child來進行snapshotting; parent不必自己做I/O work
CONs:
larger chance of data loss
call fork() often; when fork()ing, Redis必須暫停serve clients
common problems: performance? risk of 空窗期?
IDC: No persistence
How to do?
PROs and CONS / 適合的情境
/*
Redis is not something you’d want to use as a persistent data storage. Think of it more like message bus or a caching layer
*/
### 2. Describe Garbage Collection and Memory Management in JS.
//
EXPERIMENT of:
- memory leak in node
- closure, bad programming(?)
- .
//
### 3. What are the difference between SQL and NoSQL? How to choose? What is ACID and CAP?
//
//
SQL vs NoSQL differ in:
Language
Scalability
Structure
Properties
Support and communities
ACID: atomicity, consistency, isolation, durability
CAP定理(CAP theorem),又被稱作布魯爾定理(Brewer's theorem)
討論分散式資料庫(distributed database)的架構下,CAP不可能同時滿足。By definition, CA-type不可能存在,所以通常討論集中在CP, AP兩種取向。
1. 一致性(Consistency) (等同於所有節點訪問同一份最新的數據副本)
2. 可用性(Availability)(每次請求都能獲取響應——但是不保證獲取的數據為最新數據)
3. 分區容錯性(Partition tolerance)(以實際效果而言,分區相當於對通信的時限要求。系統如果不能在時限內達成數據一致性,就意味著發生了分區的情況,必須就當前操作在C和A之間做出選擇[3]。)
NoSQL: (not SQL/not relational/Non-SQL/not only SQL)
pure document databases, key-value stores, wide-column databases, and graph databases
Database要素(必須?):
data integrity
proper structure
table relationships
and database keys, etc
SQL: 用tables來存資料,凸顯relation between tables: 用key來表現資料之間的關係。
強調高穩定性、資料之間的高連結性
scaling
B+tree $O((logdN)^2)$
NoSQL: 存object, better flexibility, 低連結性
scaling
hash table:O(1)
## Content
### 2.
JavaScript memory leaks are sneaky and could be challenging to localize because they can go unnoticed for some time. And even if your performance gets progressively worse, you will not see a thrown error on the browser while running the leaking app because it’s not an invalid code that causes memory leaks, but a logical flaw in it.
1. browser: closure
2. node: [global variable](https://vocus.cc/article/61176c17fd89780001942f1c)
[next, if any](https://www.gushiciku.cn/pl/pBqi/zh-tw)
### 3. NoSQL types intro
NoSQL的設計原則(理念)
#### Document Oriented Database
[MS link](https://docs.microsoft.com/zh-tw/azure/architecture/data-guide/big-data/non-relational-data)
In RDBMS...
以訂單資料為例
- XML/JSON/BSON...格式作為儲存資料的方式
- 可以使用key-value來access data
- 可利用key-value來實現index
----------
- 關聯式資料庫中主要以Table(表格)的方式儲存資料
- 文件導向資料主要以Collection(集合)的方式儲存資料
- ::Web HTML, 網頁資料
#### Key-value Oriented Database
每一筆資料包含一組鍵值(key-value),在key與value間建立映射關係,透過key可以直接存取value
鍵值資料庫以Bucket(桶)的方式儲存資料,資料被稱為key-value(鍵值)。
- 記錄檔系統、快取常用網頁
- Redis,
#### Column Oriented Database
- 分散式檔案系統、高擴充能力
- 以列儲存,將同一資料列儲存在一起。在一張大資料表中儲存很多行資料,每一行的結構同樣有一個主要Key值和任意數量的列欄位。
- 資料行系列資料庫的實際能力在於其建構疏鬆資料的反正規化方法,此方法源自於用來儲存資料的資料行導向方法。
-

- e.g. Cassandra: Wide Column Stores
- row key -> column key
- 這種模型可以理解為是一個二維的key-value儲存,即整個資料模型被定義成一個類似map<key1, map<key2,value>>的類型
----------
- 關聯式資料庫中主要以Table(表格)的方式儲存資料,而資料則稱為Row(列)
- 列式資料庫以Column Family(欄位群)的方式儲存資料,資料被稱為row(列)
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>

#### Graph Oriented Database
- 運用圖結構的概念來儲存資料,並運用圖結構相關演算法提高性能
- 以Node(節點)的方式儲存資料,資料被稱為Attribute(屬性)

Velocity(速度) - 可在一定的時間內完成查詢操作
Variety(多樣資料格式) - 網路上的資訊多半是半結構化、非結構化居多,無法事先定義資料類型,NoSQL的出現即是為了方便處理這類型的資料。
Volume(資料量) - 分散式運算架構可以處理大量資料
conclusion:
- NoSQL往往沒有固定的結構描述
- NoSQL往往不支援交易,要不然就是會限制交易的範圍
- 需求為本:
The Cassandra Query Language (CQL):CQL為Cassandra提供一個定義接近SQL的語法,採用table/row/column等相同定義的名稱去描述Cassandra的Data Model。
#### DB-Engines Ranking
[SQL](https://db-engines.com/en/ranking)
```cpp=
valgrind --vgdb=yes --vgdb-error=0 ./demo
target remote | vgdb
//b points
c
monitor leak_check full reachable any
```