java web development with mongodb (presented at devoxx 2010)

129
Alvin Richards [email protected]

Upload: alvin-john-richards

Post on 22-Nov-2014

450 views

Category:

Documents


1 download

DESCRIPTION

In this presentation, we will try to answer the following- What is a document and a document database?- How does replication and sharding enable me to scale my application?- How does Java web development change when using MongoDB?- How do I deploy my application with MongoDB

TRANSCRIPT

Page 1: Java Web Development with MongoDB (presented at Devoxx 2010)

Alvin Richards [email protected]

Page 2: Java Web Development with MongoDB (presented at Devoxx 2010)

Topics

OverviewData modelingReplication & ShardingDeveloping with JavaDeployment

Page 3: Java Web Development with MongoDB (presented at Devoxx 2010)

Drinking from the fire hose

Page 4: Java Web Development with MongoDB (presented at Devoxx 2010)

Part OneMongoDB Overview

Page 5: Java Web Development with MongoDB (presented at Devoxx 2010)

Strong adoption of MongoDB

90,000 Database downloads per month

Page 6: Java Web Development with MongoDB (presented at Devoxx 2010)

Over 1,000 Production Deployments

web 2.0 companies started out using thisbut now:- enterprises- financial industries

3 Reason - Performance- Large number of readers / writers- Large data volume- Agility (ease of development)

Page 7: Java Web Development with MongoDB (presented at Devoxx 2010)

non-­‐relational,  next-­‐generation  operational  datastores  and  databases

NoSQL Really Means:

Page 8: Java Web Development with MongoDB (presented at Devoxx 2010)

RDBMS(Oracle,  MySQL)

past : one-size-fits-all

Page 9: Java Web Development with MongoDB (presented at Devoxx 2010)

RDBMS(Oracle,  MySQL)

New Gen. OLAP

(vertica,  aster,  greenplum)

present : business intelligence and analytics is now its own segment.

Page 10: Java Web Development with MongoDB (presented at Devoxx 2010)

RDBMS(Oracle,  MySQL)

New Gen. OLAP

(vertica,  aster,  greenplum)

Non-relationalOperational

Stores(“NoSQL”)

futurewe claim nosql segment will be: * large* not fragmented* ‘platformitize-able’

Page 11: Java Web Development with MongoDB (presented at Devoxx 2010)

Philosophy:  maximize  features  -­‐  up  to  the  “knee”  in  the  curve,  then  stop

depth  of  functionality

scalab

ility  &  perform

ance •memcached

• key/value

• RDBMS

Page 12: Java Web Development with MongoDB (presented at Devoxx 2010)

Horizontally ScalableArchitectures

no  joinsno  complex  transactions+

Page 13: Java Web Development with MongoDB (presented at Devoxx 2010)

New Data ModelsImproved ways to develop

no  joinsno  complex  transactions+

Page 14: Java Web Development with MongoDB (presented at Devoxx 2010)

Platform and Language supportMongoDB is Implemented in C++ for best performance

Platforms 32/64 bit• Windows• Linux, Mac OS-X, FreeBSD, Solaris

ease of development a surprisingly big benefit : faster to code, faster to change, avoid upgrades and scheduled downtimemore predictable performancefast single server performance -> developer spends less time manually coding around the databasebottom line: usually, developers like it much better after trying

Page 15: Java Web Development with MongoDB (presented at Devoxx 2010)

Platform and Language supportMongoDB is Implemented in C++ for best performance

Platforms 32/64 bit• Windows• Linux, Mac OS-X, FreeBSD, Solaris

Language drivers for• Java • Ruby / Ruby-on-Rails • C#• C / C++• Erlang • Python, Perl, JavaScript• Scala• others...

ease of development a surprisingly big benefit : faster to code, faster to change, avoid upgrades and scheduled downtimemore predictable performancefast single server performance -> developer spends less time manually coding around the databasebottom line: usually, developers like it much better after trying

Page 16: Java Web Development with MongoDB (presented at Devoxx 2010)

Part TwoData Modeling in MongoDB

Page 17: Java Web Development with MongoDB (presented at Devoxx 2010)

So why model data?

Page 18: Java Web Development with MongoDB (presented at Devoxx 2010)

A brief history of normalization• 1970 E.F.Codd introduces 1st Normal Form (1NF)• 1971 E.F.Codd introduces 2nd and 3rd Normal Form (2NF, 3NF)• 1974 Codd & Boyce define Boyce/Codd Normal Form (BCNF)• 2002 Date, Darween, Lorentzos define 6th Normal Form (6NF)

Goals:• Avoid anomalies when inserting, updating or deleting• Minimize redesign when extending the schema• Make the model informative to users• Avoid bias towards a particular style of query

* source : wikipedia

Page 19: Java Web Development with MongoDB (presented at Devoxx 2010)

The real benefit of relational

• Before relational• Data and Logic combined

• After relational• Separation of concerns• Data modeled independent of logic• Logic freed from concerns of data design

• MongoDB continues this separation

Page 20: Java Web Development with MongoDB (presented at Devoxx 2010)

Relational made normalized data look like this

Page 21: Java Web Development with MongoDB (presented at Devoxx 2010)

Document databases make normalized data look like this

Page 22: Java Web Development with MongoDB (presented at Devoxx 2010)

Terminology

RDBMS MongoDB

Table Collection

Row(s) JSON  Document

Index Index

Join Embedding  &  Linking

Partition Shard

Partition  Key Shard  Key

Page 23: Java Web Development with MongoDB (presented at Devoxx 2010)

DB ConsiderationsHow can we manipulate

this data ?

• Dynamic Queries

• Secondary Indexes

• Atomic Updates

• Map Reduce

Considerations• No Joins• Document writes are atomic

Access Patterns ?

• Read / Write Ratio

• Types of updates

• Types of queries

• Data life-cycle

Page 24: Java Web Development with MongoDB (presented at Devoxx 2010)

So today’s example will use...

Page 25: Java Web Development with MongoDB (presented at Devoxx 2010)

Design Session

Design documents that simply map to your applicationpost  =  {author:  “Hergé”,                date:  new  Date(),                text:  “Destination  Moon”,                tags:  [“comic”,  “adventure”]}

>db.post.save(post)

Page 26: Java Web Development with MongoDB (presented at Devoxx 2010)

>db.posts.find()

{ _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), author : "Hergé", date : "Sat Jul 24 2010 19:47:11 GMT-0700 (PDT)", text : "Destination Moon", tags : [ "comic", "adventure" ] } Notes:• ID must be unique, but can be anything you’d like• MongoDB will generate a default ID if one is not supplied

Find the document

Page 27: Java Web Development with MongoDB (presented at Devoxx 2010)

Secondary index for “author”

// 1 means ascending, -1 means descending

>db.posts.ensureIndex({author: 1})

>db.posts.find({author: 'Hergé'}) { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), author : "Hergé", ... }

Add and index, find via Index

Page 28: Java Web Development with MongoDB (presented at Devoxx 2010)

Verifying indexes exist

>db.system.indexes.find()

// Index on ID { name : "_id_", ns : "test.posts", key : { "_id" : 1 } }

// Index on author { _id : ObjectId("4c4ba6c5672c685e5e8aabf4"), ns : "test.posts", key : { "author" : 1 }, name : "author_1" }

Page 29: Java Web Development with MongoDB (presented at Devoxx 2010)

Query operatorsConditional operators: $ne, $in, $nin, $mod, $all, $size, $exists, $type, .. $lt, $lte, $gt, $gte, $ne,

// find posts with any tags >db.posts.find({tags: {$exists: true}})

Page 30: Java Web Development with MongoDB (presented at Devoxx 2010)

Query operatorsConditional operators: $ne, $in, $nin, $mod, $all, $size, $exists, $type, .. $lt, $lte, $gt, $gte, $ne,

// find posts with any tags >db.posts.find({tags: {$exists: true}})

Regular expressions: // posts where author starts with h >db.posts.find({author: /^h*/i })

Page 31: Java Web Development with MongoDB (presented at Devoxx 2010)

Query operatorsConditional operators: $ne, $in, $nin, $mod, $all, $size, $exists, $type, .. $lt, $lte, $gt, $gte, $ne,

// find posts with any tags >db.posts.find({tags: {$exists: true}})

Regular expressions: // posts where author starts with h >db.posts.find({author: /^h*/i })

Counting: // posts written by Hergé    >db.posts.find({author:  “Hergé”}).count()

Page 32: Java Web Development with MongoDB (presented at Devoxx 2010)

Extending the Schema        new_comment  =  {author:  “Kyle”,                                  date:  new  Date(),                                text:  “great  book”}

 >db.posts.update({_id:  “...”  },                        {  ‘$push’:  {comments:  new_comment},                          ‘$inc’:  {comments_count:  1}})

Page 33: Java Web Development with MongoDB (presented at Devoxx 2010)

     {  _id  :  ObjectId("4c4ba5c0672c685e5e8aabf3"),          author  :  "Hergé",        date  :  "Sat  Jul  24  2010  19:47:11  GMT-­‐0700  (PDT)",          text  :  "Destination  Moon",        tags  :  [  "comic",  "adventure"  ],        comments_count:  1,          comments  :  [   {     author  :  "Kyle",     date  :  "Sat  Jul  24  2010  20:51:03  GMT-­‐0700  (PDT)",     text  :  "great  book"   }        ]}

 

Extending the Schema

Page 34: Java Web Development with MongoDB (presented at Devoxx 2010)

// create index on nested documents: >db.posts.ensureIndex({"comments.author": 1})

>db.posts.find({comments.author:”Kyle”})

Extending the Schema

Page 35: Java Web Development with MongoDB (presented at Devoxx 2010)

// create index on nested documents: >db.posts.ensureIndex({"comments.author": 1})

>db.posts.find({comments.author:”Kyle”})

// find last 5 posts: >db.posts.find().sort({date:-1}).limit(5)

Extending the Schema

Page 36: Java Web Development with MongoDB (presented at Devoxx 2010)

// create index on nested documents: >db.posts.ensureIndex({"comments.author": 1})

>db.posts.find({comments.author:”Kyle”})

// find last 5 posts: >db.posts.find().sort({date:-1}).limit(5)

// most commented post: >db.posts.find().sort({comments_count:-1}).limit(1)

When sorting, check if you need an index

Extending the Schema

Page 37: Java Web Development with MongoDB (presented at Devoxx 2010)

Explain a query plan>  db.blogs.find({author:  'Hergé'}).explain(){   "cursor"  :  "BtreeCursor  author_1",   "nscanned"  :  1,   "nscannedObjects"  :  1,   "n"  :  1,   "millis"  :  5,   "indexBounds"  :  {     "author"  :  [       [         "Hergé",         "Hergé"       ]     ]   }

Page 38: Java Web Development with MongoDB (presented at Devoxx 2010)

Watch for full table scans

>  db.blogs.find({text:  'Destination  Moon'}).explain()    {   "cursor"  :  "BasicCursor",   "nscanned"  :  1,   "nscannedObjects"  :  1,   "n"  :  1,   "millis"  :  0,   "indexBounds"  :  {       }}

Page 39: Java Web Development with MongoDB (presented at Devoxx 2010)

Map Reduce

Page 40: Java Web Development with MongoDB (presented at Devoxx 2010)

Map reduce : count tagsmapFunc = function () { this.tags.forEach(function (z) {emit(z, {count:1});});}

reduceFunc = function (k, v) { var total = 0; for (var i = 0; i < v.length; i++) { total += v[i].count; } return {count:total}; }

res = db.posts.mapReduce(mapFunc, reduceFunc)

>db[res.result].find() { _id : "comic", value : { count : 1 } } { _id : "adventure", value : { count : 1 } }

Page 41: Java Web Development with MongoDB (presented at Devoxx 2010)

Group

• Equivalent to a Group By in SQL

• Specific the attributes to group the data

• Process the results in a Reduce function

Page 42: Java Web Development with MongoDB (presented at Devoxx 2010)

Groupcmd  =  {  key:  {  "author":true  },                initial:  {count:  0},                reduce:  function(obj,  prev)  {                                prev.count++;                            },            };result  =  db.posts.group(cmd);

[   {     "author"  :  "Hergé",     "count"  :  1   },   {     "author"  :  "Kyle",     "count"  :  3   }]

Page 43: Java Web Development with MongoDB (presented at Devoxx 2010)

Review

So Far:- Started out with a simple schema- Queried Data- Evolved the schema - Queried / Updated the data some more

Page 44: Java Web Development with MongoDB (presented at Devoxx 2010)
Page 45: Java Web Development with MongoDB (presented at Devoxx 2010)

Single Table Inheritance

>db.shapes.find() { _id: ObjectId("..."), type: "circle", area: 3.14, radius: 1} { _id: ObjectId("..."), type: "square", area: 4, d: 2} { _id: ObjectId("..."), type: "rect", area: 10, length: 5, width: 2}

// find shapes where radius > 0 >db.shapes.find({radius: {$gt: 0}})

// create index >db.shapes.ensureIndex({radius: 1})

Page 46: Java Web Development with MongoDB (presented at Devoxx 2010)

One to Many- Embedded Array / Array Keys - slice operator to return subset of array - some queries hard e.g find latest comments across all documents

Page 47: Java Web Development with MongoDB (presented at Devoxx 2010)

One to Many- Embedded Array / Array Keys - slice operator to return subset of array - some queries hard e.g find latest comments across all documents

- Embedded tree - Single document - Natural - Hard to query

Page 48: Java Web Development with MongoDB (presented at Devoxx 2010)

One to Many- Embedded Array / Array Keys - slice operator to return subset of array - some queries hard e.g find latest comments across all documents

- Embedded tree - Single document - Natural - Hard to query

- Normalized (2 collections) - most flexible - more queries

Page 49: Java Web Development with MongoDB (presented at Devoxx 2010)

Many - ManyExample: - Product can be in many categories- Category can have many products

Products- product_id

Category- category_id

Product_Categories- product_id- category_id

Page 50: Java Web Development with MongoDB (presented at Devoxx 2010)

products:      {  _id:  ObjectId("4c4ca23933fb5941681b912e"),          name:  "Destination  Moon",          category_ids:  [  ObjectId("4c4ca25433fb5941681b912f"),                                          ObjectId("4c4ca25433fb5941681b92af”]}    

Many - Many

Page 51: Java Web Development with MongoDB (presented at Devoxx 2010)

products:      {  _id:  ObjectId("4c4ca23933fb5941681b912e"),          name:  "Destination  Moon",          category_ids:  [  ObjectId("4c4ca25433fb5941681b912f"),                                          ObjectId("4c4ca25433fb5941681b92af”]}    categories:      {  _id:  ObjectId("4c4ca25433fb5941681b912f"),            name:  "Adventure",            product_ids:  [  ObjectId("4c4ca23933fb5941681b912e"),                                        ObjectId("4c4ca30433fb5941681b9130"),                                        ObjectId("4c4ca30433fb5941681b913a"]}

Many - Many

Page 52: Java Web Development with MongoDB (presented at Devoxx 2010)

products:      {  _id:  ObjectId("4c4ca23933fb5941681b912e"),          name:  "Destination  Moon",          category_ids:  [  ObjectId("4c4ca25433fb5941681b912f"),                                          ObjectId("4c4ca25433fb5941681b92af”]}    categories:      {  _id:  ObjectId("4c4ca25433fb5941681b912f"),            name:  "Adventure",            product_ids:  [  ObjectId("4c4ca23933fb5941681b912e"),                                        ObjectId("4c4ca30433fb5941681b9130"),                                        ObjectId("4c4ca30433fb5941681b913a"]}

//All  categories  for  a  given  product>db.categories.find({product_ids:  ObjectId("4c4ca23933fb5941681b912e")})

Many - Many

Page 53: Java Web Development with MongoDB (presented at Devoxx 2010)

products:      {  _id:  ObjectId("4c4ca23933fb5941681b912e"),          name:  "Destination  Moon",          category_ids:  [  ObjectId("4c4ca25433fb5941681b912f"),                                          ObjectId("4c4ca25433fb5941681b92af”]}    categories:      {  _id:  ObjectId("4c4ca25433fb5941681b912f"),            name:  "Adventure"}

Alternative

Page 54: Java Web Development with MongoDB (presented at Devoxx 2010)

products:      {  _id:  ObjectId("4c4ca23933fb5941681b912e"),          name:  "Destination  Moon",          category_ids:  [  ObjectId("4c4ca25433fb5941681b912f"),                                          ObjectId("4c4ca25433fb5941681b92af”]}    categories:      {  _id:  ObjectId("4c4ca25433fb5941681b912f"),            name:  "Adventure"}

//  All  products  for  a  given  category>db.products.find({category_ids:  ObjectId("4c4ca25433fb5941681b912f")})    

Alternative

Page 55: Java Web Development with MongoDB (presented at Devoxx 2010)

products:      {  _id:  ObjectId("4c4ca23933fb5941681b912e"),          name:  "Destination  Moon",          category_ids:  [  ObjectId("4c4ca25433fb5941681b912f"),                                          ObjectId("4c4ca25433fb5941681b92af”]}    categories:      {  _id:  ObjectId("4c4ca25433fb5941681b912f"),            name:  "Adventure"}

//  All  products  for  a  given  category>db.products.find({category_ids:  ObjectId("4c4ca25433fb5941681b912f")})  

//  All  categories  for  a  given  productproduct    =  db.products.find(_id  :  some_id)>db.categories.find({_id  :  {$in  :  product.category_ids}})  

Alternative

Page 56: Java Web Development with MongoDB (presented at Devoxx 2010)

TreesFull Tree in Document

{  comments:  [          {  author:  “Kyle”,  text:  “...”,                replies:  [                                            {author:  “Fred”,  text:  “...”,                                              replies:  []}                ]}    ]}

Pros: Single Document, Performance, Intuitive

Cons: Hard to search, Partial Results, 4MB limit

   

Page 57: Java Web Development with MongoDB (presented at Devoxx 2010)

TreesParent Links- Each node is stored as a document- Contains the id of the parent

Child Links- Each node contains the id’s of the children- Can support graphs (multiple parents / child)

Page 58: Java Web Development with MongoDB (presented at Devoxx 2010)

Array of Ancestors- Store Ancestors of a node { _id: "a" } { _id: "b", ancestors: [ "a" ], parent: "a" } { _id: "c", ancestors: [ "a", "b" ], parent: "b" } { _id: "d", ancestors: [ "a", "b" ], parent: "b" } { _id: "e", ancestors: [ "a" ], parent: "a" } { _id: "f", ancestors: [ "a", "e" ], parent: "e" } { _id: "g", ancestors: [ "a", "b", "d" ], parent: "d" }

Page 59: Java Web Development with MongoDB (presented at Devoxx 2010)

Array of Ancestors- Store Ancestors of a node { _id: "a" } { _id: "b", ancestors: [ "a" ], parent: "a" } { _id: "c", ancestors: [ "a", "b" ], parent: "b" } { _id: "d", ancestors: [ "a", "b" ], parent: "b" } { _id: "e", ancestors: [ "a" ], parent: "a" } { _id: "f", ancestors: [ "a", "e" ], parent: "e" } { _id: "g", ancestors: [ "a", "b", "d" ], parent: "d" }

//find all descendants of b:>db.tree2.find({ancestors: ‘b’})

Page 60: Java Web Development with MongoDB (presented at Devoxx 2010)

Array of Ancestors- Store Ancestors of a node { _id: "a" } { _id: "b", ancestors: [ "a" ], parent: "a" } { _id: "c", ancestors: [ "a", "b" ], parent: "b" } { _id: "d", ancestors: [ "a", "b" ], parent: "b" } { _id: "e", ancestors: [ "a" ], parent: "a" } { _id: "f", ancestors: [ "a", "e" ], parent: "e" } { _id: "g", ancestors: [ "a", "b", "d" ], parent: "d" }

//find all descendants of b:>db.tree2.find({ancestors: ‘b’})

//find all ancestors of f:>ancestors = db.tree2.findOne({_id:’f’}).ancestors>db.tree2.find({_id: { $in : ancestors})

Page 61: Java Web Development with MongoDB (presented at Devoxx 2010)

findAndModifyQueue example

//Example: find highest priority job and mark

job = db.jobs.findAndModify({ query: {inprogress: false}, sort: {priority: -1), update: {$set: {inprogress: true, started: new Date()}}, new: true})

Page 62: Java Web Development with MongoDB (presented at Devoxx 2010)

Part ThreeReplication & Sharding

Page 63: Java Web Development with MongoDB (presented at Devoxx 2010)

Scaling

• Data size only goes up• Operations/sec only go up• Vertical scaling is limited• Hard to scale vertically in the cloud• Can scale wider than higher

What is scaling?Well - hopefully for everyone here.

Page 64: Java Web Development with MongoDB (presented at Devoxx 2010)

Traditional Horizontal Scaling

• read only slaves• caching• custom partitioning code

scaling isn’t newsharding isn’tmanual re-balancing is painful at best

Page 65: Java Web Development with MongoDB (presented at Devoxx 2010)

New methods of Scaling

• relational database clustering• consistent hashing (Dynamo)• range based partitioning (BigTable/PNUTS)

Page 66: Java Web Development with MongoDB (presented at Devoxx 2010)

Read Scalability : Replication

write

read

ReplicaSet  1

Primary

Secondary

Secondary

Page 67: Java Web Development with MongoDB (presented at Devoxx 2010)

Basics• MongoDB replication is a bit like MySQL replication

Asynchronous master/slave at its core• Variations:

Master / slaveReplica Pairs (deprecated – use replica sets)Replica Sets

Page 68: Java Web Development with MongoDB (presented at Devoxx 2010)

• A cluster of N servers• Any (one) node can be primary• Consensus election of primary• Automatic failover• Automatic recovery• All writes to primary• Reads can be to primary (default) or a secondary

Replica Sets

Page 69: Java Web Development with MongoDB (presented at Devoxx 2010)

Replica Sets – Design Concepts

1. Write is durable once avilable on a majority of members

2. Writes may be visible before a cluster wide commit has been completed

3. On a failover, if data has not been replicated from the primary, the data is dropped (see #1).

Page 70: Java Web Development with MongoDB (presented at Devoxx 2010)

Replica Set: Establishing

Member 1

Member 2

Member 3

Page 71: Java Web Development with MongoDB (presented at Devoxx 2010)

Replica Set: Electing primary

Member 1

Member 2PRIMARY

Member 3

Page 72: Java Web Development with MongoDB (presented at Devoxx 2010)

Replica Set: Failure of master

Member 1

Member 2DOWN

Member 3PRIMARY

negotiate new

master

Page 73: Java Web Development with MongoDB (presented at Devoxx 2010)

Replica Set: Reconfiguring

Member 1

Member 2DOWN

Member 3PRIMARY

Page 74: Java Web Development with MongoDB (presented at Devoxx 2010)

Replica Set: Member recovers

Member 1

Member 2RECOVER-

ING

Member 3PRIMARY

Page 75: Java Web Development with MongoDB (presented at Devoxx 2010)

Replica Set: Active

Member 1

Member 2

Member 3PRIMARY

Page 76: Java Web Development with MongoDB (presented at Devoxx 2010)

Set Member TypesNormal (priority == 1)Passive (priority == 0)Arbiter (no data, but can vote)

Page 77: Java Web Development with MongoDB (presented at Devoxx 2010)

Write Scalability: Sharding

write

read

ReplicaSet  1

Primary

Secondary

Secondary

ReplicaSet  2

Primary

Secondary

Secondary

ReplicaSet  3

Primary

Secondary

Secondary

key  range  0  ..  30

key  range  31  ..  60

key  range  61  ..  100

Page 78: Java Web Development with MongoDB (presented at Devoxx 2010)

Sharding

• Scale horizontally for data size, index size, write and consistent read scaling

• Distribute databases, collections or a objects in a collection

• Auto-balancing, migrations, management happen with no down time

• Replica Sets for inconsistent read scaling

for inconsistent read scaling

Page 79: Java Web Development with MongoDB (presented at Devoxx 2010)

Sharding

• Choose how you partition data• Can convert from single master to sharded system with no downtime• Same features as non-sharding single master• Fully consistent

Page 80: Java Web Development with MongoDB (presented at Devoxx 2010)

Range Based

• collection is broken into chunks by range• chunks default to 200mb or 100,000 objects

Page 81: Java Web Development with MongoDB (presented at Devoxx 2010)

Architecture

client

mongos ...mongos

mongodmongod

mongod mongod

mongod

mongod ...

Shards

mongod

mongod

mongod

ConfigServers

Page 82: Java Web Development with MongoDB (presented at Devoxx 2010)

Config Servers

• Hold meta data of where chunks are located •1 or 3 of them (3 for availability)• changes are made with 2 phase commit• if a majority are down, meta data goes read only• system is online as long as 1/3 is up

Page 83: Java Web Development with MongoDB (presented at Devoxx 2010)

Shards

• Hold the actual data •Can be master, master/slave or replica sets• Replica sets gives sharding + full auto-failover• Regular mongod processes

Page 84: Java Web Development with MongoDB (presented at Devoxx 2010)

mongos

• Sharding Router (or Switch)• Acts just like a mongod to clients• Can have 1 or as many as you want• Can run on appserver so no extra network traffic

Page 85: Java Web Development with MongoDB (presented at Devoxx 2010)

Writes

• Inserts : require shard key, routed• Removes: routed and/or scattered• Updates: routed or scattered

Page 86: Java Web Development with MongoDB (presented at Devoxx 2010)

Queries

• By shard key: routed• Sorted by shard key: routed in order• By non shard key: scatter gather• Sorted by non shard key: distributed merge sort

Page 87: Java Web Development with MongoDB (presented at Devoxx 2010)

Operations

• split: breaking a chunk into 2• migrate: move a chunk from 1 shard to another• balancing: moving chunks automatically to keep system in balance

Page 88: Java Web Development with MongoDB (presented at Devoxx 2010)

Part FourJava Development

Page 89: Java Web Development with MongoDB (presented at Devoxx 2010)

Library Choices• Raw MongoDB Driver

Map<String, Object> view of objectsRough but dynamic

• Morphia (type-safe mapper)POJOsAnnotation based (similar to JPA)Syntactic sugar and helpers

• OthersCode generators, other jvm languages

Page 90: Java Web Development with MongoDB (presented at Devoxx 2010)

MongoDB Java Driver• BSON Package

TypesEncode/DecodeDBObject (Map<String, Object>)

Nested MapsDirectly encoded to binary format (BSON)

• MongoDB PackageMongoDBObject (BasicDBObject/Builder)DB/DBColletionDBQuery/DBCursor

Page 91: Java Web Development with MongoDB (presented at Devoxx 2010)

BSON PackageTypes

int and longArray/ArrayListStringbyte[] – binDataDouble (IEEE 754 FP)Date (secs since epoch)NullBooleanJavaScript StringRegex

Page 92: Java Web Development with MongoDB (presented at Devoxx 2010)

MongoDB Package• Mongo

Connection, ThreadSafeWriteConcern*

• DBAuth, Collections getLastError()Command(), eval()RequestStart/Done

• DBCollectionInsert/Save/Find/Remove/Update/FindAndModifyensureIndex

Page 93: Java Web Development with MongoDB (presented at Devoxx 2010)

Simple ExampleDBCollection  coll  =  new  Mongo().getDB(“blogdb”);

ArrayList<String>  tags  =  new  ArrayList<String>();tags.add("comic");tags.add("adventure");

coll.save(   new  BasicDBObjectBuilder(                “author”,  “Hergé”).       append(“text”,  “Destination  Moon”).       append(“date”,  new  Date()).            append(“tags”,  tags);

Page 94: Java Web Development with MongoDB (presented at Devoxx 2010)

Simple Example, AgainDBCollection  coll  =  new  Mongo().getDB(“blogdb”);

ArrayList<String>  tags  =  new  ArrayList<String>();tags.add("comic");tags.add("adventure");

Map<String,  Object>  fields  =  new  …fields.add(“author”,  “Hergé”);  fields.add(“text”,  “Destination  Moon”);fields.add(“date”,  new  Date());fields.add(“tags”,  tags);

coll.insert(new  BasicDBObject(fields));

Page 95: Java Web Development with MongoDB (presented at Devoxx 2010)

DBObject <-> (B/J)SON{author:”kyle”,  text:“Destination  Moon”,date:    }

BasicDBObjectBuilder  dbObj  =  new  BasicDBObjectBuilder()

.append(“author”,  “Hergé”)  

.append(“text”,  “Destination  Moon”)  

.append(“date”,  new  Date())  .get();

String  text  =  (String)dbObj.get(“text”);  

Page 96: Java Web Development with MongoDB (presented at Devoxx 2010)

JSON.parse(…)DBObject  dbObj  =  JSON.parse(“   {‘author’:‘Hergé’,    ‘text’:‘Destination  Moon’,  ‘date’:‘Sat  Jul  24  2010  19:47:11  GMT-­‐0700  (PDT)’,}

”);

Page 97: Java Web Development with MongoDB (presented at Devoxx 2010)

ListsDBObject  dbObj  =  JSON.parse(“   {‘author’:‘Hergé’,    ‘text’:‘Destination  Moon’,  ‘date’:‘Sat  Jul  24  2010  19:47:11  GMT-­‐0700  (PDT)’,}

”);

List<String>  tags  =  new  …tags.add(“comic”);tags.add(“adventure”);dbObj.put(“tags”,  tags);

{…,  tags:  [‘comic’,  ‘adventure’]}

Page 98: Java Web Development with MongoDB (presented at Devoxx 2010)

Maps of MapsCan represent object graph/treeAlways keyed off String (field)

Page 99: Java Web Development with MongoDB (presented at Devoxx 2010)

Morphia: MongoDB MapperMaps POJOType-safeAccess Patterns: DAO/Datastore/???Data TypesJPA likeMany concepts came from Objectify (GAE)

Page 100: Java Web Development with MongoDB (presented at Devoxx 2010)

Annotations@Entity(“collectionName”)@Id@Transient (not transient)@Indexed(…)@Property(“fieldAlias”)@AlsoLoad({aliases})@Reference@Serialized[@Embedded]

Page 101: Java Web Development with MongoDB (presented at Devoxx 2010)

Lifecycle Events@PrePersist@PreSave@PostPersist@PreLoad@PostLoad

EntityListenersEntityInterceptor

Page 102: Java Web Development with MongoDB (presented at Devoxx 2010)

Basic POJO@Entityclass Person { @Id String author; @Indexed Date date; String text;}

Page 103: Java Web Development with MongoDB (presented at Devoxx 2010)

Datastore Basicsget(class, id)find(class, […])save(entity, […])delete(query)getCount(query)update/First(query, upOps)findAndModify/Delete(query, upOps)

Page 104: Java Web Development with MongoDB (presented at Devoxx 2010)

Add, Get, DeleteBlog  entry  =  new  Blog(“Hergé”,  New  Date(),  “Destination  Moon”)

Datastore  ds  =  new  Morphia().createDatastore()

ds.save(entry);

Blog  foundEntry  =  ds.get(Blog.class,  “Hergé”)

ds.delete(entry);

Page 105: Java Web Development with MongoDB (presented at Devoxx 2010)

QueriesDatastore  ds  =  …

Query  q  =  ds.createQuery(Blog.class);

q.field(“author”).equal(“Hergé”).limit(5);

for(Blog  e  :  q.fetch())      print(e);

Blog  entry  =  q.field(“author”).startsWith(“H”).get();

Page 106: Java Web Development with MongoDB (presented at Devoxx 2010)

UpdateDatastore  ds  =  …Query  q  =  ds.find(Blog.class,  “author”,  “Hergé”);UpdateOperation  uo  =  ds.createUpdateOperations(cls)

uo.inc(“views”,  1).set(“lastUpdated”,  new  Date());

UpdateResults  res  =  ds.update(q,  uo);if(res.getUpdatedCount()  >  0)    //do  something?

Page 107: Java Web Development with MongoDB (presented at Devoxx 2010)

Update Operationsset(field,  val)unset(field)

inc(field,  [val])dec(field)

add(field,  val)addAdd(field,  vals)

removeFirst/Last(field)removeAll(field,  vals)

Page 108: Java Web Development with MongoDB (presented at Devoxx 2010)

Relationships[@Embedded]

Loaded/Saved  with  EntityUpdate

@Reference

Stored  as  DBRef(s)Loaded  with  EntityNot  automatically  saved

Key<T>  (DBRef)

Stored  as  DBRef(s)Just  a  link,  but  resolvable  by  Datastore/Query

Page 109: Java Web Development with MongoDB (presented at Devoxx 2010)

MongoDB features in Java

• Durability• Replication• Sharding• Connection options

Page 110: Java Web Development with MongoDB (presented at Devoxx 2010)

Durability

What failures do you need to recover from?• Loss of a single database node?• Loss of a group of nodes?

Page 111: Java Web Development with MongoDB (presented at Devoxx 2010)

Durability - Master only

• Write acknowledged when in memory on master only

Page 112: Java Web Development with MongoDB (presented at Devoxx 2010)

Durability - Master + Slaves

• Write acknowledged when in memory on master + slave

• Will survive failure of a single node

Page 113: Java Web Development with MongoDB (presented at Devoxx 2010)

Durability - Master + Slaves + fsync• Write acknowledged when in memory on master + slaves

• Pick a “majority” of nodes

• fsync in batches (since it blocking)

Page 114: Java Web Development with MongoDB (presented at Devoxx 2010)

Setting default error checking//  Do  not  check  or  report  errors  on  writecom.mongodb.WriteConcern.NONE;

//  Use  default  level  of  error  check.  Do  not  send//  a  getLastError(),  but  raise  exction  on  errorcom.mongodb.WriteConcern.NORMAL;

//  Send  getLastError()  after  each  write.  Raise  an//  exception  on  errorcom.mongodb.WriteConcern.STRICT;

//  Set  the  concerndb.setWriteConcern(concern);

Page 115: Java Web Development with MongoDB (presented at Devoxx 2010)

Customized WriteConcern//  Wait  for  three  servers  to  acknowledge  writeWriteConcern  concern  =        new  WriteConcern(3);

//  Wait  for  three  servers,  with  a  1000ms  timeoutWriteConcern  concern  =        new  WriteConcern(3,  1000);

//  Wait  for  3  server,  100ms  timeout  and  fsync  //  data  to  diskWriteConcern  concern  =        new  WriteConcern(3,  1000,  true);          //  Set  the  concerndb.setWriteConcern(concern);

Page 116: Java Web Development with MongoDB (presented at Devoxx 2010)

Using Replication from Java

slaveOk()- driver to send read requests to Secondaries- driver will always send writes to Primary

Can be set on-­‐  DB.slaveOk()-­‐  Collection.slaveOk()-­‐  find(q).addOption(Bytes.QUERYOPTION_SLAVEOK);

Page 117: Java Web Development with MongoDB (presented at Devoxx 2010)

Using sharding Java

Before sharding

coll.save(  new  BasicDBObjectBuilder(“author”,  “Hergé”).       append(“text”,  “Destination  Moon”).       append(“date”,  new  Date());

Query  q  =  ds.find(Blog.class,  “author”,  “Hergé”);

After sharding

No  code  change  required!

Page 118: Java Web Development with MongoDB (presented at Devoxx 2010)

Connection options

MongoOptions  mo  =  new  MongoOptions();

//  Restrict  number  of  connectionsmo.connectionsPerHost  =  MAX_THREADS  +  5;

//  Auto  reconnection  on  connection  failuremo.autoConnectRetry  =  true;

Page 119: Java Web Development with MongoDB (presented at Devoxx 2010)

Part FiveDeploying MongoDB

Page 120: Java Web Development with MongoDB (presented at Devoxx 2010)

Part FiveDeploying MongoDB

• Performance tuning• Sizing• O/S Tuning / File System layout• Backup

Page 121: Java Web Development with MongoDB (presented at Devoxx 2010)

Backup

• Typically backups are driven from a slave• Eliminates impact to client / application traffic to master

Page 122: Java Web Development with MongoDB (presented at Devoxx 2010)

Backup

•Two strategies• mogodump / mongorestore• fsync + lock

Page 123: Java Web Development with MongoDB (presented at Devoxx 2010)

mongodump

• binary, compact object dump• each consistent object is written• not necessarily consistent from start to finish

Page 124: Java Web Development with MongoDB (presented at Devoxx 2010)

fsync + lock

• fsync - flushes buffers to disk• lock - blocks writes

db.runCommand({fsync:1,lock:1})

• Use file-system / LVM / storage snapshot

• unlock db.$cmd.sys.unlock.findOne();

Page 125: Java Web Development with MongoDB (presented at Devoxx 2010)

Slave delay

• Protection against app faults• Protection against administration mistakes

Page 126: Java Web Development with MongoDB (presented at Devoxx 2010)

O/S Config

• RAM - lots of it

• Filesystem• EXT4 / XFS• Better file allocation & performance

• I/O• More disk the better• Consider RAID10 or other RAID configs

Page 127: Java Web Development with MongoDB (presented at Devoxx 2010)

Monitoring

• Munin, Cacti, Nagios

Primary function: • Measure stats over time• Tells you what is going on with your system• Alerts when threshold reached

Page 128: Java Web Development with MongoDB (presented at Devoxx 2010)

Remember me?

Page 129: Java Web Development with MongoDB (presented at Devoxx 2010)

Summary

MongoDB makes building Java Web application simple

You can focus on what the apps needs to do

MongoDB has built-in

• Horizontal scaling (reads and writes)• Simplified schema evolution• Simplified deployed and operation• Best match for development tools and agile processes