65
MongoDB and Spring Data Chris Harris Twitter : cj_harris5 Email : [email protected] © 2012 SpringOne 2GX. All rights reserved. Do not distribute without permission.

The Spring Data MongoDB Project

  • Upload
    mongodb

  • View
    3.113

  • Download
    0

Embed Size (px)

Citation preview

Page 1: The Spring Data MongoDB Project

MongoDB and Spring DataChris Harris

Twitter : cj_harris5Email : [email protected]

© 2012 SpringOne 2GX. All rights reserved. Do not distribute without permission.

Page 2: The Spring Data MongoDB Project

22

Solution Architect at 10gen (the company behind MongoDB)

- Previous Roles:

• EMEA Architect at SpringSource• EMEA Principal JBoss Consultant at RedHat

About Me

Page 5: The Spring Data MongoDB Project

http://daddytypes.com/archive/duct_tape_baby_mianro.jpg

Page 6: The Spring Data MongoDB Project

Is there a better way?

Page 7: The Spring Data MongoDB Project

Terminology

RDBMS MongoDBTable CollectionRow(s) JSON DocumentIndex IndexJoin Embedding & LinkingPartition ShardPartition Key Shard Key

Page 8: The Spring Data MongoDB Project

As simple as possible, but no simpler

Depth of functionality

Sca

labi

lity

& P

erfo

rman

ceMemcached

Key / Value

RDBMS

MongoDB

Page 9: The Spring Data MongoDB Project

Lets build a Spring Data MongoDB App!

Page 10: The Spring Data MongoDB Project

Welcome to Dev News• User Story 1:

– As a user I should be able to create “News Articles”

Page 11: The Spring Data MongoDB Project

What is a News Article?{ title: “What is new in 2.2 MongoDB”, date : new Date(), author : “Fred” text : “.......” }

Page 12: The Spring Data MongoDB Project

Example 1 - Create

Page 13: The Spring Data MongoDB Project

Welcome to MongoDB News Dev• User Story 1:

– As a user I should be able to create “News Articles”• User Story 2:

– As a user I should be able to find a “News Articles” by Name

Page 14: The Spring Data MongoDB Project

Example 2 - Query

Page 15: The Spring Data MongoDB Project

Example 3 - Repo

Page 16: The Spring Data MongoDB Project

Welcome to MongoDB News Dev• User Story 1:

– As a user I should be able to create “News Articles”• User Story 2:

– As a user I should be able to find a “News Articles” by Name• User Story 3:

– As a user I should be able to comment on “News Articles”

Page 17: The Spring Data MongoDB Project

What is a Comment?{ title: “What is new in 2.2 MongoDB”, date : new Date(), author : “Fred” text : “.......” tags : [“mongodb”, “nosql”] comments : [ {author : “chris”, text: “this is great” , date : new Date()}, {author : “tom”, text: “rock on”, date : new Date()} ] }

Page 18: The Spring Data MongoDB Project

Example 4 - Comments

Page 19: The Spring Data MongoDB Project

Welcome to MongoDB News Dev• User Story 1:

– As a user I should be able to create “News Articles”• User Story 2:

– As a user I should be able to find a “News Articles” by Name• User Story 3:

– As a user I should be able to comment on “News Articles”• User Story 4:

–As a user I want to know how many comments are on a “Articles”

Page 20: The Spring Data MongoDB Project

What is a Comment?{ title: “What is new in 2.2 MongoDB”, .... comments : [ {author : “chris”, text: “this is great” , date : new Date()}, {author : “tom”, text: “rock on”, date : new Date()} ] , comments: count : 2}

Page 21: The Spring Data MongoDB Project

Example 5 - Inc

Page 22: The Spring Data MongoDB Project

“MongoDB lost my data!!”

Page 23: The Spring Data MongoDB Project

Data: 125647383885969795743

Page 24: The Spring Data MongoDB Project

Least durability - Don't use!

Driver MongoDB

apply in memory

write

Page 25: The Spring Data MongoDB Project

Wait for Journal Sync - Same as RDBMSDriver MongoDB

apply in memory

write

j:trueWrite to journal

Page 26: The Spring Data MongoDB Project

Replication

http://www.flickr.com/photos/10335017@N07/4570943043

Page 27: The Spring Data MongoDB Project

Replica Set• Data Protection• Multiple copies of the data• Spread across Data Centers, AZs• High Availability• Automated Failover• Automated Recovery

Page 28: The Spring Data MongoDB Project

Primary

Secondary

Secondary

Read

Write

Java App

Asynchronous Replication

Replica Sets

Page 29: The Spring Data MongoDB Project

Replica Sets

Primary

Secondary

Secondary

Read

Write

Java App

Page 30: The Spring Data MongoDB Project

Replica Sets

Primary

Primary

Secondary

Read

Write Automatic Election of new PrimaryJava App

Page 31: The Spring Data MongoDB Project

Replica Sets

Recovering

Primary

Secondary

Read

Write New primary serves dataJava App

Page 32: The Spring Data MongoDB Project

Replica Sets

Secondary

Primary

Secondary

Read

WriteJava App

Page 33: The Spring Data MongoDB Project

Durability SummaryMemory Journal Secondary

Other Data Center

RDBMS

Default"Fire & Forget"

w=1

w=1j=true

w="majority"w=n

w="myTag"

Less More

Page 34: The Spring Data MongoDB Project

Example 6 - Write Concern

Page 35: The Spring Data MongoDB Project

Eventual Consistency

• http://www.flickr.com/photos/26095468@N04/3779692985

Page 36: The Spring Data MongoDB Project

Understanding Eventual Consistency

Primary Secondary

v1

Thread #1

Insert

Page 37: The Spring Data MongoDB Project

Understanding Eventual Consistency

Primary SecondaryThread #1

Insert

v1

v1

Page 38: The Spring Data MongoDB Project

Understanding Eventual Consistency

Primary SecondaryThread #1

Insert

Readv1

v1

Page 39: The Spring Data MongoDB Project

Understanding Eventual Consistency

Primary SecondaryThread #1

Insert

Readreads v1v1

v1

Page 40: The Spring Data MongoDB Project

Understanding Eventual Consistency

Primary SecondaryThread #1

Insert

v2Update

Readreads v1v1

v1

Page 41: The Spring Data MongoDB Project

Understanding Eventual Consistency

Primary SecondaryThread #1

Insert

Update

Read

v2

reads v1

v2

v1

v1

Page 42: The Spring Data MongoDB Project

Understanding Eventual Consistency

Primary SecondaryThread #1

Insert

Update

Read

Read

reads v1

v2

v2

v1

v1

Page 43: The Spring Data MongoDB Project

Understanding Eventual Consistency

Primary SecondaryThread #1

Insert

Update

Read

Readreads v2

reads v1

v2

v2

v1

v1

Page 44: The Spring Data MongoDB Project

Understanding Eventual Consistency

Primary SecondaryThread #1

Insert

Update

Read

Read

Thread #2

v2

v2

v1

v1

Page 45: The Spring Data MongoDB Project

Understanding Eventual Consistency

Primary SecondaryThread #1

Insert

Update

Read

Read

Thread #2

v2

v2

v1

v1

Page 46: The Spring Data MongoDB Project

Understanding Eventual Consistency

Primary SecondaryThread #1

Insert

Update

Read

Read

Thread #2

v2

v2

v1

v1

v1 does not exist on

Secondary

Page 47: The Spring Data MongoDB Project

Understanding Eventual Consistency

Primary SecondaryThread #1

Insert

Update

Read

Read

Thread #2

v2

v2

v1

v1

v1 does not exist on

Secondary

Reads v1 but Primary at v2

Read v2

Page 48: The Spring Data MongoDB Project

Example 7 - Eventual Consistency

Page 49: The Spring Data MongoDB Project

http://community.qlikview.com/cfs-filesystemfile.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/theqlikviewblog/Cutting-Grass-with-Scissors-_2D00_-2.jpg

Page 50: The Spring Data MongoDB Project

http://www.bitquill.net/blog/wp-content/uploads/2008/07/pack_of_harvesters.jpg

Page 51: The Spring Data MongoDB Project

MongoDB Sharding• Automatic partitioning and management

• Range based

• Convert to sharded system with no downtime

• Fully consistent

Page 52: The Spring Data MongoDB Project

How mongoDB Sharding works

• Range keys from -∞ to +∞ • Ranges are stored as "chunks"

-∞  +∞  

> db.runCommand({addshard: "shard1"});> db.runCommand({shardCollection: "mydb.users", key: {age: 1}})

Page 53: The Spring Data MongoDB Project

How mongoDB Sharding works

• Data in inserted• Ranges are split into more "chunks"

-∞  +∞  

-∞  40 41 +∞  

> db.users.save({age: 40})

Page 54: The Spring Data MongoDB Project

How mongoDB Sharding works

-∞  +∞  

-∞  40 41 +∞  

41 50 51 +∞  

61 +∞  51 60

> db.users.save({age: 40})> db.users.save({age: 50})> db.users.save({age: 60})

Page 55: The Spring Data MongoDB Project

-∞  4041 50

61 +∞  

51 60

> db.runCommand({addshard: "shard2"});> db.runCommand({addshard: "shard3"});

How mongoDB Sharding works

Page 56: The Spring Data MongoDB Project

-∞  4041 50

61 +∞  

51 60

shard1

> db.runCommand({addshard: "shard2"});> db.runCommand({addshard: "shard3"});

How mongoDB Sharding works

Page 57: The Spring Data MongoDB Project

-∞  4041 50

61 +∞  

51 60

shard1 shard2 shard3

> db.runCommand({addshard: "shard2"});> db.runCommand({addshard: "shard3"});

How mongoDB Sharding works

Page 58: The Spring Data MongoDB Project

Architecture

C1

C2

C3

Config Serversmongos mongos

app app

mongod

Shard 1

mongod

mongod

mongod

Shard 2

mongod

mongod

Shard 4

mongod

Shard 3

mongod

mongod

Replica Set

mongod

mongod

mongod

Page 59: The Spring Data MongoDB Project

Balancing

config

config

config

Shard 1 Shard 2 Shard 4Shard 3

mongosbalancer

5

9

1

6

10

2

7

11

3

8

12

4

13 14 15 16 17 18 19 20 21 22 23 24

ImbalanceData inserted all in a range

mongos

Page 60: The Spring Data MongoDB Project

Balancing

config

config

config

Shard 1 Shard 2 Shard 4Shard 3

mongosbalancer

5

9

1

6

10

2

7

11

3

8

12

4

13 14 15 16 17 18 19 20 21 22 23 24

Move chunk 1 to Shard 2

mongos

Page 61: The Spring Data MongoDB Project

Balancing

config

config

config

Shard 1 Shard 2 Shard 4Shard 3

balancer

5

9

6

10

2

7

11

3

8

12

4

13 14 15 16 17 18 19 20 21 22 23 24

1

mongos

Page 62: The Spring Data MongoDB Project

Balancing

config

config

config

Shard 1 Shard 2 Shard 4Shard 3

balancer

5

9

6

10

2

7

11

3

8

12

4

13 14 15 16 17 18 19 20 21 22 23 24

1

mongos

Page 63: The Spring Data MongoDB Project

Balancing

config

config

config

Shard 1 Shard 2 Shard 4Shard 3

mongosbalancer

5

9

16

10

2

7

11

3

8

12

4

13 14 15 16 17 18 19 20 21 22 23 24

mongos

Page 64: The Spring Data MongoDB Project

Example 8 - Sharding

Page 65: The Spring Data MongoDB Project

• MongoDB 2.2– Improved concurrency–Tag-aware sharding–Aggregation framework–TTL collections

• Free online MongoDB training–Develop–Deploy–Classes start Oct. 2012

Questions