Slide 1

Slide 1 text

in-­‐  and  outside Mike Dirolf @mdirolf mike@corp.fiesta.cc

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Horizontally Scalable Architectures

Slide 6

Slide 6 text

Horizontally Scalable Architectures no  joins no  complex  transactions +

Slide 7

Slide 7 text

New Data Models no  joins no  complex  transactions +

Slide 8

Slide 8 text

New Data Models improved  ways  to  develop  applications?

Slide 9

Slide 9 text

JSON-style Documents {“hello”:  “world”}

Slide 10

Slide 10 text

A Fiesta User {addresses:  ["[email protected]",                          "[email protected]"],  first:  "Mike",  last:  "Dirolf",  pw:  "...",  groups:  [      {address:  "[email protected]",        name:  "family",        id:  ObjectId("...")},      {address:  "[email protected]",        name:  "rovers",        id:  ObjectId("...")}],  owned:  [ObjectId("...")]}

Slide 11

Slide 11 text

Flexible “Schemas” {“author”:  “mike”,  “text”:  “...”} {“author”:  “eliot”,  “text”:  “...”,  “tags”:  [“mongodb”]}

Slide 12

Slide 12 text

To munge, or not to munge...

Slide 13

Slide 13 text

if user.get("munge", True): do_munge(...)

Slide 14

Slide 14 text

Query Language “query  by  example”  plus  $  modifiers: {first:  “Mike”,  age:  {$gte:  20,  $lt:  40}}

Slide 15

Slide 15 text

Dynamic Queries db.users.find({“groups.id”:  ObjectId(...)}) db.users.find({...}).sort({last:  -­‐1})

Slide 16

Slide 16 text

Indexing db.users.ensureIndex({“groups.id”:  1})

Slide 17

Slide 17 text

What’s Easy About MongoDB Indexing? It’s  almost  the  same  as  in  your   RDBMS

Slide 18

Slide 18 text

What’s Hard About MongoDB Indexing? It’s  almost  the  same  as  in  your   RDBMS

Slide 19

Slide 19 text

Indexes Maintain Order Index  on  {a:  1} {a:  0,  b:  9} {a:  2,  b:  0} {a:  7,  b:  1} {a:  3,  b:  2} {a:  3,  b:  5} {a:  3,  b:  7} {a:  9,  b:  1}

Slide 20

Slide 20 text

Indexes Maintain Order Index  on  {a:  1,  b:  -­‐1} {a:  0,  b:  9} {a:  2,  b:  0} {a:  7,  b:  1} {a:  3,  b:  7} {a:  3,  b:  2} {a:  3,  b:  5} {a:  9,  b:  1}

Slide 21

Slide 21 text

B-tree Structure Index  on  {a:  1} {...}  {...}  {...}  {...}  {...}  {...}  {...}  {...}  {...}  {...}  {...} [-∞, 5) [5, 10) [10, ∞) [5, 7) [7, 9) [9, 10) [10, ∞) buckets [-∞, 5) buckets

Slide 22

Slide 22 text

Query for {a: 7} {...}  {...}  {...}  {...}  {...}  {...}  {...}  {...}  {...}  {...}  {...} [-∞, 5) [5, 10) [10, ∞) [5, 7) [7, 9) [9, 10) [10, ∞) buckets [-∞, 5) buckets Index Scan

Slide 23

Slide 23 text

Picking an Index find({x:  10,  y:  “foo”})    scan    index  on  x    index  on  y remember terminate

Slide 24

Slide 24 text

Replica Sets primary secondary secondary offline secondary secondary offline secondary primary secondary secondary primary

Slide 25

Slide 25 text

Replication @ Fiesta primary passive secondary primary backups backups

Slide 26

Slide 26 text

Auto-sharding client mongos ... mongos mongod mongod mongod mongod mongod mongod ... Shards mongod mongod mongod Config Servers

Slide 27

Slide 27 text

db.test.insert({hello:  “world”})

Slide 28

Slide 28 text

_id if  not  specified  drivers  will  add  default: ObjectId("4bface1a2231316e04f3c434") timestamp machine  id process  id counter

Slide 29

Slide 29 text

BSON Encoding {_id:  ObjectId(XXXXXXXXXXXX),  hello:  “world”} \x27\x00\x00\x00\x07      _      i      d\x00      X          X      X      X      X      X      X      X      X      X      X      X\x02      h      e      l      l      o\x00\x06\x00 \x00\x00      w      o      r      l      d\x00\x00

Slide 30

Slide 30 text

Insert Message (TCP/IP) message  length request  id response  id op  code  (insert) \x68\x00\x00\x00 \xXX\xXX\xXX\xXX \x00\x00\x00\x00 \xd2\x07\x00\x00 reserved collection  name document(s) \x00\x00\x00\x00 f  o  o  .  t  e  s  t  \x00 BSON  Data

Slide 31

Slide 31 text

Data File Allocation $  ls  -­‐sk  /data/db/  16384  foo.ns  65536  foo.0 131072  foo.1  16384  bar.ns              ... }allocated  per   database { double  in  size   (up  to  2  gigs)

Slide 32

Slide 32 text

Memory Management

Slide 33

Slide 33 text

Extent Allocation foo.0 foo.1 foo.2 00000000000 00000000000 00000000000 00000000000 00000000000 00000000000 00000000000 preallocated  space 00000000000 0000 foo.$freelist foo.baz foo.bar foo.test allocated  per  namespace: ns  details  stored  in   foo.ns

Slide 34

Slide 34 text

Deleted  Record  (Size,  Offset,  Next) BSON  Data Header  (Size,  Offset,  Next,  Prev) Record Allocation Padding ... ...

Slide 35

Slide 35 text

Cursors >  var  c  =  db.test.find({x:  20}).skip(20).limit(10) >  c.next() >  c.next()      ... query first  N  results  +  cursor  id getMore  w/  cursor  id next  N  results  +  cursor  id  or  0 ...

Slide 36

Slide 36 text

Download MongoDB and  try

Slide 37

Slide 37 text

Capped Collections preallocated auto  LRI  age-­‐out no  default  _id  index always  in  insertion  order

Slide 38

Slide 38 text

Replication Oplog >  use  local switched  to  db  local >  db.oplog.$main.find() {ts:  ...,  op:  "n",  ns:  "",  o:  {}} {ts:  ...,  op:  "n",  ns:  "",  o:  {}} {ts:  ...,  op:  "i",  ns:  "foo.test",    o:  {_id:  ObjectId("..."),          x:  1,          url:  "http://dirolf.com"}} {ts:  ...,  op:  "n",  ns:  "",  o:  {}} {ts:  ...,  op:  "u",  ns:  "foo.test",  o2:  {_id:  ObjectId("...")},  o:  {$set:  {x:  2}}} >  use  foo switched  to  db  foo >  db.test.insert({x:  1,  url:   "http://dirolf.com"}); >  db.test.update({url:  "http:// dirolf.com"},  {$inc:  {x:  1}});

Slide 39

Slide 39 text

Geohashing (20,  10) (0001  0100,  0000  1010) 0000  0010  0110  0100 0000  0010  0110  0011 tricky  part  happens  at  bit-­‐flips  (127  vs  128) maps  close  coordinates  (21,  9)  to  close  hashes: