Brief introduction to Cassandra 【Cassandra簡介】
From wikipedia https://en.wikipedia.org/wiki/CAP_theorem
In theoretical computer science, the CAP theorem, also named Brewer‘s theorem after computer scientist Eric Brewer, states that it is impossible for a distributed data store to simultaneously provide more than two out of the following three guarantees:[1]
[2][3]
Consistency Availability Partition tolerance Every read receives the most recent write or an error Every request receives a (non-error) response – without guarantee that it contains the most recent write The system continues to operate despite an arbitrary number of messages being dropped (or delayed) by the network between nodes In other words, the CAP Theorem states that in the presence of a network partition, one has to choose between consistency and availability. Note that consistency as defined in the CAP Theorem is quite different from the consistency guaranteed in ACID database transactions.
維基上對CAP有簡單的解釋
C 一致性 讀請求要麽讀到最新值,要麽觸發錯誤
A 可用性 每個請求都能收到一個非錯誤的相應,不保證讀取的一定是最新值
P 分區容忍性 系統可在網絡節點之間丟包或者包延遲的情況下繼續運行
CAP理論說,分布式系統無法三者兼顧,須有舍得。
As a distributed system, multiple nodes are required to balance and handle growing loads, which is why P is usually required as default. Since the module has more than one nodes, scacrificing either consistency or 100% availability is unavoidable according to the theory.
作為分布式系統,為了負載均衡以及應對日益增長的訪問壓力,分區容忍性通常都是需要的。根據CAP理論,我們只好在一致性與100%可用之間做出一定的犧牲。
ACID is typical property of traditional relational databse.
ACID是傳統關系型數據庫的特點。
Atomicity Atomicity requires that each transaction be "all or nothing": if one part of the transaction fails, then the entire transaction fails, and the database state is left unchanged
原子性 一個事務中所有操作都必須全部完成,要麽全部不完成。
Consistency The consistency property ensures that any transaction will bring the database from one valid state to another.
一致性 事務將把數據庫從一個狀態帶入另一個狀態。
Isolation The isolation property ensures that the concurrent execution of transactions results in a system state that would be obtained if transactions were executed sequentially, i.e., one after the other.
隔離性 各種事務並發執行會感覺到和串行無差。
Durability The durability property ensures that once a transaction has been committed, it will remain so, even in the event of power loss, crashes, or errors.
持久性 一旦事務完成,宕機,斷電都不會影響
Another consistent model is called BASE ( Basically Available, Soft state, Eventual consistency)
另一個與之相對的一致性模型叫做BASE,基本可用,柔性事務,最終一致性。
Cassandra is NoSQL database. Directed by CAP theory, it sacfrices some features of traditional database like table joins and acid transactions. But the trade off may worth it. Cassandra handles high incoming data velocity, supports very large data volumes, and no single point of failure.
Cassandra是非關系型數據庫。在CAP理論的指導下,它犧牲掉了傳統數據庫的一些特性,比如聯合查詢,ACID事務。然而Cassandra可以處理較高的數據寫入速率,支持大數據存儲,也沒有單點失敗的問題,可謂有得有失。
Cassandra is well known for its impressive performance in both reading and writing data.
Data is written to Cassandra in a way that provides both full data durability and high performance. Data written to a Cassandra node is first recorded in an on-disk commit log and then written to a memory-based structure called a memtable. When a memtable’s size exceeds a configurable threshold, the data is written to an immutable file on disk called an SSTable. Buffering writes in memory in this way allows writes always to be a fully sequential operation, with many megabytes of disk I/O happening at the same time, rather than one at a time over a long period. This architecture gives Cassandra its legendary write performance.
寫入Cassandra節點的數據會首先寫入commit日誌,之後寫入內存中的memtable。當memtable大到超過閾值,數據會寫入磁盤上的SSTable持久化文件。緩沖寫使得寫操作總是順序操作,讓許多兆的IO讀寫能同時發生。這種架構給予了Cassandra傳奇的寫入速度。
Sharding is a familiar term for dba who manages relational database cluster. Cassandra automatically distributes and maintains data across a cluster thus freeing developers and archtects. Canssandra has an internal component called partioner. It take a row‘s primary key, calculates a token and then assign to a predicatable node.
對於關系型數據庫的DBA,分片是一個很常聽到的術語。而Cassandra能夠自動的分布和維護集群的數據,這對開發者和構架師都是一個好消息。Cassandra 內部有一個叫做partioner的組件,它根據數據行的主鍵,計算出token,然後以可預測的方式分配給數據節點。
Cassandra introduced CQL as query language.
Cassandra使用CQL來進行數據查詢
CREATE TABLE test.demo ( id int, name text, PRIMARY KEY (id) )
insert into test.demo (id, name) values (1, ‘hello‘)
select * from test.demo
Result:
How to use cassandra in Java?
Java裏面怎麽使用Cassandra呢? 參見下面代碼片段 (註意紅色部分,Cassandra支持給插入的數據加入TTL)
Statement statement = QueryBuilder.insertInto("test","demo")
.value("id", 1)
.value("name", "hello")
.using(QueryBuilder.ttl(MSG_TTL)); //We can set ttl for a row
session.execute(statement);
Here we go a little deeper into Cassandra data storage. The first element in the PRIMARY KEY is what we call a partition key. In this case, it‘s "id".
Partition key is responsible for data distribution in the cluster. Cassandra use consistent hashing to distribute data so that data reorganization when nodes are added or removed can be minimized.
Data center below has four nodes. Each is responsible for a hash range. Suddenly, node B run into failure, data should be stored in B will be relocated to A instead.
這裏我們稍微多談談Cassandra的數據存儲。 主鍵的第一部分叫做分區鍵,這個例子中是id。分區鍵用於數據分區,具體地,Cassandra通過一致性哈希來進行分區,這還可以減少節點增減帶來的數據轉移。
如下圖所示的數據中心,有4個節點,每個節點負責一段哈希範圍。突然,節點B宕機,本應該存儲在B的數據就會轉移去節點A,而不是到所有的節點去。
In practice, we can add many more nodes (far larger than real nodes) to the ring. The additional virtual nodes can be mapped to the real nodes so that data can be distributed evenly among nodes.
實際使用中,我們可以增加許多的虛擬節點到哈希環上。然後把多余的虛擬節點映射到真實節點上,這樣數據的分布可以更加均勻。
Brief introduction to Cassandra 【Cassandra簡介】