SlideShare a Scribd company logo
Mike	
  Dirolf	
  -­‐	
  @mdirolf	
  -­‐	
  10gen,	
  Inc.

                                            http://www.mongodb.org
Inside




  http://www.flickr.com/photos/tmh9/677919415/
a word of warning
  this	
  talk	
  might	
  be	
  a	
  bit	
  “hard”,
  but	
  MongoDB	
  is	
  really	
  easy:
  http://try.mongodb.org
db.test.insert({hello:	
  “world”})
_id

    if	
  not	
  specified	
  drivers	
  will	
  add	
  default:

ObjectId("4bface1a2231316e04f3c434")
                timestamp
                        machine	
  id
                               process	
  id
                                           counter


                  http://www.mongodb.org/display/DOCS/Object+IDs
BSON Encoding
     {_id:	
  ObjectId(XXXXXXXXXXXX),
     	
  hello:	
  “world”}


x27x00x00x00x07	
  	
  	
  _	
  	
  	
  i	
  	
  	
  dx00	
  	
  	
  X	
                                                     	
  
	
  	
  	
  X	
  	
  	
  X	
  	
  	
  X	
  	
  	
  X	
  	
  	
  X	
  	
  	
  X	
  	
  	
  X	
  	
  	
  X	
  	
  	
  X	
  	
  	
  X
	
  	
  	
  Xx02	
  	
  	
  h	
  	
  	
  e	
  	
  	
  l	
  	
  	
  l	
  	
  	
  ox00x06x00
x00x00	
  	
  	
  w	
  	
  	
  o	
  	
  	
  r	
  	
  	
  l	
  	
  	
  dx00x00

                                                                                                      http://bsonspec.org
Insert Message (TCP/IP)

message	
  length   request	
  id                 response	
  id      op	
  code	
  (insert)
x68x00x00x00 xXXxXXxXXxXX x00x00x00x00 xd2x07x00x00

     reserved            collection	
  name                           document(s)
 x00x00x00x00      f	
  o	
  o	
  .	
  t	
  e	
  s	
  t	
  x00     BSON	
  Data




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

                           $	
  ls	
  -­‐sk	
  /data/db/

                                                           }
                           	
  16384	
  foo.ns             allocated	
  per	
  

(up	
  to	
  2	
  gigs){
double	
  in	
  size	
     	
  65536	
  foo.0
                           131072	
  foo.1
                                                             database


                           	
  16384	
  bar.ns
                           	
  	
  	
  	
  	
  	
  	
  ...
Memory Management
Extent Allocation
foo.0
                      allocated	
  per	
  namespace:
foo.1                      foo.test
        00000000000        foo.bar
                           foo.baz
        00000000000
        00000000000        foo.$freelist
        00000000000   0000 preallocated	
  space
foo.2   00000000000
        00000000000   ns	
  details	
  stored	
  in	
  foo.ns
        00000000000
        00000000000
Record Allocation
                      ...
  Header	
  (Size,	
  Offset,	
  Next,	
  Prev)
                BSON	
  Data
                   Padding

 Deleted	
  Record	
  (Size,	
  Offset,	
  Next)

                      ...
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
db.test.find({hello:	
  “world”})
Query Language

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




       http://www.mongodb.org/display/DOCS/Advanced+Queries
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
                                           ...
Query Optimizer
          find({x:	
  10,	
  y:	
  “foo”})


	
  	
  scan
                                    terminate
	
  	
  index	
  on	
  x

	
  	
  index	
  on	
  y     remember
db.foo.drop()
Commands


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




         http://www.mongodb.org/display/DOCS/Commands
db.foo.drop();

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

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

                 =
db.$cmd.find({drop:	
  "foo"}).limit(-­‐1);
Capped Collections
            preallocated
      auto	
  LRI	
  age-­‐out
     no	
  default	
  _id	
  index
   always	
  in	
  insertion	
  order




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



                               http://www.mongodb.org/display/DOCS/Replication
Replication Topology
                                         master           slave

        master
                                         master           slave


slave       slave        slave           master          master

                                          slave          master



                    http://www.mongodb.org/display/DOCS/Replication
Auto-Sharding
                      Shards
          mongod      mongod       mongod
                                                   ...
Config     mongod     mongod        mongod
Servers

mongod

mongod

mongod
                      mongos      mongos     ...


                       client
            http://www.mongodb.org/display/DOCS/Sharding
Geohashing
                              (20,	
  10)
             (0001	
  0100,	
  0000	
  1010)
                0000	
  0010	
  0110	
  0100

maps	
  close	
  coordinates	
  (21,	
  9)	
  to	
  close	
  hashes:
             0000	
  0010	
  0110	
  0011

   tricky	
  part	
  happens	
  at	
  bit-­‐flips	
  (127	
  vs	
  128)

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




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

More Related Content

PDF
PostgreSQL Deep Internal
PDF
MySQL Administrator 2021 - 네오클로바
PPTX
PDF
React Js Simplified
PDF
Infrastructure & System Monitoring using Prometheus
PPTX
re:Invent 2022 DAT326 Deep dive into Amazon Aurora and its innovations
PPTX
ProxySQL for MySQL
PDF
The MySQL Query Optimizer Explained Through Optimizer Trace
PostgreSQL Deep Internal
MySQL Administrator 2021 - 네오클로바
React Js Simplified
Infrastructure & System Monitoring using Prometheus
re:Invent 2022 DAT326 Deep dive into Amazon Aurora and its innovations
ProxySQL for MySQL
The MySQL Query Optimizer Explained Through Optimizer Trace

What's hot (20)

PDF
MongodB Internals
PDF
MongoDB Performance Tuning
PDF
Introduction to MongoDB
PPTX
PDF
MongoDB Fundamentals
PPTX
MongoDB
PPTX
Indexing with MongoDB
PPTX
Why your Spark Job is Failing
PDF
Understanding Presto - Presto meetup @ Tokyo #1
PDF
MongoDB WiredTiger Internals: Journey To Transactions
PDF
PostgreSQL Tutorial For Beginners | Edureka
PDF
Amazon S3 Best Practice and Tuning for Hadoop/Spark in the Cloud
PDF
ProxySQL High Avalability and Configuration Management Overview
PPTX
RocksDB compaction
PDF
MySQL Advanced Administrator 2021 - 네오클로바
PDF
Best practices for MySQL High Availability Tutorial
PDF
Mvcc in postgreSQL 권건우
PPTX
The Basics of MongoDB
PDF
Apache Spark Data Source V2 with Wenchen Fan and Gengliang Wang
MongodB Internals
MongoDB Performance Tuning
Introduction to MongoDB
MongoDB Fundamentals
MongoDB
Indexing with MongoDB
Why your Spark Job is Failing
Understanding Presto - Presto meetup @ Tokyo #1
MongoDB WiredTiger Internals: Journey To Transactions
PostgreSQL Tutorial For Beginners | Edureka
Amazon S3 Best Practice and Tuning for Hadoop/Spark in the Cloud
ProxySQL High Avalability and Configuration Management Overview
RocksDB compaction
MySQL Advanced Administrator 2021 - 네오클로바
Best practices for MySQL High Availability Tutorial
Mvcc in postgreSQL 권건우
The Basics of MongoDB
Apache Spark Data Source V2 with Wenchen Fan and Gengliang Wang
Ad

Viewers also liked (12)

KEY
MongoDB: How it Works
PDF
MongoDB World 2016: Deciphering .explain() Output
PDF
Deciphering Explain Output
PDF
MongoDB WiredTiger Internals
PPTX
Trading up: Adding Flexibility and Scalability to Bouygues Telecom with MongoDB
PPT
MongoDB Basic Concepts
PPTX
Agility and Scalability with MongoDB
PDF
Indexing
PPTX
Availability and scalability in mongo
PPTX
Performance Tuning and Optimization
PPTX
Scaling and Transaction Futures
PPTX
Developing with the Modern App Stack: MEAN and MERN (with Angular2 and ReactJS)
MongoDB: How it Works
MongoDB World 2016: Deciphering .explain() Output
Deciphering Explain Output
MongoDB WiredTiger Internals
Trading up: Adding Flexibility and Scalability to Bouygues Telecom with MongoDB
MongoDB Basic Concepts
Agility and Scalability with MongoDB
Indexing
Availability and scalability in mongo
Performance Tuning and Optimization
Scaling and Transaction Futures
Developing with the Modern App Stack: MEAN and MERN (with Angular2 and ReactJS)
Ad

Similar to Inside MongoDB: the Internals of an Open-Source Database (20)

PDF
FrozenRails Training
PDF
Latinoware
PDF
はじめてのMongoDB
PDF
Javantura v2 - Replication with MongoDB - what could go wrong... - Philipp Krenn
PDF
dojo.Patterns
PPTX
Rapid and Scalable Development with MongoDB, PyMongo, and Ming
PPTX
Introduction to MongoDB
PDF
Mongodb workshop
PDF
MongoDB: Optimising for Performance, Scale & Analytics
PDF
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
ODP
Mongo db dla administratora
PDF
From mysql to MongoDB(MongoDB2011北京交流会)
KEY
Introduction to MongoDB
PDF
mongodb-introduction
PDF
Trimming The Cruft
PDF
Ensuring High Availability for Real-time Analytics featuring Boxed Ice / Serv...
PDF
Spray Json and MongoDB Queries: Insights and Simple Tricks.
KEY
An introduction to CouchDB
PPT
Sedna XML Database: Executor Internals
PDF
Mongophilly shell-2011-04-26
FrozenRails Training
Latinoware
はじめてのMongoDB
Javantura v2 - Replication with MongoDB - what could go wrong... - Philipp Krenn
dojo.Patterns
Rapid and Scalable Development with MongoDB, PyMongo, and Ming
Introduction to MongoDB
Mongodb workshop
MongoDB: Optimising for Performance, Scale & Analytics
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
Mongo db dla administratora
From mysql to MongoDB(MongoDB2011北京交流会)
Introduction to MongoDB
mongodb-introduction
Trimming The Cruft
Ensuring High Availability for Real-time Analytics featuring Boxed Ice / Serv...
Spray Json and MongoDB Queries: Insights and Simple Tricks.
An introduction to CouchDB
Sedna XML Database: Executor Internals
Mongophilly shell-2011-04-26

More from Mike Dirolf (16)

KEY
Inside PyMongo - MongoNYC
PDF
MongoDB at FrozenRails
KEY
Python Development (MongoSF)
KEY
MongoDB at ZPUGDC
KEY
MongoDB hearts Django? (Django NYC)
KEY
MongoDB at CodeMash 2.0.1.0
PDF
MongoDB at RubyConf
KEY
MongoDB at RuPy
KEY
MongoDB at RubyEnRails 2009
KEY
MongoDB Strange Loop 2009
KEY
MongoDB Hadoop DC
KEY
MongoDB London PHP
KEY
MongoDB EuroPython 2009
KEY
MongoDB NYC Python
KEY
MongoDB SF Python
KEY
MongoDB SF Ruby
Inside PyMongo - MongoNYC
MongoDB at FrozenRails
Python Development (MongoSF)
MongoDB at ZPUGDC
MongoDB hearts Django? (Django NYC)
MongoDB at CodeMash 2.0.1.0
MongoDB at RubyConf
MongoDB at RuPy
MongoDB at RubyEnRails 2009
MongoDB Strange Loop 2009
MongoDB Hadoop DC
MongoDB London PHP
MongoDB EuroPython 2009
MongoDB NYC Python
MongoDB SF Python
MongoDB SF Ruby

Recently uploaded (20)

PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Electronic commerce courselecture one. Pdf
PDF
KodekX | Application Modernization Development
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Encapsulation theory and applications.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Cloud computing and distributed systems.
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
20250228 LYD VKU AI Blended-Learning.pptx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Electronic commerce courselecture one. Pdf
KodekX | Application Modernization Development
The AUB Centre for AI in Media Proposal.docx
MYSQL Presentation for SQL database connectivity
Big Data Technologies - Introduction.pptx
Digital-Transformation-Roadmap-for-Companies.pptx
Encapsulation theory and applications.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Cloud computing and distributed systems.
Spectral efficient network and resource selection model in 5G networks
Diabetes mellitus diagnosis method based random forest with bat algorithm

Inside MongoDB: the Internals of an Open-Source Database

  • 1. Mike  Dirolf  -­‐  @mdirolf  -­‐  10gen,  Inc. http://www.mongodb.org
  • 3. a word of warning this  talk  might  be  a  bit  “hard”, but  MongoDB  is  really  easy: http://try.mongodb.org
  • 5. _id if  not  specified  drivers  will  add  default: ObjectId("4bface1a2231316e04f3c434") timestamp machine  id process  id counter http://www.mongodb.org/display/DOCS/Object+IDs
  • 6. BSON Encoding {_id:  ObjectId(XXXXXXXXXXXX),  hello:  “world”} x27x00x00x00x07      _      i      dx00      X          X      X      X      X      X      X      X      X      X      X      Xx02      h      e      l      l      ox00x06x00 x00x00      w      o      r      l      dx00x00 http://bsonspec.org
  • 7. Insert Message (TCP/IP) message  length request  id response  id op  code  (insert) x68x00x00x00 xXXxXXxXXxXX x00x00x00x00 xd2x07x00x00 reserved collection  name document(s) x00x00x00x00 f  o  o  .  t  e  s  t  x00 BSON  Data http://www.mongodb.org/display/DOCS/Mongo+Wire+Protocol
  • 8. Data File Allocation $  ls  -­‐sk  /data/db/ }  16384  foo.ns allocated  per   (up  to  2  gigs){ double  in  size    65536  foo.0 131072  foo.1 database  16384  bar.ns              ...
  • 10. Extent Allocation foo.0 allocated  per  namespace: foo.1 foo.test 00000000000 foo.bar foo.baz 00000000000 00000000000 foo.$freelist 00000000000 0000 preallocated  space foo.2 00000000000 00000000000 ns  details  stored  in  foo.ns 00000000000 00000000000
  • 11. Record Allocation ... Header  (Size,  Offset,  Next,  Prev) BSON  Data Padding Deleted  Record  (Size,  Offset,  Next) ...
  • 12. 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
  • 14. Query Language “query  by  example”  plus  $  modifiers: {first_name:  “Mike”,  age:  {$gte:  20,  $lt:  40}} http://www.mongodb.org/display/DOCS/Advanced+Queries
  • 15. 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 ...
  • 16. Query Optimizer find({x:  10,  y:  “foo”})    scan terminate    index  on  x    index  on  y remember
  • 18. Commands drop,  count,  copydb,   findAndModify,  serverStatus,  ... http://www.mongodb.org/display/DOCS/Commands
  • 19. db.foo.drop(); = db.foo.runCommand({drop:  "foo"}); = db.$cmd.findOne({drop:  "foo"}); = db.$cmd.find({drop:  "foo"}).limit(-­‐1);
  • 20. Capped Collections preallocated auto  LRI  age-­‐out no  default  _id  index always  in  insertion  order http://www.mongodb.org/display/DOCS/Capped+Collections
  • 21. Replication Oplog >  use  local switched  to  db  local >  use  foo >  db.oplog.$main.find() switched  to  db  foo {ts:  ...,  op:  "n",  ns:  "",  o:  {}} {ts:  ...,  op:  "n",  ns:  "",  o:  {}} >  db.test.insert({x:  1,  url:   {ts:  ...,  op:  "i",  ns:  "foo.test",   "http://dirolf.com"});  o:  {_id:  ObjectId("..."),          x:  1,          url:  "http://dirolf.com"}} {ts:  ...,  op:  "n",  ns:  "",  o:  {}} >  db.test.update({url:  "http:// {ts:  ...,  op:  "u",  ns:  "foo.test", dirolf.com"},  {$inc:  {x:  1}});  o2:  {_id:  ObjectId("...")},  o:  {$set:  {x:  2}}} http://www.mongodb.org/display/DOCS/Replication
  • 22. Replication Topology master slave master master slave slave slave slave master master slave master http://www.mongodb.org/display/DOCS/Replication
  • 23. Auto-Sharding Shards mongod mongod mongod ... Config mongod mongod mongod Servers mongod mongod mongod mongos mongos ... client http://www.mongodb.org/display/DOCS/Sharding
  • 24. Geohashing (20,  10) (0001  0100,  0000  1010) 0000  0010  0110  0100 maps  close  coordinates  (21,  9)  to  close  hashes: 0000  0010  0110  0011 tricky  part  happens  at  bit-­‐flips  (127  vs  128) http://www.mongodb.org/display/DOCS/Geospatial+Indexing
  • 25. Download MongoDB http://www.mongodb.org these  slides  are  available  at  http://dirolf.com