Share
Embed
Transcript
55 slides
-
Chris Harris Email : [email protected] Twitter : cj_harris5 Grails Introduction
Thursday, 17 May 12 -
Terminology RDBMS MongoDB Table Collection Row(s) JSON Document Index Index
Join Embedding & Linking Partition Shard Partition Key Shard Key Thursday, 17 May 12 -
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 -
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 -
class Person { String firstName String lastName Address address static
embedded = ['address'] } Domain Class - Embedded Thursday, 17 May 12 -
class Person { String firstName String lastName Address address List
otherAddresses static embedded = ['address', 'otherAddresses'] } Domain Class - List Thursday, 17 May 12 -
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 -
def p = new Person(firstName:”Chris”) p["city"]="London" p.save() Domain Class -
Dynamic Attributes Thursday, 17 May 12 -
def db = mongo.getDB("mydb") Note : MongoDB will create the
database if it does not exist Getting a Database Thursday, 17 May 12 -
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 -
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 -
Replica Sets • Data Protection • Multiple copies of the
data • Spread across Data Centers, AZs • High Availability • Automated Failover • Automated Recovery Thursday, 17 May 12 -
Replica Sets Primary Secondary Secondary Read Write Read Read App
Asynchronous Replication Thursday, 17 May 12 -
Replica Sets Primary Secondary Secondary Read Write Read Read App
Thursday, 17 May 12 -
Replica Sets Primary Primary Secondary Read Write Read Automatic Election
of new Primary App Thursday, 17 May 12 -
Replica Sets Recovering Primary Secondary Read Write Read New primary
serves data App Thursday, 17 May 12 -
Replica Sets Secondary Primary Secondary Read Write Read Read App
Thursday, 17 May 12 -
Elections During an election • Most up to date •
Highest priority • Less than 10s behind failed Primary Thursday, 17 May 12 -
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 -
Wait for Journal Sync Driver Primary apply in memory write
Write to journal Thursday, 17 May 12 -
Wait for replication Driver Primary apply in memory write Secondary
replicate Thursday, 17 May 12 -
import com.mongodb.WriteConcern class Person { String name static mapping =
{ writeConcern WriteConcern.SAFE } } Domain Class - Write Concern Thursday, 17 May 12 -
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 -
class Person { String name static mapping = { name
index:true } } Domain Class - Index Thursday, 17 May 12 -
Compound Indexes db.blogs.save({ author: "James", ts: new Date() ... });
db.blogs.ensureIndex({author: 1, ts: -1}) Thursday, 17 May 12 -
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 -
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 -
class Hotel { String name List location static mapping =
{ location geoIndex:true } } Domain Class - Geospacial Thursday, 17 May 12 -
Write scaling - add Shards write read shard1 node_c1 node_b1
node_a1 shard2 node_c2 node_b2 node_a2 Thursday, 17 May 12 -
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 -
MongoDB Sharding • Automatic partitioning and management • Range based
• Convert to sharded system with no downtime • Fully consistent Thursday, 17 May 12 -
How MongoDB Sharding works > db.posts.save( {age:40} ) -∞ +∞
-∞ 40 41 +∞ •Data in inserted •Ranges are split into more “chunks” Thursday, 17 May 12 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
conferences, appearances http://www.10gen.com/events and meetups http://www.meetup.com/London-MongoDB-User-Group http://www.meetup.com/London-Data-Bar Questions? Chris Harris
Email : [email protected] Twitter : cj_harris5 Thursday, 17 May 12