Upgrade to Pro — share decks privately, control downloads, hide ads and more …

NoSQL: MongoDB and Cassandra Overview

Avatar for dgoncharov dgoncharov
December 18, 2013

NoSQL: MongoDB and Cassandra Overview

Comparison of MongoDB and Cassandra

Avatar for dgoncharov

dgoncharov

December 18, 2013

Other Decks in Technology

Transcript

  1. Scalable eCommerce Platform Solutions CAP Theorem C A P Relational:

    MS SQL, MySQL, Posrgres ... Amazon Dynamo derivatives: Cassandra, Voldemort, Riak Neo4J, Google BigTable and Google BigTable derivatives: MongoDB, HBase, Redis, ...
  2. Scalable eCommerce Platform Solutions Scalable eCommerce Platform Solutions "NoSQL DBs

    trade off traditional features to better support new and emerging use cases"
  3. Scalable eCommerce Platform Solutions NoSQL • No JOINS • No

    Transactions • Relatively immature But • Could process Big data under high load
  4. Scalable eCommerce Platform Solutions Quick View MongoDB Cassandra Storage Type

    Document Column Short Description Developed and supported by 10gen Open source Written in C++ Initial release 2009 Initially developed by Facebook. Now Apache Cassandra Project Open source Written in Java Initial release 2008 Paid support by DataStax Best Use Content/product management, dynamic queries Write often, read less Key Points Schema less. Retains some properties of SQL such as query and index. High availability. Almost linear scales. SQL-like query language. Tunable consistency and flexible schema
  5. Scalable eCommerce Platform Solutions Sample Data { "_id" : 102,

    "name" : "Product1", "upcs" : [ { "number" : 10, "available" : true }, { "number" : 12 } ] } MongoDB UpcNumber (Key) ProductId Available Name 12 102 true “Product 1” 10 102 “Product 1” Cassandra
  6. Scalable eCommerce Platform Solutions Cassandra Row Details UpcNumber (Key) Available

    ProductId Name 12 true 102 “Product 1” 10 102 “Product 1” Details for row #1 Key Column Name Column Value Timestamp 12 "Available" true 1367216461600000 "ProductId" 120 1367216461142000 "Name" "Product 1" 1367216461142000
  7. Scalable eCommerce Platform Solutions MongoDB • Document collections are schemaless

    • BSON Documents  Binary serialized JSON with additional data types like Date, BinData, ObjectId  Document size up to 16Mb  Field names stored in each document
  8. Scalable eCommerce Platform Solutions Cassandra • Schema less (Thrift API)

     byte[] - is base type for keys, column name and values. Validators for Strings, Primitives - long, float, etc.  Rows could be wide - up to 2 billions  You can think about Row as SortedMap<ColumnName,ColumnValue> • Schema strict (CQL - SQL like syntax)  String column names  Set, List, and Map of Strings/Primitives
  9. Scalable eCommerce Platform Solutions Cassandra • Schema less (Thrift API)

    byte[] - is base type for keys, column name and values. Validators for Strings, Primitives - long, float, etc. Rows could be wide - up to 2 billions You can think about Row as SortedMap<ColumnName,ColumnValue> • Schema strict (CQL - SQL like syntax) String column names No direct wide rows support Set, List, and Map of Strings/Primitives
  10. Scalable eCommerce Platform Solutions Data Model Conclusion MongoDB Cassandra Schema

    less Schema strict/less BSON Wide rows (up to 2 Billions), Blobs or Set, List, Map of primitives Document size up to 16M Column value several megabytes. Row up to 2 GB Length of field names does matter Length of column names does matter
  11. Scalable eCommerce Platform Solutions MongoDB Replications • Replica sets –

    Primary/Secondaries • Min 3, max 12 nodes Primary Secondary 1 Secondary 2
  12. Scalable eCommerce Platform Solutions MongoDB Replications • Updates/Reads processed by

    primary • Oplog (capped collection), asynchronous communication with secondaries
  13. Scalable eCommerce Platform Solutions MongoDB Update request Primary Secondary 1

    Secondary 2 Update request Primary oplog Secondary 1 oplog Secondary 2 oplog
  14. Scalable eCommerce Platform Solutions MongoDB Update request Primary Secondary 1

    Secondary 2 Primary oplog Secondary 1 oplog Secondary 2 oplog Asynchronously Asynchronously
  15. Scalable eCommerce Platform Solutions MongoDB Sharding • Sharding performed by

    document field • Config server (used two phase commit) • mongos (aka routers) Replica Set - 1 Replica Set 1 Replica Set N mongos 1 Client 1 Config Server 1 Config Server 2 Config Server 3
  16. Scalable eCommerce Platform Solutions Request by _id in collection shared

    by _id Replica Set - 1 Replica Set 1 Replica Set N mongos 1 Client 1 Config Server 1 Config Server 2 Config Server 3
  17. Scalable eCommerce Platform Solutions Request by field that is not

    a shard key Replica Set - 1 Replica Set 1 Replica Set N mongos 1 Client 1 Config Server 1 Config Server 2 Config Server 3
  18. Scalable eCommerce Platform Solutions Cassandra Replication & Sharding • P2P

    architecture. No single point of failure • Keyspace level replication factor • Partitioning by key • Tunable topology (Could be Datacenter/Rack aware) • Elastic scalability Node 1 Node 5 Node 4 Node 2 Node 6 Node 3
  19. Scalable eCommerce Platform Solutions Write to cluster with 6 nodes

    Replication factor - 3 Client Node 6 Node 1 Node 5 Node 4 Node 3 Node 2
  20. Scalable eCommerce Platform Solutions Client Node 6 Node 1 Node

    5 Node 4 Node 3 Node 2 Key Range Node-1 A-D Node-2 E-I Node-3 J-M Node-4 N-R Node-5 S-V Node-6 W-Z Write row with key=K Coordinator Node
  21. Scalable eCommerce Platform Solutions Client Node 6 Node 1 Node

    5 Node 4 Node 3 Node 2 Client could determine node which should handle request
  22. Scalable eCommerce Platform Solutions Client Node 6 Node 1 Node

    5 Node 4 Node 3 Node 2 Cassandra failover • Gossip protocol to identify node fail
  23. Scalable eCommerce Platform Solutions Replication & Sharding summary MongoDB Cassandra

    Primary / Slaves P2P ring Shard by any document field Shard by key Different roles and more complex configuration Simple configuration, elastic scalability, Datacenter/Rack aware
  24. Scalable eCommerce Platform Solutions MongoDB Consistency & Durability • Consistent

    read from primaries, but eventually for reads from secondaries • database level read-writes locks • Write concern  Write acknowledgment from replica set (by default acknowledgment from primary)  Journaling (flush delay 100ms)
  25. Scalable eCommerce Platform Solutions Cassandra Consistency & Durability Write Read

    ANY - ONE ONE QUORUM QUORUM ALL ALL • Conflict resolution by timestamp • Commit log Strong consistency Weak consistency
  26. Scalable eCommerce Platform Solutions MongoDB Cassandra Consistent write. Tunable read

    consistency. Tunable write acknowledgement Tunable consistency for reads/writes. Trade off between consistency and availability Read-Write database level lock Conflict resolution by timestamp Document level isolation Row level isolation Journaling. Flush every 100ms Commit log on disk ACID Summary
  27. Scalable eCommerce Platform Solutions Querying db.products.find({"upcs.number": 10}, {"name":1}) ================================================= {

    "_id" : 3, "name" : "Product 1" } MongoDB Cassandra SELECT name FROM products WHERE upcNumber = 10; name Product 1
  28. Scalable eCommerce Platform Solutions MongoDB - Indexes • B-TREE (Similar

    performance problems as for RDBMS) • Single/Multiple fields • One index per query. Hint • Unique indexes (must be shard key) • Geospatial Indexes • Text indexes • explain
  29. Scalable eCommerce Platform Solutions MongoDB Aggregation queries • SQL like

    concepts:  WHERE, GROUP BY, HAVING, ORDER BY, LIMIT, SUM, COUNT • Map Reduce (javascript)
  30. Scalable eCommerce Platform Solutions Cassandra Indexes • Secondary indexes for

    non selective data • Store index in wide row • Compound primary keys • Plan your data model by your queries
  31. Scalable eCommerce Platform Solutions Cassandra queries • SELECT by primary,

    secondary or composite keys • COUNT, LIMIT support • Table with composite keys supports: ORDER BY, SORT BY, >, <
  32. Scalable eCommerce Platform Solutions Compound key sample CREATE TABLE category_products_by_price

    ( category_id int, price bigint, product_id int, product_name text, PRIMARY KEY (category_id, price, product_id) );
  33. Scalable eCommerce Platform Solutions Select with order by select *

    from category_products_by_price where category_id = 20 order by price asc; category_id price product_id product_name 20 20 101 Product 3 20 50 102 Product 1 20 150 103 Product 2
  34. Scalable eCommerce Platform Solutions Select with condition select * from

    category_products_by_price where category_id = 20 and price > 100; category_id price product_id product_name 20 150 102 Product 2
  35. Scalable eCommerce Platform Solutions Implementation details composite primary key Key

    Column Name Column Value Timestamp 20 150:102: 1367216461142000 150:102:product_name Product 2 1367216461142000 category_id price product_id product_name 20 150 102 Product 2
  36. Scalable eCommerce Platform Solutions MongoDB Cassandra Consistent write. Tunable read

    consistency. Tunable consistency for reads/writes. Read-Write database level lock Conflict resolution by timestamp Rich query and indexing support Limited query/indexing support. Plan your data model by your queries. More complex sharding/replicating model. Sharding by non PK Simple p2p architecture. Sharding only by PK. Elastic scale. Read better than writes. Intensive write could significantly impair performance. Extremely fast write. Reads looks good too. Overall summary