Slide 1

Slide 1 text

GitHub John Nunemaker MongoSF 2012 May 4, 2012 MongoDB for Analytics A loving conversation with @jnunemaker

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Background How hernias can be good for you

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

1 month Of evenings and weekends

Slide 7

Slide 7 text

1 year Since public launch

Slide 8

Slide 8 text

13 tiny servers 2 web, 6 app, 3 db, 2 queue

Slide 9

Slide 9 text

7-8 Million Page views per day

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

Implementation Imma show you how we do what we do baby

Slide 15

Slide 15 text

Doing It (mostly) Live No aggregate querying

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

get('/track.gif') do track_service.record(...) TrackGif end

Slide 19

Slide 19 text

class TrackService def record(attrs) message = MessagePack.pack(attrs) @client.set(@queue, message) end end

Slide 20

Slide 20 text

class TrackProcessor def run loop { process } end def process record @client.get(@queue) end def record(message) attrs = MessagePack.unpack(message) Hit.record(attrs) end end

Slide 21

Slide 21 text

http://bit.ly/rt-kestrel

Slide 22

Slide 22 text

class Hit def record site.atomic_update(site_updates) Resolution.record(self) Technology.record(self) Location.record(self) Referrer.record(self) Content.record(self) Search.record(self) Notification.record(self) View.record(self) end end

Slide 23

Slide 23 text

class Resolution def record(hit) query = {'_id' => "..."} update = {'$inc' => {}} update['$inc']["sx.#{hit.screenx}"] = 1 update['$inc']["bx.#{hit.browserx}"] = 1 update['$inc']["by.#{hit.browsery}"] = 1 collection(hit.created_on) .update(query, update, :upsert => true) end end end

Slide 24

Slide 24 text

Pros

Slide 25

Slide 25 text

Pros Space

Slide 26

Slide 26 text

Pros Space RAM

Slide 27

Slide 27 text

Pros Space RAM Reads

Slide 28

Slide 28 text

Pros Space RAM Reads Live

Slide 29

Slide 29 text

Cons

Slide 30

Slide 30 text

Cons Writes

Slide 31

Slide 31 text

Cons Writes Constraints

Slide 32

Slide 32 text

Cons Writes Constraints More Forethought

Slide 33

Slide 33 text

Cons Writes Constraints More Forethought No raw data

Slide 34

Slide 34 text

http://bit.ly/rt-counters http://bit.ly/rt-counters2

Slide 35

Slide 35 text

Time Frame Minute, hour, month, day, year, forever?

Slide 36

Slide 36 text

# of Variations One document vs many

Slide 37

Slide 37 text

Single Document Per Time Frame

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

{ "t" => 336381, "u" => 158951, "2011" => { "02" => { "18" => { "t" => 9, "u" => 6 } } } }

Slide 40

Slide 40 text

{ '$inc' => { 't' => 1, 'u' => 1, '2011.02.18.t' => 1, '2011.02.18.u' => 1, } }

Slide 41

Slide 41 text

Single Document For all ranges in time frame

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

{ "_id" =>"...:10", "bx" => { "320" => 85, "480" => 318, "800" => 1938, "1024" => 5033, "1280" => 6288, "1440" => 2323, "1600" => 3817, "2000" => 137 }, "by" => { "480" => 2205, "600" => 7359,

Slide 44

Slide 44 text

"600" => 7359, "768" => 4515, "900" => 3833, "1024" => 2026 }, "sx" => { "320" => 191, "480" => 179, "800" => 195, "1024" => 1059, "1280" => 5861, "1440" => 3533, "1600" => 7675, "2000" => 1279 } }

Slide 45

Slide 45 text

{ '$inc' => { 'sx.1440' => 1, 'bx.1280' => 1, 'by.768' => 1, } }

Slide 46

Slide 46 text

Many Documents Search terms, content, referrers...

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

[ { "_id" => ":", "t" => "ruby class variables", "sid" => BSON::ObjectId(''), "v" => 352 }, { "_id" => ":", "t" => "ruby unless", "sid" => BSON::ObjectId(''), "v" => 347 }, ]

Slide 49

Slide 49 text

Writes {'_id' => "#{sid}:#{hash}"}

Slide 50

Slide 50 text

Reads [['sid', 1], ['v', -1]]

Slide 51

Slide 51 text

Growth Don’t say shard, don’t say shard...

Slide 52

Slide 52 text

Partition Hot Data Currently using collections for time frames

Slide 53

Slide 53 text

Bigger, Faster Server More CPU, RAM, Disk Space

Slide 54

Slide 54 text

Users Sites Content Referrers Terms Engines Resolutions Locations Users Sites Content Referrers Terms Engines Resolutions Locations

Slide 55

Slide 55 text

Partition by Function Spread writes across a few servers

Slide 56

Slide 56 text

Users Sites Content Referrers Terms Engines Resolutions Locations

Slide 57

Slide 57 text

Partition by Server Spread writes across a ton of servers, way down the road, not worried yet

Slide 58

Slide 58 text

GitHub Thank you! [email protected] John Nunemaker MongoSF 2012 May 4, 2012 @jnunemaker