cassandra community webinar - introduction to apache cassandra 1.2

93
CASSANDRA COMMUNITY WEBINARS APRIL 2013 INTRODUCTION TO APACHE CASSANDRA 1.2 Aaron Morton Apache Cassandra Committer, Data Stax MVP for Apache Cassandra @aaronmorton www.thelastpickle.com Licensed under a Creative Commons Attribution-NonCommercial 3.0 New Zealand License

Upload: aaronmorton

Post on 27-Jun-2015

572 views

Category:

Technology


2 download

DESCRIPTION

Slides from the April 25th presentation http://www.youtube.com/watch?v=zFCjekgK7ZY

TRANSCRIPT

Page 1: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

CASSANDRA COMMUNITY WEBINARS APRIL 2013

INTRODUCTION TO APACHE CASSANDRA 1.2

Aaron MortonApache Cassandra Committer, Data Stax MVP for Apache Cassandra

@aaronmortonwww.thelastpickle.com

Licensed under a Creative Commons Attribution-NonCommercial 3.0 New Zealand License

Page 2: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Cassandra Summit 2013June 11 & 12San Francisco

Use SFSummit25 for 25% off

Page 3: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Cassandra Summit 2013DataStax Ac*ademy

Free certification during the summit.

Page 4: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

OverviewThe ClusterThe Node

The Data Model

Page 5: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Cassandra- Started at Facebook- Open sourced in 2008- Top Level Apache project

since 2010.

Page 6: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Used by...Netflix, Twitter, Reddit,

Rackspace...

Page 7: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Inspiration- Google Big Table (2006) - Amazon Dynamo (2007)

Page 8: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Why Cassandra?- Scale- Operations- Data Model

Page 9: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

OverviewThe ClusterThe Node

The Data Model

Page 10: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Store ‘foo’ key with Replication Factor 3.Node 1 - 'foo'

Node 2 - 'foo'Node 4

Node 3 - 'foo'

Page 11: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Consistent Hashing.- Evenly map keys to nodes- Minimise key movements

when nodes join or leave

Page 12: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Partitioner.RandomPartitioner transforms Keys to Tokens

using MD5.(Default pre version 1.2.)

Page 13: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Partitioner.Murmur3Partitioner transforms Keys to Tokens

using Murmur3.(Default in version 1.2.)

Page 14: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Keys and Tokens?

token 0 99

key 'fop' 'foo'

10 90

Page 15: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Token Ring.

'foo'token: 90

'fop'token: 10

99 0

Page 16: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Token Ranges pre v1.2.Node 1token: 0

Node 2token: 25

Node 4token: 75

Node 3token: 50

1-2576-0

Page 17: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Token Ranges with Virtual Nodes in v1.2.Node 1

Node 2

Node 3

Node 4

Page 18: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Locate Token Range.Node 1

Node 2

Node 3

Node 4

'foo'token: 90

Page 19: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Replication Strategy selects Replication Factor number of

nodes for a row.

Page 20: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

SimpleStrategy with RF 3.Node 1

Node 2

Node 3

Node 4

'foo'token: 90

Page 21: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

NetworkTopologyStrategy uses a Replication Factor per Data

Centre. (Default.)

Page 22: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Multi DC Replication with RF 3 and RF 2.Node 1

Node 2

Node 3

Node 4

'foo'token: 90

Node 1

Node 2

Node 3

Node 4

West DC East DC

Page 23: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

The Snitch knows which Data Centre and Rack the Node is

in.

Page 24: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

SimpleSnitch.Places all nodes in the same

DC and Rack.(Default, there are others.)

Page 25: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

EC2Snitch.DC is set to AWS Region and a Rack to Availability Zone.

Page 26: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

The Client and the Coordinator.Node 1

Node 2

Node 3

Node 4

'foo'token: 90

Client

Page 27: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Multi DC Client and the Coordinator.Node 1

Node 2

Node 3

Node 4

'foo'token: 90

Client

Node 10

Node 20

Node 30

Node 40

Page 28: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Gossip.Nodes share information with a small number of neighbours. Who share information with a

small number of neigh..

Page 29: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Consistency Level (CL).- Specified for each request- Number of nodes to wait

for.

Page 30: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Consistency Level (CL)- Any*- One, Two Three- QUORUM- LOCAL_QUORUM, EACH_QUOURM*

Page 31: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

QUOURM at Replication Factor...

ReplicationFactor

QUOURM

2 or 3 4 or 5 6 or 7

2 3 4

Page 32: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Write ‘foo’ at QUOURM with Hinted Handoff.Node 1

Node 2

Node 3

Node 4'foo' for #3

'foo'token: 90

Client

Page 33: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Read ‘foo’ at QUOURM.Node 1

Node 2

Node 3

Node 4

'foo'token: 90

Client

Page 34: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Column Timestamps used to resolve

differences.

Page 35: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Resolving differences.Column Node 1 Node 2 Node 3

purplecromulent

(timestamp 10)cromulent

(timestamp 10) <missing>

monkeyembiggens

(timestamp 10)embiggens

(timestamp 10)debigulator

(timestamp 5)

dishwashertomato

(timestamp 10)tomato

(timestamp 10)tomacco

(timestamp 15)

Page 36: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Consistent read for ‘foo’ at QUOURM.Node 1

Node 2

Node 3

Node 4

Client

cromulent

cromulent

<empty>

Node 1

Node 2

Node 3

Node 4

Client

cromulent cromulent

Page 37: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Strong Consistency

W + R > N(#Write Nodes + #Read Nodes> Replication Factor)

Page 38: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Achieving Strong Consistency.- QUOURM Read + QUORUM Write- ALL Read + ONE Write- ONE Read + ALL Write

Page 39: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Achieving Consistency- Consistency Level- Hinted Handoff- Read Repair- Anti Entropy

Page 40: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

OverviewThe ClusterThe Node

The Data Model

Page 41: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Optimised for Writes.

Page 42: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Write path

Append to Write Ahead Log.

(fsync every 10s by default, other options available)

Page 43: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Write path...Merge Columns into Memtable.

(Lock free, always in memory.)

Page 44: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

(Later.)Asynchronously flush

Memtable to new files.(May be 10’s or 100’s of MB in size.)

Page 45: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Data is stored in immutable SSTables.

(Sorted String table.)

Page 46: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

SSTable files.*-Data.db*-Index.db*-Filter.db

(And others)

Page 47: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

SSTables.

SSTable 1foo: dishwasher (ts 10): tomato purple (ts 10): cromulent

SSTable 2foo: frink (ts 20): flayven monkey (ts 10): embiggins

SSTable 3 SSTable 4foo: dishwasher (ts 15): tomacco

SSTable 5

Page 48: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Read purple, monkey, dishwasher.

SSTable 1-Data.dbfoo: dishwasher (ts 10): tomato purple (ts 10): cromulent

SSTable 2-Data.dbfoo: frink (ts 20): flayven monkey (ts 10): embiggins

SSTable 3-Data.db SSTable 4-Data.dbfoo: dishwasher (ts 15): tomacco

SSTable 5-Data.db

Bloom Filter

Index Sample

SSTable 1-Index.db

Bloom Filter

Index Sample

SSTable 2-Index.db

Bloom Filter

Index Sample

SSTable 3-Index.db

Bloom Filter

Index Sample

SSTable 4-Index.db

Bloom Filter

Index Sample

SSTable 5-Index.db

Memory

Disk

Page 49: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Key Cache caches row key position in *-Data.db file.

(Removes up to1disk seek per SSTable.)

Page 50: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Read with Key Cache.

SSTable 1-Data.dbfoo: dishwasher (ts 10): tomato purple (ts 10): cromulent

SSTable 2-Data.dbfoo: frink (ts 20): flayven monkey (ts 10): embiggins

SSTable 3-Data.db SSTable 4-Data.dbfoo: dishwasher (ts 15): tomacco

SSTable 5-Data.db

Key Cache

Index Sample

SSTable 1-Index.db

Key Cache

Index Sample

SSTable 2-Index.db

Key Cache

Index Sample

SSTable 3-Index.db

Key Cache

Index Sample

SSTable 4-Index.db

Key Cache

Index Sample

SSTable 5-Index.db

Memory

Disk

Bloom Filter Bloom Filter Bloom Filter Bloom Filter Bloom Filter

Page 51: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Row Cache caches entire row.(Removes all disk IO.)

Page 52: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Read with Row Cache.Row Cache

SSTable 1-Data.dbfoo: dishwasher (ts 10): tomato purple (ts 10): cromulent

SSTable 2-Data.dbfoo: frink (ts 20): flayven monkey (ts 10): embiggins

SSTable 3-Data.db SSTable 4-Data.dbfoo: dishwasher (ts 15): tomacco

SSTable 5-Data.db

Key Cache

Index Sample

SSTable 1-Index.db

Key Cache

Index Sample

SSTable 2-Index.db

Key Cache

Index Sample

SSTable 3-Index.db

Key Cache

Index Sample

SSTable 4-Index.db

Key Cache

Index Sample

SSTable 5-Index.db

Memory

Disk

Bloom Filter Bloom Filter Bloom Filter Bloom Filter Bloom Filter

Page 53: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Compaction merges truth from multiple SSTables into one

SSTable with the same truth.(Manual and continuous background process.)

Page 54: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Compaction.Column SSTable 1 SSTable 2 SSTable 4 New

purplecromulent

(timestamp 10)<tombstone>(timestamp 15)

<tombstone>(timestamp 15)

monkeyembiggens

(timestamp 10)embiggens

(timestamp 10)

dishwashertomato

(timestamp 10)tomacco

(timestamp 15)tomacco

(timestamp 15)

Page 55: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

OverviewThe ClusterThe Node

The Data Model

Page 56: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Cassandra is good at

reading data from a row in the order it is stored.

Page 57: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Typically an efficient data model will

denormalize data and use the storage engine order.

Page 58: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

To create a good data model

understand the queries your application requires.

Page 59: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

API ChoiceThrift

Original and still fully supported API.

Page 60: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

API ChoiceCQL3

New and fully supported API.

Page 61: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

CQL 3A Table Orientated, Schema

Driven, Data Model and Query language similar to

SQL.

Page 62: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

CQL 3A Table Orientated, Schema

Driven, Data Model and Query language similar to

SQL.

Page 63: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Twitter clone

using CQL 3 via the cqlsh tool.

bin/cqlsh

Page 64: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Queries?- Post Tweet to Followers - Get Tweet by ID- List Tweets by User- List Tweets in User Timeline- List Followers

Page 65: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Keyspace

A Namespace container.

Page 66: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Our Keyspace

CREATE KEYSPACE cass_community WITH replication = {'class':'NetworkTopologyStrategy', 'datacenter1':1};

Page 67: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Table

A sparse collection of well known, ordered columns.

Page 68: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

First Table

CREATE TABLE User ( user_name text, password text, real_name text, PRIMARY KEY (user_name));

Page 69: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Some userscqlsh:cass_community> INSERT INTO User ... (user_name, password, real_name) ... VALUES ... ('fred', 'sekr8t', 'Mr Foo');

cqlsh:cass_community> select * from User;

user_name | password | real_name-----------+----------+----------- fred | sekr8t | Mr Foo

Page 70: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Some userscqlsh:cass_community> INSERT INTO User ... (user_name, password) ... VALUES ... ('bob', 'pwd');

cqlsh:cass_community> select * from User where user_name = 'bob';

user_name | password | real_name-----------+----------+----------- bob | pwd | null

Page 71: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Data Model (so far)

Table / Value User

user_name Primary Key

Page 72: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Tweet TableCREATE TABLE Tweet ( tweet_id bigint, body text, user_name text, timestamp timestamp, PRIMARY KEY (tweet_id));

Page 73: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Tweet Table...cqlsh:cass_community> INSERT INTO Tweet ... (tweet_id, body, user_name, timestamp) ... VALUES ... (1, 'The Tweet','fred',1352150816917);

cqlsh:cass_community> select * from Tweet where tweet_id = 1;

tweet_id | body | timestamp | user_name----------+-----------+--------------------------+----------- 1 | The Tweet | 2012-11-06 10:26:56+1300 | fred

Page 74: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Data Model (so far)

Table / Value User Tweet

user_name Primary Key Field

tweet_id Primary Key

Page 75: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

UserTweets TableCREATE TABLE UserTweets ( tweet_id bigint, user_name text, body text, timestamp timestamp, PRIMARY KEY (user_name, tweet_id));

Page 76: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

UserTweets Table...cqlsh:cass_community> INSERT INTO UserTweets ... (tweet_id, body, user_name, timestamp) ... VALUES ... (1, 'The Tweet','fred',1352150816917);

cqlsh:cass_community> select * from UserTweets where user_name='fred';

user_name | tweet_id | body | timestamp-----------+----------+-----------+-------------------------- fred | 1 | The Tweet | 2012-11-06 10:26:56+1300

Page 77: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

UserTweets Table...cqlsh:cass_community> select * from UserTweets where user_name='fred' and tweet_id=1;

user_name | tweet_id | body | timestamp-----------+----------+-----------+-------------------------- fred | 1 | The Tweet | 2012-11-06 10:26:56+1300

Page 78: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

UserTweets Table...cqlsh:cass_community> INSERT INTO UserTweets ... (tweet_id, body, user_name, timestamp) ... VALUES ... (2, 'Second Tweet', 'fred', 1352150816918);

cqlsh:cass_community> select * from UserTweets where user_name = 'fred';

user_name | tweet_id | body | timestamp-----------+----------+--------------+-------------------------- fred | 1 | The Tweet | 2012-11-06 10:26:56+1300 fred | 2 | Second Tweet | 2012-11-06 10:26:56+1300

Page 79: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

UserTweets Table...cqlsh:cass_community> select * from UserTweets where user_name = 'fred' order by tweet_id desc;

user_name | tweet_id | body | timestamp-----------+----------+--------------+-------------------------- fred | 2 | Second Tweet | 2012-11-06 10:26:56+1300 fred | 1 | The Tweet | 2012-11-06 10:26:56+1300

Page 80: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

UserTimelineCREATE TABLE UserTimeline ( user_name text, tweet_id bigint, tweet_user text, body text, timestamp timestamp, PRIMARY KEY (user_name, tweet_id))WITH CLUSTERING ORDER BY (tweet_id DESC);

Page 81: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

UserTimelinecqlsh:cass_community> INSERT INTO UserTimeline ... (user_name, tweet_id, tweet_user, body, timestamp) ... VALUES ... ('fred', 1, 'fred', 'The Tweet',1352150816917);

cqlsh:cass_community> INSERT INTO UserTimeline ... (user_name, tweet_id, tweet_user, body, timestamp) ... VALUES ... ('fred', 100, 'bob', 'My Tweet',1352150846917);

Page 82: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

UserTimelinecqlsh:cass_community> select * from UserTimeline where user_name = 'fred';

user_name | tweet_id | body | timestamp | tweet_user-----------+----------+-----------+--------------------------+------------ fred | 100 | My Tweet | 2012-11-06 10:27:26+1300 | bob fred | 1 | The Tweet | 2012-11-06 10:26:56+1300 | fred

Page 83: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Data Model (so far)

Table / Value User Tweet User

TweetsUser

Timeline

user_name Primary Key Field Primary Key Primary Key

tweet_id Primary Key Primary KeyComponent

Primary KeyComponent

Page 84: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

UserMetrics TableCREATE TABLE UserMetrics( user_name text, tweets counter, followers counter, following counter, PRIMARY KEY (user_name));

Page 85: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

UserMetrics Table...cqlsh:cass_community> UPDATE ... UserMetrics ... SET ... tweets = tweets + 1 ... WHERE ... user_name = 'fred';cqlsh:cass_community> select * from UserMetrics where user_name = 'fred'; user_name | followers | following | tweets-----------+-----------+-----------+-------- fred | null | null | 1

Page 86: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Data Model (so far)

Table / Value User Tweet User

TweetsUser

Timeline User Metrics

user_name Primary Key Field Primary

KeyPrimary

KeyPrimary

Key

tweet_id Primary Key

Primary KeyComponent

Primary KeyComponent

Page 87: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

RelationshipsCREATE TABLE Followers( user_name text, follower text, timestamp timestamp, PRIMARY KEY (user_name, follower));

CREATE TABLE Following( user_name text, following text, timestamp timestamp, PRIMARY KEY (user_name, following));

Page 88: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Relationshipscqlsh:cass_community> INSERT INTO ... Following ... (user_name, following, timestamp) ... VALUES ... ('bob', 'fred', 1352247749161);cqlsh:cass_community> INSERT INTO ... Followers ... (user_name, follower, timestamp) ... VALUES ... ('fred', 'bob', 1352247749161);

Page 89: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Relationshipscqlsh:cass_community> select * from Following;

user_name | following | timestamp-----------+-----------+-------------------------- bob | fred | 2012-11-07 13:22:29+1300

cqlsh:cass_community> select * from Followers;

user_name | follower | timestamp-----------+----------+-------------------------- fred | bob | 2012-11-07 13:22:29+1300

Page 90: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Data Model

Table / Value User Tweet User

TweetsUser

TimelineUser

MetricsFollows

Followers

user_name Primary Key Field Primary

KeyPrimary

KeyPrimary

KeyPrimary

Key

tweet_id Primary Key

Primary KeyComponent

Primary KeyComponent

Page 91: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Cassandra Summit 2013June 11 & 12San Francisco

Use SFSummit25 for 25% off

Page 92: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Thanks.

Page 93: Cassandra Community Webinar  - Introduction To Apache Cassandra 1.2

Aaron Morton@aaronmorton

www.thelastpickle.com

Licensed under a Creative Commons Attribution-NonCommercial 3.0 New Zealand License