24
Cassandra writes, reads, and compaction Amy Hanlon

Cassandra writes-reads-compaction

Embed Size (px)

Citation preview

Cassandra writes, reads, and compaction

Amy Hanlon

Venmo Hacker School Recurse Center

!

@amygdalama mathamy.com

“In Cassandra, writes are fast,

but reads are slow.”

DESCRIBE  contact_book;  +-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐-­‐-­‐+  |  Field                |  Type                |  Null  |  Key  |  Default  |  Extra  |  +-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐-­‐-­‐+  |  user_id            |  int(11)          |  NO      |  PRI  |  NULL        |              |  |  phone_number  |  varchar(15)  |  NO      |  PRI  |  NULL        |              |  |  name                  |  varchar(60)  |  YES    |          |  NULL        |              |  +-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐-­‐-­‐+

SELECT  *  FROM  contact_book;  +-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+  |  user_id  |  phone_number  |  name              |  +-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+  |              1  |  3453456789      |  NULL              |  |              1  |  6786786789      |  Amy  Hanlon  |  +-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+

UPDATE  contact_book      SET  name='Cassidy  Williams'        WHERE  user_id=1  AND        phone_number='3453456789';

MySQL updates• append to log

• locate row(s)

• modify index and row(s) in memory

• acknowledge

• update index and row on disk

“In Cassandra, writes are fast,

but reads are slow.”

DESCRIBE  TABLE  contact_book;  !

CREATE  TABLE  contact_book  (      user_id  int,      phone_number  text,      name  text,      PRIMARY  KEY  ((user_id),        phone_number)  )

select  *  from  contact_book;  !

 user_id  |  phone_number  |  name  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐                1  |      3453456789  |              null                1  |      6786786789  |  Amy  Hanlon

select  *  from  contact_book;  !

user_id  1:  !

 3453456789:name  |  6786786789:name  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐                          null  |                Amy  Hanlon  

“In Cassandra, writes are fast,

but reads are slow.”

Cassandra updates

• append to log

• write update to memtable

• acknowledge

• (eventually) flush memtable to disk into sstable

INSERT  INTO  contact_book        (user_id,  phone_number,  name)        VALUES  (1,  '3453456789',        'cassidy');  !

#  flush  memtable  !

UPDATE  contact_book      SET  name='Cassidy  Williams'        WHERE  user_id=1  AND      phone_number='3453456789';

“In Cassandra, writes are fast,

but reads are slow.”

Cassandra reads

• read row(s) from sstables

• get updates to row(s) from memtable

• reduce any multiple values for columns

• respond to coordinator

Compaction!

Size-Tiered Compaction

Size-Tiered Compaction

Leveled Compaction

level 1 level 2

Faster reads

• use the right compaction strategy

• row cache

• key cache

Links• http://planetcassandra.org/blog/composite-keys-in-apache-cassandra/

• http://docs.datastax.com/en/cassandra/2.0/cassandra/dml/dml_write_path_c.html

• http://thelastpickle.com/blog/2011/04/28/Forces-of-Write-and-Read.html

• http://docs.datastax.com/en/cassandra/2.0/cassandra/dml/dml_about_reads_c.html

• http://docs.datastax.com/en/cassandra/2.0/cassandra/operations/ops_how_cache_works_c.html

• http://www.datastax.com/dev/blog/when-to-use-leveled-compaction

• http://www.datastax.com/dev/blog/leveled-compaction-in-apache-cassandra

Thank you! !

@amygdalama mathamy.com