Cassandra 設(shè)計(jì)用來(lái)處理多節(jié)點(diǎn)大型數(shù)據(jù)工作負(fù)載,系統(tǒng)中沒(méi)有單點(diǎn),Cassandra 采用peer-to-peer架構(gòu),數(shù)據(jù)在所有節(jié)點(diǎn)之間分發(fā)。
- cluster中所有node具有相同的角色。每個(gè)node互相獨(dú)立,同時(shí)在內(nèi)部又互相溝通。
- cluster中所有node都可以處理讀寫(xiě)請(qǐng)求,而不用管數(shù)據(jù)具體在哪兒。
- 如果一個(gè)node掛了,其它node可以處理讀寫(xiě)請(qǐng)求。
1. node之間的溝通
Cassandra 各node之間采用 gossip 協(xié)議進(jìn)行溝通,gossip 進(jìn)程每秒與集群中最多三個(gè)node交換信息,信息包括node自身的信息以及與該node交換過(guò)信息的node的信息,這樣,所有node都可以很快獲取集群中所有的node信息。
2. Data distribution and replication
Cassandra是一個(gè)分區(qū)的按行存儲(chǔ)的數(shù)據(jù)庫(kù)(partitioned row store database)。在Cassandra中,數(shù)據(jù)以table的形式組織起來(lái),primary key唯一標(biāo)記一行,同時(shí),primary key也決定了數(shù)據(jù)行存儲(chǔ)的node,replication是數(shù)據(jù)行的備份,Cassandra將數(shù)據(jù)復(fù)制到多個(gè)node上,從而實(shí)現(xiàn)高可用和容錯(cuò)。
下面分data distribution 和 replication兩個(gè)方面進(jìn)行闡述,其中distribution說(shuō)明將數(shù)據(jù)分發(fā)到哪個(gè)node,replication說(shuō)明如何備份。
2.1 Data distribution
partitioner 決定了數(shù)據(jù)是怎樣在集群中分布的。簡(jiǎn)單的講,partitioner是一個(gè)函數(shù),根據(jù)partition key產(chǎn)生一個(gè)唯一標(biāo)記一行的token,然后,這行數(shù)據(jù)根據(jù)token分發(fā)到集群中的節(jié)點(diǎn),通常,partitioner是Hash函數(shù)。Cassandra提供了三種partitioner,包括Murmur3Partitioner(default)、RandomPartitioner、ByteOrderedPartitioner。(為更好理解這部分,可以參考一致性哈希)。
2.2 Data replication
Cassandra將數(shù)據(jù)存儲(chǔ)到多個(gè)node以實(shí)現(xiàn)高可用和容錯(cuò),replication策略決定了將數(shù)據(jù)備份到哪些節(jié)點(diǎn)。
replication factor 指數(shù)據(jù)在整個(gè)cluster中的份數(shù),如果replication factor等于1,則數(shù)據(jù)在整個(gè)集群中僅存在一份,此時(shí),如果存儲(chǔ)數(shù)據(jù)的node出現(xiàn)故障,那么數(shù)據(jù)就丟失了。在實(shí)踐中,replication factor 應(yīng)該不超過(guò)cluster中node的數(shù)量。
Cassandra提供了兩種replication 策略:
- SimpleStrategy: 僅適用于單datacenter 單 rack。根據(jù)partitioner存儲(chǔ)第一份replica,然后在順時(shí)針?lè)较虻南乱粋€(gè)node上存放下一份replica(不考慮網(wǎng)絡(luò)拓?fù)湫畔ⅲ?/li>
- NetworkTopologyStrategy: 可以方便的擴(kuò)展到多datacenter,推薦使用,同時(shí),NetworkTopologyStrategy盡量避免將數(shù)據(jù)存儲(chǔ)到相同的rack上。
3. Snitches
snitch 決定了node屬于哪個(gè)datacenter的哪個(gè)rack??梢杂糜诟嬷狢assandra集群網(wǎng)絡(luò)拓?fù)湫畔?,以?shí)現(xiàn)高效的請(qǐng)求路由與分發(fā)、備份數(shù)據(jù)。
主要的snitch包括:
- dynamic snitching
- SimpleSnitch
- RackInferringSnitch
- PropertyFileSnitch
- GossipingPropertyFileSnitch
- ...
4. 總結(jié)
本文主要介紹Cassandra的架構(gòu)、數(shù)據(jù)distribution 與 replication,下一章介紹Cassandra的內(nèi)部信息,包括存儲(chǔ)引擎、Cassandra讀寫(xiě)、數(shù)據(jù)一致性等。