Scaling with mongo db - SF Mongo User Group 7-19-2011

36
Scaling with MongoDB Jared Rosoff (jsr @10gen.com ) - @forjared

description

My talk from SF Mongo User Group on 7-19-2011

Transcript of Scaling with mongo db - SF Mongo User Group 7-19-2011

Page 1: Scaling with mongo db - SF Mongo User Group 7-19-2011

Scaling with MongoDB

Jared Rosoff ([email protected]) - @forjared

Page 2: Scaling with mongo db - SF Mongo User Group 7-19-2011
Page 3: Scaling with mongo db - SF Mongo User Group 7-19-2011

How do we do it today?

• We use a relational database but … – We don’t use joins – We don’t use transactions – We add read-only slaves– We added a caching layer– We de-normalized our data– We implemented custom sharding– We buy bigger servers

Page 4: Scaling with mongo db - SF Mongo User Group 7-19-2011

HOW’S THAT WORKING OUT FOR YOU?

Page 5: Scaling with mongo db - SF Mongo User Group 7-19-2011

Costs go up

Launch

+30

Days

+90

Days

+6

Months

+1 Year

Page 6: Scaling with mongo db - SF Mongo User Group 7-19-2011

Productivity goes downProject Start

De-normalize data model

Stop using joins

Custom caching layer

Custom sharding

Page 7: Scaling with mongo db - SF Mongo User Group 7-19-2011

By engineers, for engineers

Page 8: Scaling with mongo db - SF Mongo User Group 7-19-2011

The landscape

Depth of functionality

Sca

labi

lity

& P

erfo

rma

nce

MemcachedKey / Value

RDBMS

Page 9: Scaling with mongo db - SF Mongo User Group 7-19-2011

Scaling your app

1. Use documents 2. Indexes make me happy3. Knowing your working set4. Disks are the bottleneck5. Replication makes reading fun6. Sharding for profit

Page 10: Scaling with mongo db - SF Mongo User Group 7-19-2011

SCALING YOUR DATA MODEL

Page 11: Scaling with mongo db - SF Mongo User Group 7-19-2011

Documents

{ author : "roger", date : "Sat Jul 24 2010 19:47:11 GMT-0700 (PDT)", text : "Spirited Away", tags : [ "Tezuka", "Manga" ], comments : [ { author : "Fred", date : "Sat Jul 24 2010 20:51:03 GMT-0700 (PDT)", text : "Best Movie Ever” } ]}

Page 12: Scaling with mongo db - SF Mongo User Group 7-19-2011

Disk Seeks & Data Locality

Seek = 5+ ms Read = really really fast

Page 13: Scaling with mongo db - SF Mongo User Group 7-19-2011

Disk Seeks & Data Locality

Post

AuthorCommen

t

Page 14: Scaling with mongo db - SF Mongo User Group 7-19-2011

Disk Seeks & Data LocalityPost

Author

Comment

Comment

Comment

Comment

Comment

Page 15: Scaling with mongo db - SF Mongo User Group 7-19-2011

OPTIMIZED INDEXES

Page 16: Scaling with mongo db - SF Mongo User Group 7-19-2011

Table scans

1 2 3 4 5 6 7

Looked at 7 objects

Find where x equals 7

Page 17: Scaling with mongo db - SF Mongo User Group 7-19-2011

Tree Lookup

7

6

5

4

3

2

1

Looked at 3 objects

Find where x equals 7

Page 18: Scaling with mongo db - SF Mongo User Group 7-19-2011

Random Index

Entire index must fit in RAM

Page 19: Scaling with mongo db - SF Mongo User Group 7-19-2011

Right AlignedOnly small portion in

RAM

Page 20: Scaling with mongo db - SF Mongo User Group 7-19-2011

WORKING SET SIZE

Page 21: Scaling with mongo db - SF Mongo User Group 7-19-2011

Disk

Working Set

RAM

Active Documents

+ Used Indexes

Page 22: Scaling with mongo db - SF Mongo User Group 7-19-2011

Disk

Page Fault

RAM

App

1

23

4

5

1. App requests document

2. Document not in memory

3. Evict a page from memory

4. Read block from disk

5. Return document from memory

Page 23: Scaling with mongo db - SF Mongo User Group 7-19-2011

Figuring out working Set> db.foo.stats() {

"ns" : "test.foo","count" : 1338330,"size" : 46915928,"avgObjSize" : 35.05557523181876,"storageSize" : 86092032,"numExtents" : 12,"nindexes" : 2,"lastExtentSize" : 20872960,"paddingFactor" : 1,"flags" : 0,"totalIndexSize" : 99860480,"indexSizes" : {

"_id_" : 55877632,"x_1" : 43982848

},"ok" : 1

}

Size of data

Size on disk (and in

memory!)

Size of all indexes

Average document

size

Size of each index

Page 24: Scaling with mongo db - SF Mongo User Group 7-19-2011

DISK CONFIGURATIONS

Page 25: Scaling with mongo db - SF Mongo User Group 7-19-2011

Single Disk

~200 seeks / second

Page 26: Scaling with mongo db - SF Mongo User Group 7-19-2011

RAID0

~200 seeks / second ~200 seeks / second ~200 seeks / second

Page 27: Scaling with mongo db - SF Mongo User Group 7-19-2011

RAID10

~400 seeks / second ~400 seeks / second ~400 seeks / second

Page 28: Scaling with mongo db - SF Mongo User Group 7-19-2011

REPLICATION

Page 29: Scaling with mongo db - SF Mongo User Group 7-19-2011

Replica Sets

Primary

Secondary

Secondary

Read / Write

Read

Read

Page 30: Scaling with mongo db - SF Mongo User Group 7-19-2011

Replica SetsPrimary

Secondary

Secondary

Read / Write

Read

Read

Secondary

SecondaryRead

Read

Page 31: Scaling with mongo db - SF Mongo User Group 7-19-2011

SHARDING

Page 32: Scaling with mongo db - SF Mongo User Group 7-19-2011

Shard 10..10

Shard 210..20

Shard 320..30

Shard 430..40

Primary

Secondary

Secondary

Primary

Secondary

Secondary

Primary

Secondary

Secondary

Primary

Secondary

Secondary

MongoS MongoS

Page 33: Scaling with mongo db - SF Mongo User Group 7-19-2011

400GB Index?

Page 34: Scaling with mongo db - SF Mongo User Group 7-19-2011

Shard 10..10

Shard 210..20

Shard 320..30

Shard 430..40

400GB Index?

100GBIndex!

100GBIndex!

100GBIndex!

100GBIndex!

Page 35: Scaling with mongo db - SF Mongo User Group 7-19-2011

SUMMARY

Page 36: Scaling with mongo db - SF Mongo User Group 7-19-2011

Summary

1. Use documents to your advantage!2. Optimize your indexes 3. Understand your working set4. Use a sane disk configuratino5. Use replicas to scale reads 6. Use sharding to scale writes &

working RAM