Top Banner
Cassandra 2.0 Michaël Figuière @mfiguiere
38
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: ChtiJUG - Cassandra 2.0

Cassandra 2.0

Michaël Figuière@mfiguiere

Page 2: ChtiJUG - Cassandra 2.0

©2012 DataStax

Speaker

Michaël Figuière

@mfiguiere

2

Page 3: ChtiJUG - Cassandra 2.0

©2012 DataStax

Ring Architecture

CassandraNode

Node

NodeNode

Node

Node

3

Page 4: ChtiJUG - Cassandra 2.0

©2012 DataStax

Ring Architecture

4

Node

Node Replica

Replica

NodeReplica

Page 5: ChtiJUG - Cassandra 2.0

©2012 DataStax

Linear Scalability

5

Client Writes/s by Node Count - Replication Factor = 3

Page 6: ChtiJUG - Cassandra 2.0

©2012 DataStax

Client / Server Communication

Client

Client

Client

Client

Node?

Node

6

Replica

Replica

Replica

Node

Page 7: ChtiJUG - Cassandra 2.0

©2012 DataStax

Client / Server Communication

Client

Client

Client

Client

Node

Node

Coordinator node:Forwards all R/W requeststo corresponding replicas

7

Replica

Replica

ReplicaNode

Page 8: ChtiJUG - Cassandra 2.0

©2012 DataStax

3 replicas

A A A

Time

8

Tunable Consistency

Page 9: ChtiJUG - Cassandra 2.0

©2012 DataStax

Write and wait for acknowledge from one node

Write ‘B’

B A A

9

Time

A A A

Tunable Consistency

Page 10: ChtiJUG - Cassandra 2.0

©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

Page 11: ChtiJUG - Cassandra 2.0

©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

Page 12: ChtiJUG - Cassandra 2.0

©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

Page 13: ChtiJUG - Cassandra 2.0

©2012 DataStax

Tunable Consistency

R = W = QUORUM

13

B A

B A

A A A

B

B

Time

QUORUM = (N / 2) + 1

Page 14: ChtiJUG - Cassandra 2.0

©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

Page 15: ChtiJUG - Cassandra 2.0

©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

Page 16: ChtiJUG - Cassandra 2.0

©2012 DataStax

Storage Engine

16

Memtable

CommitLog SSTable SSTableSSTableSSTable

Memtable

Page 17: ChtiJUG - Cassandra 2.0

©2012 DataStax

CQL Denormalized Model

17

Data duplicated over several tables

Cassandra comes with a Data Model abstraction made of denormalized, statically defined tables

Page 18: ChtiJUG - Cassandra 2.0

©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

Page 19: ChtiJUG - Cassandra 2.0

©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

Page 20: ChtiJUG - Cassandra 2.0

©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

Page 21: ChtiJUG - Cassandra 2.0

©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));

Page 22: ChtiJUG - Cassandra 2.0

©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));

Page 23: ChtiJUG - Cassandra 2.0

©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);

Page 24: ChtiJUG - Cassandra 2.0

©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);

Page 25: ChtiJUG - Cassandra 2.0

©2012 DataStax

Example: Video DB Application

25

CREATE TABLE video_rating ( videoid uuid, rating_counter counter, rating_total counter, PRIMARY KEY (videoid));

Page 26: ChtiJUG - Cassandra 2.0

©2012 DataStax

New Driver Architecture

26

DB API

CQL Native Protocol

CQL API OO API

Next Generation Driver

Page 27: ChtiJUG - Cassandra 2.0

©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

Page 28: ChtiJUG - Cassandra 2.0

©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]')");

Page 29: ChtiJUG - Cassandra 2.0

©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");}

Page 30: ChtiJUG - Cassandra 2.0

©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");}

Page 31: ChtiJUG - Cassandra 2.0

©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);

Page 32: ChtiJUG - Cassandra 2.0

©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’, ...)

Page 33: ChtiJUG - Cassandra 2.0

©2012 DataStax

Lightweight Transactions• Based on Paxos

• Fault tolerant

• Quorum based

• 4 round trips vs. 1 for normal updates

• State is durable

33

Page 34: ChtiJUG - Cassandra 2.0

©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]’;

Page 35: ChtiJUG - Cassandra 2.0

©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));

Page 36: ChtiJUG - Cassandra 2.0

©2012 DataStax

Automatic Paging

36

ClientPage 1

+ Paging State 1 Node

Node Replica

Replica

NodeReplica

Query1st Request

Page 37: ChtiJUG - Cassandra 2.0

©2012 DataStax

Automatic Paging

37

Client

Page 2 + Paging State 2

Node

Node Replica

Replica

NodeReplica

Query + Paging State 1

2nd Request

Page 38: ChtiJUG - Cassandra 2.0

@mfiguiere

blog.datastax.com

Stay Tuned!