inside mongodb: the internals of an open-source database

25
Mike Dirolf @mdirolf 10gen, Inc. http://www.mongodb.org

Upload: mike-dirolf

Post on 17-Dec-2014

26.443 views

Category:

Technology


7 download

DESCRIPTION

Slides from a talk about MongoDB internals given at Gluecon 2010.

TRANSCRIPT

Page 1: Inside MongoDB: the Internals of an Open-Source Database

Mike  Dirolf  -­‐  @mdirolf  -­‐  10gen,  Inc.

http://www.mongodb.org

Page 2: Inside MongoDB: the Internals of an Open-Source Database

Inside

http://www.flickr.com/photos/tmh9/677919415/

Page 3: Inside MongoDB: the Internals of an Open-Source Database

this  talk  might  be  a  bit  “hard”,but  MongoDB  is  really  easy:http://try.mongodb.org

a word of warning

Page 4: Inside MongoDB: the Internals of an Open-Source Database

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

Page 5: Inside MongoDB: the Internals of an Open-Source Database

_id

if  not  specified  drivers  will  add  default:

ObjectId("4bface1a2231316e04f3c434")timestamp

machine  idprocess  id

counter

http://www.mongodb.org/display/DOCS/Object+IDs

Page 6: Inside MongoDB: the Internals of an Open-Source Database

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

http://bsonspec.org

Page 7: Inside MongoDB: the Internals of an Open-Source Database

Insert Message (TCP/IP)

http://www.mongodb.org/display/DOCS/Mongo+Wire+Protocol

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

Page 8: Inside MongoDB: the Internals of an Open-Source Database

Data File Allocation

$  ls  -­‐sk  /data/db/  16384  foo.ns  65536  foo.0131072  foo.1  16384  bar.ns              ...

}allocated  per  database{double  in  size  

(up  to  2  gigs)

Page 9: Inside MongoDB: the Internals of an Open-Source Database

Memory Management

Page 10: Inside MongoDB: the Internals of an Open-Source Database

Extent Allocationfoo.0

foo.1

foo.2

00000000000000000000000000000000000000000000000000000000000000000000000000000

preallocated  space

00000000000

0000

foo.$freelistfoo.bazfoo.barfoo.test

allocated  per  namespace:

ns  details  stored  in  foo.ns

Page 11: Inside MongoDB: the Internals of an Open-Source Database

Deleted  Record  (Size,  Offset,  Next)

BSON  Data

Header  (Size,  Offset,  Next,  Prev)

Record Allocation

Padding

...

...

Page 12: Inside MongoDB: the Internals of an Open-Source Database

Indexing

B-­‐Tree  indexes,  stored  in  own  namespaces

>  db.system.namespaces.find(){  "name"  :  "foo.system.indexes"  }{  "name"  :  "foo.test"  }{  "name"  :  "foo.test.$_id_"  }

http://www.mongodb.org/display/DOCS/Indexes

Page 13: Inside MongoDB: the Internals of an Open-Source Database

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

Page 14: Inside MongoDB: the Internals of an Open-Source Database

Query Language

“query  by  example”  plus  $  modifiers:

{first_name:  “Mike”,  age:  {$gte:  20,  $lt:  40}}

http://www.mongodb.org/display/DOCS/Advanced+Queries

Page 15: Inside MongoDB: the Internals of an Open-Source Database

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...

Page 16: Inside MongoDB: the Internals of an Open-Source Database

Query Optimizerfind({x:  10,  y:  “foo”})

   scan

   index  on  x

   index  on  y remember

terminate

Page 17: Inside MongoDB: the Internals of an Open-Source Database

db.foo.drop()

Page 18: Inside MongoDB: the Internals of an Open-Source Database

Commands

drop,  count,  copydb,  findAndModify,  serverStatus,  ...

http://www.mongodb.org/display/DOCS/Commands

Page 19: Inside MongoDB: the Internals of an Open-Source Database

db.$cmd.find({drop:  "foo"}).limit(-­‐1);

db.foo.drop();

db.foo.runCommand({drop:  "foo"});

db.$cmd.findOne({drop:  "foo"});

=

=

=

Page 20: Inside MongoDB: the Internals of an Open-Source Database

Capped Collections

http://www.mongodb.org/display/DOCS/Capped+Collections

preallocatedauto  LRI  age-­‐out

no  default  _id  indexalways  in  insertion  order

Page 21: Inside MongoDB: the Internals of an Open-Source Database

Replication Oplog

http://www.mongodb.org/display/DOCS/Replication

>  use  localswitched  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  fooswitched  to  db  foo

>  db.test.insert({x:  1,  url:  "http://dirolf.com"});

>  db.test.update({url:  "http://dirolf.com"},  {$inc:  {x:  1}});

Page 22: Inside MongoDB: the Internals of an Open-Source Database

Replication Topology

master

slave slaveslave

master slave

master slave

master master

slave master

http://www.mongodb.org/display/DOCS/Replication

Page 23: Inside MongoDB: the Internals of an Open-Source Database

Auto-Sharding

client

mongos ...mongos

mongodmongod

mongod mongod

mongod

mongod...

Shards

mongod

mongod

mongod

ConfigServers

http://www.mongodb.org/display/DOCS/Sharding

Page 24: Inside MongoDB: the Internals of an Open-Source Database

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:

http://www.mongodb.org/display/DOCS/Geospatial+Indexing

Page 25: Inside MongoDB: the Internals of an Open-Source Database

Download MongoDBhttp://www.mongodb.org

these  slides  are  available  at  http://dirolf.com