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

MongoDB - Grails Introduction

cj_harris5
May 17, 2012
500

MongoDB - Grails Introduction

cj_harris5

May 17, 2012
Tweet

Transcript

  1. Terminology RDBMS MongoDB Table Collection Row(s) JSON Document Index Index

    Join Embedding & Linking Partition Shard Partition Key Shard Key Thursday, 17 May 12
  2. Here is a “simple” SQL Model mysql> select * from

    book; +----+----------------------------------------------------------+ | id | title | +----+----------------------------------------------------------+ | 1 | The Demon-Haunted World: Science as a Candle in the Dark | | 2 | Cosmos | | 3 | Programming in Scala | +----+----------------------------------------------------------+ 3 rows in set (0.00 sec) mysql> select * from bookauthor; +---------+-----------+ | book_id | author_id | +---------+-----------+ | 1 | 1 | | 2 | 1 | | 3 | 2 | | 3 | 3 | | 3 | 4 | +---------+-----------+ 5 rows in set (0.00 sec) mysql> select * from author; +----+-----------+------------+-------------+-------------+---------------+ | id | last_name | first_name | middle_name | nationality | year_of_birth | +----+-----------+------------+-------------+-------------+---------------+ | 1 | Sagan | Carl | Edward | NULL | 1934 | | 2 | Odersky | Martin | NULL | DE | 1958 | | 3 | Spoon | Lex | NULL | NULL | NULL | | 4 | Venners | Bill | NULL | NULL | NULL | +----+-----------+------------+-------------+-------------+---------------+ 4 rows in set (0.00 sec) Thursday, 17 May 12
  3. The Same Data in MongoDB { "_id" : ObjectId("4dfa6baa9c65dae09a4bbda5"), "title"

    : "Programming in Scala", "author" : [ { "first_name" : "Martin", "last_name" : "Odersky", "nationality" : "DE", "year_of_birth" : 1958 }, { "first_name" : "Lex", "last_name" : "Spoon" }, { "first_name" : "Bill", "last_name" : "Venners" } ] } Thursday, 17 May 12
  4. class Person { String firstName String lastName Address address static

    embedded = ['address'] } Domain Class - Embedded Thursday, 17 May 12
  5. class Person { String firstName String lastName Address address List

    otherAddresses static embedded = ['address', 'otherAddresses'] } Domain Class - List Thursday, 17 May 12
  6. new Person (firstName: "Chris").save() class Person { String firstName String

    lastName Address address List otherAddresses static embedded = ['address', 'otherAddresses'] } Domain Class - Save Thursday, 17 May 12
  7. def db = mongo.getDB("mydb") Note : MongoDB will create the

    database if it does not exist Getting a Database Thursday, 17 May 12
  8. Cursors > var c = db.test.find({x: 20}).skip(20).limit(10)> c.next() > c.next()

    ... $gt, $lt, $gte, $lte, $ne, $all, $in, $nin, $or, $not, $mod, $size, $exists, $type, $elemMatch query first N results + cursor id getMore w/ cursor id next N results + cursor id or 0 ... Thursday, 17 May 12
  9. Types of outage • Planned • Hardware upgrade • O/S

    or file-system tuning • Relocation of data to new file-system / storage • Software upgrade • Unplanned • Hardware failure • Data center failure • Region outage • Human error • Application corruption Thursday, 17 May 12
  10. Replica Sets • Data Protection • Multiple copies of the

    data • Spread across Data Centers, AZs • High Availability • Automated Failover • Automated Recovery Thursday, 17 May 12
  11. Replica Sets Primary Secondary Secondary Read Write Read Read App

    Asynchronous Replication Thursday, 17 May 12
  12. Elections During an election • Most up to date •

    Highest priority • Less than 10s behind failed Primary Thursday, 17 May 12
  13. Types of Durability with MongoDB • Fire and forget •

    Wait for error • Wait for fsync • Wait for journal sync • Wait for replication Thursday, 17 May 12
  14. Wait for Journal Sync Driver Primary apply in memory write

    Write to journal Thursday, 17 May 12
  15. import com.mongodb.WriteConcern class Person { String name static mapping =

    { writeConcern WriteConcern.SAFE } } Domain Class - Write Concern Thursday, 17 May 12
  16. db.blogs.ensureIndex({author: 1}) 1 = ascending -1 = descending An index

    on _id is automatic. For more use ensureIndex: Creating Indexes Thursday, 17 May 12
  17. class Person { String name static mapping = { name

    index:true } } Domain Class - Index Thursday, 17 May 12
  18. Compound Indexes db.blogs.save({ author: "James", ts: new Date() ... });

    db.blogs.ensureIndex({author: 1, ts: -1}) Thursday, 17 May 12
  19. db.blogs.save({ title: "My First blog", comments: [ {author: "James", ts

    : new Date()} ] }); db.blogs.ensureIndex({"comments.author": 1}) db.blogs.find({"comments.author": "James"}) Indexing Embedded Arrays Thursday, 17 May 12
  20. db.blogs.save({ loc: { long: 40.739037, lat: 40.739037 } }); db.blogs.save({

    loc: [40.739037, 40.739037] }); db.blogs.ensureIndex({"loc": "2d"}) Geospatial • Geo hash stored in B-Tree • First two values indexed Thursday, 17 May 12
  21. class Hotel { String name List location static mapping =

    { location geoIndex:true } } Domain Class - Geospacial Thursday, 17 May 12
  22. Write scaling - add Shards write read shard1 node_c1 node_b1

    node_a1 shard2 node_c2 node_b2 node_a2 Thursday, 17 May 12
  23. Write scaling - add Shards write read shard1 node_c1 node_b1

    node_a1 shard2 node_c2 node_b2 node_a2 shard3 node_c3 node_b3 node_a3 Thursday, 17 May 12
  24. MongoDB Sharding • Automatic partitioning and management • Range based

    • Convert to sharded system with no downtime • Fully consistent Thursday, 17 May 12
  25. How MongoDB Sharding works > db.posts.save( {age:40} ) -∞ +∞

    -∞ 40 41 +∞ •Data in inserted •Ranges are split into more “chunks” Thursday, 17 May 12
  26. How MongoDB Sharding works > db.posts.save( {age:40} ) > db.posts.save(

    {age:50} ) > db.posts.save( {age:60} ) -∞ +∞ -∞ 40 41 +∞ 41 50 51 +∞ 61 +∞ 51 60 Thursday, 17 May 12
  27. Balancing Shard 1 Shard 2 Shard 3 Shard 4 5

    9 1 6 10 2 7 11 3 8 12 4 17 21 13 18 22 14 19 23 15 20 24 16 29 33 25 30 34 26 31 35 27 32 36 28 41 45 37 42 46 38 43 47 39 44 48 40 mongos balancer config config config Chunks! Thursday, 17 May 12
  28. Balancing mongos balancer config config config Shard 1 Shard 2

    Shard 3 Shard 4 5 9 1 6 10 2 7 11 3 8 12 4 21 22 23 24 33 34 35 36 45 46 47 48 Imbalance Imbalance Thursday, 17 May 12
  29. Balancing mongos balancer Move chunk 1 to Shard 2 config

    config config Shard 1 Shard 2 Shard 3 Shard 4 5 9 1 6 10 2 7 11 3 8 12 4 21 22 23 24 33 34 35 36 45 46 47 48 Thursday, 17 May 12
  30. Balancing mongos balancer config config config Shard 1 Shard 2

    Shard 3 Shard 4 5 9 6 10 2 7 11 3 8 12 4 21 22 23 24 33 34 35 36 45 46 47 48 1 Thursday, 17 May 12
  31. Balancing mongos balancer config config config Shard 1 Shard 2

    Shard 3 Shard 4 5 9 6 10 2 7 11 3 8 12 4 21 22 23 24 33 34 35 36 45 46 47 48 1 Thursday, 17 May 12
  32. Balancing mongos balancer Chunk 1 now lives on Shard 2

    config config config Shard 1 Shard 2 Shard 3 Shard 4 5 9 1 6 10 2 7 11 3 8 12 4 21 22 23 24 33 34 35 36 45 46 47 48 Thursday, 17 May 12