Practical Cassandra


Published on

When and why to move from RDMBS to Cassandra, it's quirks and limitations

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Practical Cassandra

  1. 1. Practical CassandraNoSQL key-value vs RDBMS – why and whenCassandra architectureCassandra data modelLife without joins or HDD space is cheap todayHardware requirements & deployment hints Vitalii Tymchyshyn @tivv00
  2. 2. RDBMS problemsSometimes you reach the point where single server cant copeRelational Replication Not write scalable Data is not instantly visibleSharding No foreign keys or joins No transactions Reduced reliability (multiple servers)Schema update is a pain
  3. 3. Cassandra NoSQLMaster-Master Replication + Sharding in one bottlePeer-to-peer architecture (no SPOF)Easy cluster reconfigurationEventual consistency as a standardAll data in one record – no need to joinFlexible schema
  4. 4. Our dataWe have intelligent Internet cacheIntelligent means we dont cache everything or we would need Googles DCIts still hundreds of millions of sitesAnd 10s of TB of packed dataRandomly updatedAnalysis must be able to process all of this in term of hours
  5. 5. Cassandra ring - server - client
  6. 6. Ring partitioner types Order Preserving RandomEach server serves Data is smoothly key range distributed onRange queries servers possible No range queriesRead/Write/Disk No hot spots space hot spots Fixed key range possibleComplex to fix key range
  7. 7. Runtime CAP-solvingThe whole thing is about replicationCAP: Consistency, Availability, Partition tolerance – choose two.With cassandra you can choose at runtime.
  8. 8. Runtime CAP-solvingQuorum read/write Fast writes Fast reads Fast, less consistency
  9. 9. Data modelKeyspaces – much like database in RDBMSColumn Families – storage element, like tables in RDBMSColumns – you can have million for a row, names are flexible, still like columns in RDBMSSuper Column – A column that has structured content, superseded by composite columns
  10. 10. Example Twitter DB Twitter Keyspace Users table Users CFID, Name, Birthday Key: User ID Name(Str), Birthday(Str)Tweets table Timeline CFUserID, TweetID, Key: User ID TweetContent <TweetID>(TweetContent)
  11. 11. Example (alternative) Twitter DB Twitter Keyspace Users tableID, Name, Birthday Data CF Key: User ID Name(Str), Birthday(Str),Tweets table <TweetID>(TweetContent)UserID, TweetID, TweetContent
  12. 12. Example (data)Users Tweets User ID Text ID Name 1 1 Hello 1 Tom 1 2 See me? 2 John 2 3 See you!DataKey Data1 Name = Tom T_1 = Hello T_2 = See me?2 Name = John T_3 = See you!
  13. 13. Data modelYou can have same key in multiple column familiesYou can have different set of columns for different keys in same column familyYou can query a range of columns for a key (columns are sorted) with paginationYou can have (and its useful) to have columns without values
  14. 14. ACID vs BASESuper Heroes are good, but not scalable. So, what do we loose?
  15. 15. No AtomicityYouve got no transactions – no rollbackThe maximum you have is atomic update to single rowFailed operation MAY be applied (thats why counters are not reliable)
  16. 16. Eventual ConsistencyCassandra has no central governorThis means no bottleneckThis also means no one knows if database as a whole is consistentRegular repair is your friend!
  17. 17. No IsolationAll mutations are timestamped to restore order from chaotic arrivalYou MUST have your clock synchronized Thats how operation are applied on server :)
  18. 18. Controlled DurabilityCassandra uses transaction log to ensure durability on single serverDurability of the whole database depends on both total number of replicas and write operation replication factorRemember, single server 99% uptime 100 means 36.6% (0.99 ) of “full cluster working” uptime for 100 servers – most time youve got at least one server down!
  19. 19. Data queryingWith SQL you simply ask.You can easily scan the whole DBIndexes may helpAny calculation is repeated each timeThis can be slow on read
  20. 20. Data queryingWith NoSQL you cant efficiently scan the whole dbNo “group by” or “order by”You must prepare your data beforehandYou have multiple copies of dataYou must recalculate on application logic changeThe precalculated reads are fast
  21. 21. Think on your queries in advance!There is no “Ill simply add an index, some hints and my query will become fast”Any index is created and maintained from application codeNow cassandra have secondary indexes, but they are much inferior to custom ones
  22. 22. Whats wrong with secondary indexesThey work on fixed column namesThey are consistent with dataThis means they live near the data they indexThis means they are distributed between nodes by row key, not by indexed column valueThis means you need to ask every node to get single value
  23. 23. Whats wrong with secondary indexesNode 1 Node 2 A: phone=1 C: phone=3 B: phone=3 D: phone=5Phone index: Phone index: 1=A,3=B 3=C,5=DNode 3 Node 4 E: phone=1 G: phone=3 F: phone=5 H: phone=7Phone index: Phone index: 1=E,5=F 3=G,7=H
  24. 24. “Index” exampleColumn family people Key: Fred [phone=2223355, phone2=4445566, fax=9998877] Key: John [phone=4445566, mobile=099123456]Column family phone_directory Key: 2223355 [Fred] Key: 4445566 [Fred, John] Key: 9998877 [Fred] Key: 099123456 [John]
  25. 25. “Join” exampleColumn family customer Key: Boeing [email:] Key: Oracle [skype: java]Column family orders Key: 1 [customer: Boeing, total: 200m] Key: 2 [customer: Oracle, total: 300m] Key: 3 [customer: Boeing, total: 500m]Column family customer_order_totals Key: Boeing[ 1:200m, 3:500m] Key: Oracle[ 2:300m]
  26. 26. Peer-to-peer replicationYour operation can return OK even if it was not written to every replicaHinted handoff will try to repair laterEven if your operation have failed, it may have been written to some replicasThis inconsistency wont be repaired automaticallyThis are drawbacks of “no master” architectureYou need to repair regular!
  27. 27. Tombstones and Repair Delete events are recorded as Tombstones to ensure arriving “before delete” data wont be usedRegular repair not only makes sureyour data is replicated, but alsothat your deletes are replicated.If you dont, beware of ghosts!
  28. 28. Resources & EnvironmentDisk space requirementsMemory requirementsNative plugins & configuration
  29. 29. Disk estimationsSay, weve got 1TB of dataReplication factor 3 make it 3TBData duplication make it 12TBTombstones/repair space make it 24TBBackups make it 36TB
  30. 30. Memory estimationsCassandra has certain in-memory structures that are linear to data amountKey and Row caches – configured at column family level. Change defaults if youve got a lot of CFsBloom filters and key samples cache are configured globally in latest versionsEstimate minimum ~0.5% of RAM for your data amount
  31. 31. Native specificsCassandra (like may other large things) likes JNA. Please install.Cassandra maps files to memory – cassandra process virtual and resident memory size will grow because of mmap.Default heap sizes are large – tame it if its not only task on the host
  32. 32. Q&AAuthor: Vitalii Tymchyshyn @tivv00