A Morning with MongoDB Barcelona: MongoDB Basic Concepts

62
MongoDB Basic Concepts Norberto Leite Senior Solutions Architect, EMEA [email protected] @nleite Sunday, 21 October 12

description

http://www.10gen.com/events/MongoDB-Morning-Barcelona

Transcript of A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Page 1: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

MongoDB Basic Concepts

Norberto Leite

Senior Solutions Architect, [email protected]

@nleite

Sunday, 21 October 12

Page 2: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Agenda

•Overview•Replication•Scalability•Consistency & Durability•Flexibility, Developer Experienc

Sunday, 21 October 12

Page 3: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

http://bit.ly/OT71M4

Your data needs started here...

Sunday, 21 October 12

Page 4: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

http://bit.ly/Oxcsis

...but soon you had to be here

Sunday, 21 October 12

Page 5: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Basic Concepts

Horizontally Scalable

{ author : “steve”, date : new Date(), text : “About MongoDB...”, tags : [“tech”, “database”]}

Document Oriented

Application

Fully Consistent

High Performance

Sunday, 21 October 12

Page 6: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

depth of functionality

scal

abili

ty &

per

form

ance •memcached

•key/value

• RDBMS

Tradeoff: Scale vs Functionality

Sunday, 21 October 12

Page 7: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Replication

Sunday, 21 October 12

Page 8: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Why do we need replication

•Failover •Backups•Secondary batch jobs •High availability

Sunday, 21 October 12

Page 9: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Replica SetsData Availability across nodes• Data Protection

• Multiple copies of the data• Spread across Data Centers, AZs

• High Availability• Automated Failover• Automated Recovery

Sunday, 21 October 12

Page 10: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Replica Sets

Primary

Secondary

Secondary

Read

Write

Read

Read

App

Asynchronous Replication

Sunday, 21 October 12

Page 11: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Replica Sets

Primary

Secondary

Secondary

Read

Write

Read

Read

App

Sunday, 21 October 12

Page 12: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Replica Sets

Primary

Primary

Secondary

Read

Write

Read

Automatic Election of new Primary

App

Sunday, 21 October 12

Page 13: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Replica Sets

Recovering

Primary

Secondary

Read

Write

Read

New primary serves data

App

Sunday, 21 October 12

Page 14: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Replica Sets

Secondary

Primary

Secondary

Read

Write

Read

Read

App

Sunday, 21 October 12

Page 15: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Scalability

Sunday, 21 October 12

Page 16: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Horizontal Scalability

Sunday, 21 October 12

Page 17: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

ShardingData Distribution across nodes• Data location transparent to your code• Data distribution is automatic• Data re-distribution is automatic• Aggregate system resources horizontally• No code changes

Sunday, 21 October 12

Page 18: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Sharding - Range distribution

shard01 shard02 shard03

sh.shardCollection("test.tweets", {_id: 1} , false)

Sunday, 21 October 12

Page 19: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Sharding - Range distribution

shard01 shard02 shard03

a-i j-r s-z

Sunday, 21 October 12

Page 20: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Sharding - Splits

shard01 shard02 shard03

a-i ja-jz s-z

k-r

Sunday, 21 October 12

Page 21: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Sharding - Splits

shard01 shard02 shard03

a-i ja-ji s-z

ji-js

js-jw

jz-r

Sunday, 21 October 12

Page 22: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Sharding - Auto Balancing

shard01 shard02 shard03

a-i ja-ji s-z

ji-js

js-jw

jz-r

js-jw

jz-r

Sunday, 21 October 12

Page 23: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Sharding - Auto Balancing

shard01 shard02 shard03

a-i ja-ji n-z

ji-js

js-jw

jz-r

Sunday, 21 October 12

Page 24: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Sharding - Routed Query

shard01 shard02 shard03

a-i ja-ji n-z

ji-js

js-jw

jz-r

find({_id: "norberto"})

Sunday, 21 October 12

Page 25: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Sharding - Routed Query

shard01 shard02 shard03

a-i ja-ji n-z

ji-js

find({_id: "norberto"})

js-jw

jz-r

Sunday, 21 October 12

Page 26: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Sharding - Scatter Gather

shard01 shard02 shard03

a-i ja-ji n-z

ji-js

js-jw

jz-r

find({email: "[email protected]"})

Sunday, 21 October 12

Page 27: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Sharding - Scatter Gather

shard01 shard02 shard03

a-i ja-ji n-z

ji-js

js-jw

jz-r

find({email: "[email protected]"})

Sunday, 21 October 12

Page 28: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Sharding - Caching

shard01

a-i

j-r

n-z

300

GB

Dat

a

300 GB

96 GB Mem3:1 Data/Mem

Sunday, 21 October 12

Page 29: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Aggregate Horizontal Resources

shard01 shard02 shard03

a-i j-r n-z

96 GB Mem1:1 Data/Mem

100 GB 100 GB 100 GB

300

GB

Dat

a

96 GB Mem1:1 Data/Mem

96 GB Mem1:1 Data/Mem

Sunday, 21 October 12

Page 30: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Consistency & Durability

Sunday, 21 October 12

Page 31: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Two choices for consistency

•Eventual consistency•Allow updates when a system has been partitioned•Resolve conflicts later•Example: CouchDB, Cassandra

•Immediate consistency•Limit the application of updates to a single master node for a given slice of data

•Another node can take over after a failure is detected•Avoids the possibility of conflicts•Example: MongoDB

Sunday, 21 October 12

Page 32: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Durability

•For how long is my data available?•When do I now that my data is safe?•Where?

•Mongodb style•Fire and Forget•Get Last Error•Journal Sync•Replica Safe

Sunday, 21 October 12

Page 33: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Fire and Forget

Driver Primary

Sunday, 21 October 12

Page 34: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Fire and Forget

Driver Primary

write

Sunday, 21 October 12

Page 35: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Fire and Forget

Driver Primary

write

apply in memory

Sunday, 21 October 12

Page 36: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Get Last Error

Driver Primary

Sunday, 21 October 12

Page 37: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Get Last Error

Driver Primary

getLastError

write

Sunday, 21 October 12

Page 38: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Get Last Error

Driver Primary

getLastErrorapply in memory

write

Sunday, 21 October 12

Page 39: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Get Last Error

Driver Primary

getLastErrorapply in memory

write

Sunday, 21 October 12

Page 40: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Journal Sync

Driver Primary

Sunday, 21 October 12

Page 41: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Journal Sync

Driver Primary

getLastErrorapply in memory

write

j:true

Sunday, 21 October 12

Page 42: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Journal Sync

Driver Primary

getLastErrorapply in memory

write

j:true

write to journal

Sunday, 21 October 12

Page 43: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Journal Sync

Driver Primary

getLastErrorapply in memory

write

j:true

write to journal

Sunday, 21 October 12

Page 44: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Replicas Safe

Driver Primary Secondary

Sunday, 21 October 12

Page 45: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Replicas Safe

Driver Primary

getLastErrorapply in memory

write

w:2

Secondary

Sunday, 21 October 12

Page 46: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Replicas Safe

Driver Primary

getLastErrorapply in memory

write

w:2

replicate

Secondary

Sunday, 21 October 12

Page 47: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Replicas Safe

Driver Primary

getLastErrorapply in memory

write

w:2

replicate

Secondary

Sunday, 21 October 12

Page 48: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Flexibility

Sunday, 21 October 12

Page 49: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

• Cost effective operationalize abundant data (clickstreams, logs, tweets, ...)

• Relaxed transactional semantics enable easy scale out

• Auto Sharding for scale down and scale up

• Applications store complex data that is easier to model as documents

• Schemaless DB enables faster development cycles

What MongoDB solves

Agility

Flexibility

Cost

Sunday, 21 October 12

Page 50: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Challenges for Databases

✓ Build a database for scaleout• Run on clusters of 100s of commodity machines

•… that enables agile development

•… and is usable for a broad variety of applications

Sunday, 21 October 12

Page 51: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Data Model

• Why JSON?• Provides a simple, well understood encapsulation of data• Maps simply to the object in your OO language• Linking & Embedding to describe relationships

Sunday, 21 October 12

Page 52: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Json

place1 = { name : "10gen HQ", address : "578 Broadway 7th Floor", city : "New York", zip : "10011", tags : [ "business", "tech" ]}

Sunday, 21 October 12

Page 53: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Schema DesignRelational Database

Sunday, 21 October 12

Page 54: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Schema DesignMongoDB embedding

linkingSunday, 21 October 12

Page 55: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Schemas in MongoDB

Design documents that simply map to your application

post = {author: "Hergé", date: new Date(), text: "Destination Moon", tags: ["comic", "adventure"]}

> db.posts.save(post)

Sunday, 21 October 12

Page 56: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

> db.blogs.find( { author: "Hergé"} )

{ _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), author : "Hergé", date : ISODate("2011-09-18T09:56:06.298Z"), text : "Destination Moon", tags : [ "comic", "adventure" ], comments : [! {! ! author : "Kyle",! ! date : ISODate("2011-09-19T09:56:06.298Z"),! ! text : "great book"! } ] }

Embedding

Sunday, 21 October 12

Page 57: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

JSON & Scaleout

• Embedding removes need for• Distributed Joins• Two Phase commit

• Enables data to be distributed across many nodes without penalty

Sunday, 21 October 12

Page 62: A Morning with MongoDB Barcelona: MongoDB Basic Concepts

Sunday, 21 October 12