Cassandra 2.0 Michaël Figuière @mfiguiere
Jan 27, 2015
Cassandra 2.0
Michaël Figuière@mfiguiere
©2012 DataStax
Speaker
Michaël Figuière
@mfiguiere
2
©2012 DataStax
Ring Architecture
CassandraNode
Node
NodeNode
Node
Node
3
©2012 DataStax
Ring Architecture
4
Node
Node Replica
Replica
NodeReplica
©2012 DataStax
Linear Scalability
5
Client Writes/s by Node Count - Replication Factor = 3
©2012 DataStax
Client / Server Communication
Client
Client
Client
Client
Node?
Node
6
Replica
Replica
Replica
Node
©2012 DataStax
Client / Server Communication
Client
Client
Client
Client
Node
Node
Coordinator node:Forwards all R/W requeststo corresponding replicas
7
Replica
Replica
ReplicaNode
©2012 DataStax
3 replicas
A A A
Time
8
Tunable Consistency
©2012 DataStax
Write and wait for acknowledge from one node
Write ‘B’
B A A
9
Time
A A A
Tunable Consistency
©2012 DataStax
R + W < N
Read waiting for one node to answer
B A A
10
B A A
A A A
Write and wait for acknowledge from one node
Time
Tunable Consistency
©2012 DataStax
R + W = N
11
B B A
B A
A A A
B
Write and wait for acknowledges from two nodes
Read waiting for one node to answer
Tunable ConsistencyTime
©2012 DataStax
Tunable Consistency
R + W > N
12
B A
B A
A A A
B
B
Write and wait for acknowledges from two nodes
Read waiting for two nodes to answer
Time
©2012 DataStax
Tunable Consistency
R = W = QUORUM
13
B A
B A
A A A
B
B
Time
QUORUM = (N / 2) + 1
©2012 DataStax
Request Path
1
2
2
2
3
3
3
4
14
Client
Client
Client
Client
Node
Node
Node
Replica
Replica
Replica
Coordinator node
©2012 DataStax
Multi-Datacenter
Node6
Node1
Node4
Node5
Node2
Node3
15
Node 1
Node 2
Datacenter A
Node 3
Node 4
Datacenter B
Node 5
Node 6
Datacenter C
©2012 DataStax
Storage Engine
16
Memtable
CommitLog SSTable SSTableSSTableSSTable
Memtable
©2012 DataStax
CQL Denormalized Model
17
Data duplicated over several tables
Cassandra comes with a Data Model abstraction made of denormalized, statically defined tables
©2012 DataStax
Cassandra Data Model
18
gmason
user_id
1735
tweet_id
phenry
author
Give me liberty or give me death
body
PartitionKey
gmason 1742 gwashington I chopped down the cherry tree
ahamilton 1767 jadams A government of laws, not men
ahamilton 1794 gwashington I chopped down the cherry tree
ClusteringKey
Timeline Table
©2012 DataStax
Cassandra Data Model
19
gmason
user_id
1735
tweet_id
phenry
author
Give me liberty or give me death
body
gmason 1742 gwashington I chopped down the cherry tree
ahamilton 1767 jadams A government of laws, not men
ahamilton 1794 gwashington I chopped down the cherry tree
Timeline Table
CREATE TABLE timeline ( user_id varchar, tweet_id timeuuid, author varchar, body varchar, PRIMARY KEY (user_id, tweet_id));
CQL
©2012 DataStax
Cassandra Data Model
20
gmason
user_id tweet_id
phenry
author
Give me liberty or give me death
body
gmason gwashington I chopped down the cherry tree
ahamilton jadams A government of laws, not men
ahamilton gwashington I chopped down the cherry tree
gwashington
[1735, author]
I chopped down the...
[1735, body]
phenry
[1742, author]
Give me liberty or give...
[1742, body]gmason
gwashington
[1767, author]
I chopped down the...
[1767, body]
jadams
[1794, author]
A government of laws...
[1794, body]ahamilton
Timeline Table
Timeline Physical Layout
1735
1742
1767
1794
©2012 DataStax
Example: Video DB Application
21
CREATE TABLE users ( username varchar, firstname varchar, lastname varchar, email list<varchar>, password varchar, created_date timestamp, PRIMARY KEY (username));
©2012 DataStax
Example: Video DB Application
22
CREATE TABLE videos ( videoid uuid, videoname varchar, username varchar, description varchar, location map<varchar,varchar>, tags set<varchar>, upload_date timestamp, PRIMARY KEY (videoid));
©2012 DataStax
Example: Video DB Application
23
CREATE TABLE comments_by_video ( videoid uuid, username varchar, comment_ts timeuuid, comment varchar, PRIMARY KEY (videoid,comment_ts,username)) WITH CLUSTERING ORDER BY (comment_ts DESC, username ASC);
©2012 DataStax
Example: Video DB Application
24
CREATE TABLE comments_by_user ( username varchar, videoid uuid, comment_ts timeuuid, comment varchar, PRIMARY KEY (username,comment_ts,videoid)) WITH CLUSTERING ORDER BY (comment_ts DESC, videoid ASC);
©2012 DataStax
Example: Video DB Application
25
CREATE TABLE video_rating ( videoid uuid, rating_counter counter, rating_total counter, PRIMARY KEY (videoid));
©2012 DataStax
New Driver Architecture
26
DB API
CQL Native Protocol
CQL API OO API
Next Generation Driver
©2012 DataStax
DataStax Java Driver• Reference Implementation
• Asynchronous architecture based on Netty
• Prepared Statements Support
• Automatic Fail-over
• Node Discovery
• Cassandra Tracing Support
• Tunable policies• LoadBalancingPolicy• ReconnectionPolicy• RetryPolicy
27
©2012 DataStax
Connect and Write
28
Cluster cluster = new Cluster.builder() .addContactPoints("10.0.0.1", "10.0.0.2") .build();
Session session = cluster.connect("myKeyspace");
session.execute( "INSERT INTO user (user_id, name, email) VALUES (12345, 'johndoe', '[email protected]')");
©2012 DataStax
Read
29
ResultSet rs = session.execute("SELECT * FROM test");
List<Row> rows = rs.all(); for (Row row : rows) {
String userId = row.getString("user_id"); String name = row.getString("name"); String email = row.getString("email");}
©2012 DataStax
Asynchronous Read
30
ResultSetFuture future = session.executeAsync("SELECT * FROM test");
for (Row row : future.get()) {
String userId = row.getString("user_id"); String name = row.getString("name"); String email = row.getString("email");}
©2012 DataStax
Read with Callback
31
final ResultSetFuture future = session.executeAsync("SELECT * FROM test");
future.addListener(new Runnable() { public run() { for (Row row : future.get()) {
String userId = row.getString("user_id"); String name = row.getString("name"); String email = row.getString("email"); } }}, executor);
©2012 DataStax
Lightweight Transactions
32
Session 2SELECT * FROM usersWHERE username = ’jbellis’[empty resultset]INSERT INTO users (...)VALUES (’jbellis’, ...)
Session 1SELECT * FROM usersWHERE username = ’jbellis’[empty resultset]INSERT INTO users (...)VALUES (’jbellis’, ...)
©2012 DataStax
Lightweight Transactions• Based on Paxos
• Fault tolerant
• Quorum based
• 4 round trips vs. 1 for normal updates
• State is durable
33
©2012 DataStax
Lightweight Transactions
34
INSERT INTO USERS (username, email, ...)VALUES (‘jbellis’, ‘[email protected]’, ... )IF NOT EXISTS;
UPDATE USERSSET email = ’[email protected]’, ...WHERE username = ’jbellis’IF email = ’[email protected]’;
©2012 DataStax
Paging with Cassandra 1.2
35
SELECT *FROM timelineWHERE (user_id = :last_keyAND tweet_id > :last_tweet)OR token(user_id) > token(:last_key)LIMIT 100
CREATE TABLE timeline (user_id uuid,tweet_id timeuuid,tweet_author uuid,tweet_body text,PRIMARY KEY (user_id, tweet_id));
©2012 DataStax
Automatic Paging
36
ClientPage 1
+ Paging State 1 Node
Node Replica
Replica
NodeReplica
Query1st Request
©2012 DataStax
Automatic Paging
37
Client
Page 2 + Paging State 2
Node
Node Replica
Replica
NodeReplica
Query + Paging State 1
2nd Request
@mfiguiere
blog.datastax.com
Stay Tuned!