32
The new face of Cassandra Michaël Figuière @mfiguiere

ApacheCon NA 2013 - The New Face of Apache Cassandra

Embed Size (px)

DESCRIPTION

Cassandra has always been a highly performant distributed database, but not always simple to use though. Apache Cassandra 1.2 introduces some new features such as a Binary Protocol and Collections datatype that together with the now finalized CQL3 query language provide a new interface to communicate with Cassandra that dramatically shrink its learning curve and simplify its daily use while still relying on its highly scalable architecture and storage engine. This presentation will iterate over all these new features including an overview of CQL3 query language, a look at the new client architecture, and an update on data modeling best practices. Then we'll see how to implement an enterprise application using this new interface so that the audience can realize that a number of design principles are inspired from those commonly used with relational databases while some other entirely different, due to Cassandra partitioning approach.

Citation preview

Page 1: ApacheCon NA 2013 - The New Face of Apache Cassandra

The new face of Cassandra

Michaël Figuière@mfiguiere

Page 2: ApacheCon NA 2013 - The New Face of Apache Cassandra

©2012 DataStax

Speaker

Michaël Figuière

@mfiguiere

2

Page 3: ApacheCon NA 2013 - The New Face of Apache Cassandra

©2012 DataStax

Ring Architecture

CassandraNode

Node

NodeNode

Node

Node

3

Page 4: ApacheCon NA 2013 - The New Face of Apache Cassandra

©2012 DataStax

Ring Architecture

4

Node

Node Replica

Replica

NodeReplica

Page 5: ApacheCon NA 2013 - The New Face of Apache Cassandra

©2012 DataStax

Linear Scalability

5

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

Page 6: ApacheCon NA 2013 - The New Face of Apache Cassandra

©2012 DataStax

Client / Server Communication

Client

Client

Client

Client

Node?

Node

6

Replica

Replica

Replica

Node

Page 7: ApacheCon NA 2013 - The New Face of Apache Cassandra

©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: ApacheCon NA 2013 - The New Face of Apache Cassandra

©2012 DataStax

3 replicas

A A A

Time

8

Tunable Consistency

Page 9: ApacheCon NA 2013 - The New Face of Apache Cassandra

©2012 DataStax

Write and wait for acknowledge from one node

Write ‘B’

B A A

9

Time

A A A

Tunable Consistency

Page 10: ApacheCon NA 2013 - The New Face of Apache Cassandra

©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: ApacheCon NA 2013 - The New Face of Apache Cassandra

©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: ApacheCon NA 2013 - The New Face of Apache Cassandra

©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: ApacheCon NA 2013 - The New Face of Apache Cassandra

©2012 DataStax

Tunable Consistency

R = W = QUORUM

13

B A

B A

A A A

B

B

Time

QUORUM = (N / 2) + 1

Page 14: ApacheCon NA 2013 - The New Face of Apache Cassandra

©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: ApacheCon NA 2013 - The New Face of Apache Cassandra

©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: ApacheCon NA 2013 - The New Face of Apache Cassandra

©2012 DataStax

Column Family Data Model

16

123 main

address

[email protected]

email

Jonathan

name

TX

statejbellis

45 2nd st

address

[email protected]

email

Daria

name

CA

statedhutch

[email protected]

email

Eric

nameegilmore

Row Key Columns = List of K/V pairs sorted by Key

Page 17: ApacheCon NA 2013 - The New Face of Apache Cassandra

©2012 DataStax

Column Family Data Model

17

datastax dhutch egilmore mzcassiejbellis

egilmoredhutch

datastax mzcassieegilmore

Row Key Columns = List of K/V pairs sorted by Key

Page 18: ApacheCon NA 2013 - The New Face of Apache Cassandra

©2012 DataStax

Storage Engine

18

Memtable

CommitLog SSTable SSTableSSTableSSTable

Memtable

Page 19: ApacheCon NA 2013 - The New Face of Apache Cassandra

©2012 DataStax

CQL3 Denormalized Model

19

Data duplicated over several tables

CQL3 Query Language comes with a new Data Model abstraction made of denormalized, statically defined tables

Page 20: ApacheCon NA 2013 - The New Face of Apache Cassandra

©2012 DataStax

CQL3 Data Model

20

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 21: ApacheCon NA 2013 - The New Face of Apache Cassandra

©2012 DataStax

CQL3 Data Model

21

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 22: ApacheCon NA 2013 - The New Face of Apache Cassandra

©2012 DataStax

CQL3 Data Model

22

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 23: ApacheCon NA 2013 - The New Face of Apache Cassandra

©2012 DataStax

Current Drivers Architecture

23

Thrift API

Thrift RPC

CQL API

Thrift RPC

DB API

* This is a simplified view of drivers architecture. Details vary from one language to another.

OO API

Client Library CQL Driver

Page 24: ApacheCon NA 2013 - The New Face of Apache Cassandra

©2012 DataStax

New Drivers Architecture

24

DB API

CQL Native Protocol

CQL API OO API

* This is a simplified view of drivers architecture. Details vary from one language to another.

Next Generation Driver

Page 25: ApacheCon NA 2013 - The New Face of Apache Cassandra

©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

25

Page 26: ApacheCon NA 2013 - The New Face of Apache Cassandra

©2012 DataStax

Connect and Write

26

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 27: ApacheCon NA 2013 - The New Face of Apache Cassandra

©2012 DataStax

Read

27

ResultSet rs = session.execute("SELECT * FROM test");

List<Row> rows = rs.fetchAll(); for (Row row : rows) {

String userId = row.getString("user_id"); String name = row.getString("name"); String email = row.getString("email");}

Page 28: ApacheCon NA 2013 - The New Face of Apache Cassandra

©2012 DataStax

Asynchronous Read

28

ResultSet.Future 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 29: ApacheCon NA 2013 - The New Face of Apache Cassandra

©2012 DataStax

Object Mapping

29

public enum Gender {

@EnumValue("m") MALE, @EnumValue("f") FEMALE;}

@Table(name = "user")public class User { @PartitionKey @Column(name = "user_id") private String userId; private String name; private String email; private Gender gender;}

Page 30: ApacheCon NA 2013 - The New Face of Apache Cassandra

©2012 DataStax

Inheritance

30

@InheritanceValue("tv")public class TV extends Product {

private float size;}

@Table(name = "catalog")@Inheritance( subClasses = {Phone.class, TV.class}, column = "product_type")public abstract class Product {

@Column(name = "product_id") private String productId; private float price; private String vendor; private String model;

}

Page 31: ApacheCon NA 2013 - The New Face of Apache Cassandra

https://github.com/datastax/java-driver

Now Available!

Page 32: ApacheCon NA 2013 - The New Face of Apache Cassandra

@mfiguiere

blog.datastax.com

Stay Tuned!