SlideShare a Scribd company logo
1 of 113
Download to read offline
Sharding with
 MongoDB


          Tyler Brock
          @TylerBrock
Philosophy
 Concepts
Architecture
 Mechanics
Philosophy
Philosophy
Philosophy



      “Ruby is designed
          to make
        programmers
           happy.”
Philosophy




             MongoDB is a
             database for
             developers.
Philosophy




             Build
Philosophy




             Build
             Scale
Philosophy




          How does
        MongoDB scale?
Philosophy




> db.runCommand({enablesharding: "<dbname>" })


> db.runCommand({
   shardcollection: "<namespace>",
   key: <shardkeypatternobject>
})
Concepts
Simple Web Application

           app



                 Read/Write



         datastore
What happens when
your working set
exceeds memory?
What happens if
your write load
is enormous?
Vertical Scaling

        app




      datastore
Vertical Scaling

        app




      datastore
Vertical Scaling

app           app            app




            datastore   68 GB Ram
                        Raid10 EBS
Vertical Scaling

app           app            app




            datastore


                        512 GB Ram
                        Raid10 SSD
Horizontal Scaling
Horizontal Scaling


         app




       datastore


       60gb
Horizontal Scaling


              app




datastore   datastore   datastore


20gb        20gb        20gb
Horizontal Scaling

                            app

                       Routing Logic
metadata




           datastore      datastore    datastore


           20gb           20gb         20gb
Horizontal Scaling

                            app

                       Routing Logic
metadata




           datastore      datastore    datastore


            60gb          20gb         20gb
Horizontal Scaling
                            app

                       Routing Logic
                         Balancer
metadata




           datastore      datastore    datastore


            60gb          20gb         20gb
Horizontal Scaling
                            app

                       Routing Logic
                         Balancer
metadata




           datastore      datastore    datastore


            30gb           30gb        30gb
Architecture
Shard

         Really is just a mongod (or replica set)
mongod
         Where your data lives
Config Server

         Mongod started with --configsvr option
config    Must have 3 (or 1 in development)
         Data is commited using 2 phase commit
mongos
         Acts just like shard router / proxy
         One or as many as you want
mongos
         Light weight -- can run on App servers
         Caches meta-data from config servers
Routing Logic
                         Balancing
metadata




           datastore      datastore    datastore
mongos
metadata




           datastore    datastore   datastore
app




                       mongos
metadata




           datastore    datastore   datastore
app




                    mongos
config




        datastore    datastore   datastore
app




                    mongos
config

config

config




        datastore    datastore   datastore
app




                 mongos
config

config

config




        mongod    mongod   mongod
app




                 mongos
config

config

config




        RS         RS       RS
        mongod    mongod   mongod

        mongod    mongod   mongod

        mongod    mongod   mongod
app




                 mongos
config

config

config




        RS         RS       RS
        mongod    mongod   mongod

        mongod    mongod   mongod

        mongod    mongod   mongod
Configuration
Bring up mongods or Replica Sets


            mongod --shardsvr
            mongod --replSet --shardsvr




             RS          RS         RS
             mongod     mongod     mongod

             mongod     mongod     mongod

             mongod     mongod     mongod
Bring up Config Servers



                  mongod --configsvr
    config

    config

    config




             RS           RS          RS
            mongod       mongod   mongod

            mongod       mongod   mongod

            mongod       mongod       mongod
Bring up Mongos

            mongos --configdb <list of configdb uris>

                          mongos
    config

    config

    config




                 RS         RS         RS
                mongod     mongod     mongod

                mongod     mongod     mongod

                 mongod     mongod    mongod
Connect to Mongos
+ Add Shards
     > use admin
     > db.runCommand({"addShard": <shard uri>})


+Enable Sharding
    > db.runCommand( { enablesharding : "<dbname>" } );


+Shard a Collection

    > db.runCommand( { shardcollection : "<namespace>", key : <key> });
Mechanics
How does MongoDB
balance my data?
Keys
            test.users
{
     name: “Joe”,
     email: “Joe@fake.com”,
},
{
     name: “Bob”,
     email: “bob@fake.com”,
},
{
     name: “Tyler”,
     email: “tyler@fake.com”,
}
Keys
                                                 test.users


                                       {
                                            name: “Joe”,
                                            email: “Joe@fake.com”,
> db.runCommand({                      },
   shardcollection: “test.users”,      {
               key: { email: 1 }            name: “Bob”,
})                                          email: “bob@fake.com”,
                                       },
                                       {
                                            name: “Tyler”,
                                            email: “tyler@fake.com”,
                                       }
Keys
                                              test.users


                                    {
                                         name: “Joe”,
                                         email: “Joe@fake.com”,
                                    },
shardcollection: “test.users”,      {
            key: { email: 1 }            name: “Bob”,
                                         email: “bob@fake.com”,
                                    },
                                    {
                                         name: “Tyler”,
                                         email: “tyler@fake.com”,
                                    }
Keys
                                     test.users


                           {
                                name: “Joe”,
                                email: “Joe@fake.com”,
                           },
                           {
key: { email: 1 }               name: “Bob”,
                                email: “bob@fake.com”,
                           },
                           {
                                name: “Tyler”,
                                email: “tyler@fake.com”,
                           }
Chunks


-∞            +∞
Chunks


-∞                                            +∞




     joe@fake.com            tyler@fake.com

              moe@fake.com
Chunks
                    Split!



-∞                                            +∞




     joe@fake.com            tyler@fake.com

              moe@fake.com
Chunks
     This is a              Split!        This is a
      chunk                                chunk


-∞                                                    +∞




             joe@fake.com            tyler@fake.com

                      moe@fake.com
Chunks


-∞                                            +∞




     joe@fake.com            tyler@fake.com

              moe@fake.com
Chunks


-∞                                            +∞




     joe@fake.com            tyler@fake.com

              moe@fake.com
Chunks
     Split!



-∞                                              +∞




     joe@fake.com              tyler@fake.com

                moe@fake.com
Splitting
                                       config

                mongos                 config

                                       config




Shard 1   Shard 2        Shard 3   Shard 4
Splitting
                                       config

                mongos                 config

                                       config




Shard 1   Shard 2        Shard 3   Shard 4
Splitting
                                       config

                mongos                 config

                                       config




Shard 1   Shard 2        Shard 3   Shard 4
Splitting
                                       config

                mongos                 config

                                       config




Shard 1   Shard 2        Shard 3   Shard 4
Splitting
           Split
  this big chunk into 2                           config
         chunks

                           mongos                 config

                                                  config




Shard 1             Shard 2         Shard 3   Shard 4
Splitting
                                       config

                mongos                 config

                                       config




Shard 1   Shard 2        Shard 3   Shard 4
Splitting
                                                config

                         mongos                 config

         These                                  config
    chunks have split




Shard 1          Shard 2          Shard 3   Shard 4
Balancing
                                       config

                mongos                 config

                                       config




Shard 1   Shard 2        Shard 3   Shard 4
Balancing
      Shard1,
   move a chunk to                            config
      Shard2

                       mongos                 config

                                              config




Shard 1          Shard 2        Shard 3   Shard 4
Balancing
                                       config

                mongos                 config

                                       config




Shard 1   Shard 2        Shard 3   Shard 4
Balancing
      Shard1,
 move another chunk                            config
     to Shard3

                        mongos                 config

                                               config




Shard 1         Shard 2          Shard 3   Shard 4
Balancing
                                       config

                mongos                 config

                                       config




Shard 1   Shard 2        Shard 3   Shard 4
Balancing
      Shard1,
 move another chunk                            config
     to Shard4

                        mongos                 config

                                               config




Shard 1         Shard 2          Shard 3   Shard 4
Balancing
                                       config

                mongos                 config

                                       config




Shard 1   Shard 2        Shard 3   Shard 4
Balancing
                                       config

                mongos                 config

                                       config




Shard 1   Shard 2        Shard 3   Shard 4
How does MongoDB
route my queries?
Routed Request


         mongos




shard     shard   shard
Routed Request
          1
                          1. Query arrives at Mongos



         mongos




shard     shard   shard
Routed Request
             1
                             1. Query arrives at Mongos

                             2. Mongos routes query to a
            mongos           single shard


        2




shard       shard    shard
Routed Request
                 1
                                 1. Query arrives at Mongos

                                 2. Mongos routes query to a
                mongos           single shard

                                 3. Shard returns results of
            2                    query



        3



shard           shard    shard
Routed Request
                 1
                                 1. Query arrives at Mongos
                     4
                                 2. Mongos routes query to a
                mongos           single shard

                                 3. Shard returns results of
            2                    query

                                 4. Results returned to client
        3



shard           shard    shard
Scatter Gather Request


        mongos




shard   shard    shard
Scatter Gather Request
         1
                         1. Query arrives at Mongos



        mongos




shard   shard    shard
Scatter Gather Request
             1
                                 1. Query arrives at Mongos

                                 2. Mongos broadcasts query
            mongos               to all shards



        2    2       2




shard       shard        shard
Scatter Gather Request
             1
                                 1. Query arrives at Mongos

                                 2. Mongos broadcasts query
            mongos               to all shards

                                 3. Each shard returns results
                                 for query
        2    2       2


                 3         3
  3


shard       shard        shard
Scatter Gather Request
             1
                                     1. Query arrives at Mongos
                     4
                                     2. Mongos broadcasts query
            mongos                   to all shards

                                     3. Each shard returns results
                                     for query
        2    2           2
                                     4. Results combined and
                 3             3     returned to client
  3


shard       shard            shard
Distributed Merge Sort Req.


         mongos




 shard   shard    shard
Distributed Merge Sort Req.
          1
                          1. Query arrives at Mongos


         mongos




 shard   shard    shard
Distributed Merge Sort Req.
              1
                                  1. Query arrives at Mongos

                                  2. Mongos broadcasts query
             mongos               to all shards



         2    2       2




 shard       shard        shard
Distributed Merge Sort Req.
              1
                                      1. Query arrives at Mongos

                                      2. Mongos broadcasts query
             mongos                   to all shards

                                      3. Each shard locally sorts
                                      results
         2    2       2




 shard       shard        shard
         3            3           3
Distributed Merge Sort Req.
              1
                                      1. Query arrives at Mongos

                                      2. Mongos broadcasts query
             mongos                   to all shards

                                      3. Each shard locally sorts
                                      results
         2    2       2
                                      4. Results returned to
                                      mongos
    4             4           4


 shard       shard        shard
         3            3           3
Distributed Merge Sort Req.
              1
                                          1. Query arrives at Mongos

                                          2. Mongos broadcasts query
             mongos       5               to all shards

                                          3. Each shard locally sorts
                                          results
         2    2       2
                                          4. Results returned to
                                          mongos
    4             4               4
                                          5. Mongos merges sorted
                                          results
 shard       shard            shard
         3            3               3
Distributed Merge Sort Req.
              1
                                          1. Query arrives at Mongos
                  6
                                          2. Mongos broadcasts query
             mongos       5               to all shards

                                          3. Each shard locally sorts
                                          results
         2    2       2
                                          4. Results returned to
                                          mongos
    4             4               4
                                          5. Mongos merges sorted
                                          results
 shard       shard            shard
         3            3               3   6. Combined results
                                          returned to client
Queries

By Shard Key    Routed            db.users.find({email: “bob@10gen.com”})


Sorted by       Routed in order   db.users.find().sort({email:-1})
shard key
Find by non     Scatter Gather    db.users.find({state:”NY”})
shard key

Sorted by     Distributed merge   db.users.find().sort({state:1})
              sort
non shard key
Writes

Inserts   Requires shard key   db.users.insert({
                                name: “Bob”,
                                email: “Bob@fake.com”})
Removes   Routed               db.users.delete({
                                email: “bob@fake.com”})
          Scattered            db.users.delete({name: “Bob”})

Updates   Routed               db.users.update(
                                {email: “bob@10gen.com”},
                                {$set: { state: “NY”}})
          Scattered            db.users.update(
                                {state: “CA”},
                                {$set:{ state: “NY”}} )
How do I choose my
shard key?
Rule of Thumb




      Choose a field that
      is common to your
      queries.
Cardinality




        Chunks should be
        able to split.
Chunks should be able to split
  {
      node:           "ny153.example.com",
      application:    "apache",
      time:           "2011-01-02T21:21:56Z",
      level:          "ERROR",
      msg:            "something is broken"
  }


           Bad {node: 1}
Chunks should be able to split
  {
      node:           "ny153.example.com",
      application:    "apache",
      time:           "2011-01-02T21:21:56Z",
      level:          "ERROR",
      msg:            "something is broken"
  }


           Bad {node: 1}
Better {node:1, time:1}
Write Scaling




       Writes should be
       distributed.
Writes should be distributed
  {
      node:           "ny153.example.com",
      application:    "apache",
      time:           "2011-01-02T21:21:56Z",
      level:          "ERROR",
      msg:            "something is broken"
  }


               Bad { time : 1 }
Writes should be distributed
  {
      node:           "ny153.example.com",
      application:    "apache",
      time:           "2011-01-02T21:21:56Z",
      level:          "ERROR",
      msg:            "something is broken"
  }


               Bad { time : 1 }

Better {node:1, application:1, time:1}
Query Isolation
& Data Locality




          Queries should
          be routed to one
          shard.
Queries should be routed to one shard
  {
      node:          "ny153.example.com",
      application:   "apache",
      time:          "2011-01-02T21:21:56Z",
      level:         "ERROR",
      msg:           "something is broken”
  }


   Bad {msg: 1, node: 1}
Queries should be routed to one shard
  {
      node:          "ny153.example.com",
      application:   "apache",
      time:          "2011-01-02T21:21:56Z",
      level:         "ERROR",
      msg:           "something is broken”
  }


   Bad {msg: 1, node: 1}
Better {node: 1, time: 1}
> db.runCommand({enablesharding: "<dbname>" })


> db.runCommand({
   shardcollection: "<namespace>",
   key: <shardkeypatternobject>
})
Thanks!
Extra Slides
Config Servers
Config Servers

   mongod
Config Servers

   mongod

    mongod

    mongod
mongoDB Scaling - Single Node
read




       node_a1




                          write
Read scaling - add Replicas
read




       node_b1


       node_a1




                              write
Read scaling - add Replicas
read




       node_c1


       node_b1


       node_a1




                              write
Write scaling - Sharding
read


       shard1

       node_c1


       node_b1


       node_a1




                           write
Write scaling - add shards
read


       shard1    shard2

       node_c1   node_c2


       node_b1   node_b2


       node_a1   node_a2




                             write
Write scaling - add shards
read


       shard1    shard2    shard3

       node_c1   node_c2   node_c3


       node_b1   node_b2   node_b3


       node_a1   node_a2   node_a3




                                     write

More Related Content

What's hot

Building a Cross Channel Content Delivery Platform with MongoDB
Building a Cross Channel Content Delivery Platform with MongoDBBuilding a Cross Channel Content Delivery Platform with MongoDB
Building a Cross Channel Content Delivery Platform with MongoDBMongoDB
 
Webinar: Building Your First App with MongoDB and Java
Webinar: Building Your First App with MongoDB and JavaWebinar: Building Your First App with MongoDB and Java
Webinar: Building Your First App with MongoDB and JavaMongoDB
 
Introduction to MongoDB and Hadoop
Introduction to MongoDB and HadoopIntroduction to MongoDB and Hadoop
Introduction to MongoDB and HadoopSteven Francia
 
MongoDB Europe 2016 - Advanced MongoDB Aggregation Pipelines
MongoDB Europe 2016 - Advanced MongoDB Aggregation PipelinesMongoDB Europe 2016 - Advanced MongoDB Aggregation Pipelines
MongoDB Europe 2016 - Advanced MongoDB Aggregation PipelinesMongoDB
 
Build your first MongoDB App in Ruby @ StrangeLoop 2013
Build your first MongoDB App in Ruby @ StrangeLoop 2013Build your first MongoDB App in Ruby @ StrangeLoop 2013
Build your first MongoDB App in Ruby @ StrangeLoop 2013Steven Francia
 
Text Mining of Twitter in Data Mining
Text Mining of Twitter in Data MiningText Mining of Twitter in Data Mining
Text Mining of Twitter in Data MiningMeghaj Mallick
 
Back to Basics Webinar 3 - Thinking in Documents
Back to Basics Webinar 3 - Thinking in DocumentsBack to Basics Webinar 3 - Thinking in Documents
Back to Basics Webinar 3 - Thinking in DocumentsJoe Drumgoole
 
Back to Basics Webinar 1 - Introduction to NoSQL
Back to Basics Webinar 1 - Introduction to NoSQLBack to Basics Webinar 1 - Introduction to NoSQL
Back to Basics Webinar 1 - Introduction to NoSQLJoe Drumgoole
 
Don’t Get Lost in Translation for Serializing Data Structures
Don’t Get Lost in Translation for Serializing Data StructuresDon’t Get Lost in Translation for Serializing Data Structures
Don’t Get Lost in Translation for Serializing Data StructuresChristopher Brown
 
MongoDB London 2013: Data Modeling Examples from the Real World presented by ...
MongoDB London 2013: Data Modeling Examples from the Real World presented by ...MongoDB London 2013: Data Modeling Examples from the Real World presented by ...
MongoDB London 2013: Data Modeling Examples from the Real World presented by ...MongoDB
 
Back to Basics Webinar 4: Advanced Indexing, Text and Geospatial Indexes
Back to Basics Webinar 4: Advanced Indexing, Text and Geospatial IndexesBack to Basics Webinar 4: Advanced Indexing, Text and Geospatial Indexes
Back to Basics Webinar 4: Advanced Indexing, Text and Geospatial IndexesMongoDB
 
Back to Basics Webinar 2: Your First MongoDB Application
Back to Basics Webinar 2: Your First MongoDB ApplicationBack to Basics Webinar 2: Your First MongoDB Application
Back to Basics Webinar 2: Your First MongoDB ApplicationMongoDB
 
Back to Basics Webinar 3: Schema Design Thinking in Documents
 Back to Basics Webinar 3: Schema Design Thinking in Documents Back to Basics Webinar 3: Schema Design Thinking in Documents
Back to Basics Webinar 3: Schema Design Thinking in DocumentsMongoDB
 
Indexing with MongoDB
Indexing with MongoDBIndexing with MongoDB
Indexing with MongoDBMongoDB
 

What's hot (20)

Building a Cross Channel Content Delivery Platform with MongoDB
Building a Cross Channel Content Delivery Platform with MongoDBBuilding a Cross Channel Content Delivery Platform with MongoDB
Building a Cross Channel Content Delivery Platform with MongoDB
 
Webinar: Building Your First App with MongoDB and Java
Webinar: Building Your First App with MongoDB and JavaWebinar: Building Your First App with MongoDB and Java
Webinar: Building Your First App with MongoDB and Java
 
Introduction to MongoDB and Hadoop
Introduction to MongoDB and HadoopIntroduction to MongoDB and Hadoop
Introduction to MongoDB and Hadoop
 
MongoDB Europe 2016 - Advanced MongoDB Aggregation Pipelines
MongoDB Europe 2016 - Advanced MongoDB Aggregation PipelinesMongoDB Europe 2016 - Advanced MongoDB Aggregation Pipelines
MongoDB Europe 2016 - Advanced MongoDB Aggregation Pipelines
 
Build your first MongoDB App in Ruby @ StrangeLoop 2013
Build your first MongoDB App in Ruby @ StrangeLoop 2013Build your first MongoDB App in Ruby @ StrangeLoop 2013
Build your first MongoDB App in Ruby @ StrangeLoop 2013
 
The emerging world of mongo db csp
The emerging world of mongo db   cspThe emerging world of mongo db   csp
The emerging world of mongo db csp
 
Elastic search 검색
Elastic search 검색Elastic search 검색
Elastic search 검색
 
Python and MongoDB
Python and MongoDBPython and MongoDB
Python and MongoDB
 
Text Mining of Twitter in Data Mining
Text Mining of Twitter in Data MiningText Mining of Twitter in Data Mining
Text Mining of Twitter in Data Mining
 
MongoDB (Advanced)
MongoDB (Advanced)MongoDB (Advanced)
MongoDB (Advanced)
 
Back to Basics Webinar 3 - Thinking in Documents
Back to Basics Webinar 3 - Thinking in DocumentsBack to Basics Webinar 3 - Thinking in Documents
Back to Basics Webinar 3 - Thinking in Documents
 
Back to Basics Webinar 1 - Introduction to NoSQL
Back to Basics Webinar 1 - Introduction to NoSQLBack to Basics Webinar 1 - Introduction to NoSQL
Back to Basics Webinar 1 - Introduction to NoSQL
 
Json at work overview and ecosystem-v2.0
Json at work   overview and ecosystem-v2.0Json at work   overview and ecosystem-v2.0
Json at work overview and ecosystem-v2.0
 
Don’t Get Lost in Translation for Serializing Data Structures
Don’t Get Lost in Translation for Serializing Data StructuresDon’t Get Lost in Translation for Serializing Data Structures
Don’t Get Lost in Translation for Serializing Data Structures
 
MongoDB London 2013: Data Modeling Examples from the Real World presented by ...
MongoDB London 2013: Data Modeling Examples from the Real World presented by ...MongoDB London 2013: Data Modeling Examples from the Real World presented by ...
MongoDB London 2013: Data Modeling Examples from the Real World presented by ...
 
Back to Basics Webinar 4: Advanced Indexing, Text and Geospatial Indexes
Back to Basics Webinar 4: Advanced Indexing, Text and Geospatial IndexesBack to Basics Webinar 4: Advanced Indexing, Text and Geospatial Indexes
Back to Basics Webinar 4: Advanced Indexing, Text and Geospatial Indexes
 
Back to Basics Webinar 2: Your First MongoDB Application
Back to Basics Webinar 2: Your First MongoDB ApplicationBack to Basics Webinar 2: Your First MongoDB Application
Back to Basics Webinar 2: Your First MongoDB Application
 
Back to Basics Webinar 3: Schema Design Thinking in Documents
 Back to Basics Webinar 3: Schema Design Thinking in Documents Back to Basics Webinar 3: Schema Design Thinking in Documents
Back to Basics Webinar 3: Schema Design Thinking in Documents
 
Android GRPC
Android GRPCAndroid GRPC
Android GRPC
 
Indexing with MongoDB
Indexing with MongoDBIndexing with MongoDB
Indexing with MongoDB
 

Viewers also liked

Test Cards
Test CardsTest Cards
Test Cardshndoja
 
MongoDB Aggregation Framework
MongoDB Aggregation FrameworkMongoDB Aggregation Framework
MongoDB Aggregation FrameworkCaserta
 
MongoDB: Queries and Aggregation Framework with NBA Game Data
MongoDB: Queries and Aggregation Framework with NBA Game DataMongoDB: Queries and Aggregation Framework with NBA Game Data
MongoDB: Queries and Aggregation Framework with NBA Game DataValeri Karpov
 
MongoDB Aggregation Framework
MongoDB Aggregation FrameworkMongoDB Aggregation Framework
MongoDB Aggregation FrameworkTyler Brock
 
Mongodb Aggregation Pipeline
Mongodb Aggregation PipelineMongodb Aggregation Pipeline
Mongodb Aggregation Pipelinezahid-mian
 
MongoDB's New Aggregation framework
MongoDB's New Aggregation frameworkMongoDB's New Aggregation framework
MongoDB's New Aggregation frameworkChris Westin
 

Viewers also liked (6)

Test Cards
Test CardsTest Cards
Test Cards
 
MongoDB Aggregation Framework
MongoDB Aggregation FrameworkMongoDB Aggregation Framework
MongoDB Aggregation Framework
 
MongoDB: Queries and Aggregation Framework with NBA Game Data
MongoDB: Queries and Aggregation Framework with NBA Game DataMongoDB: Queries and Aggregation Framework with NBA Game Data
MongoDB: Queries and Aggregation Framework with NBA Game Data
 
MongoDB Aggregation Framework
MongoDB Aggregation FrameworkMongoDB Aggregation Framework
MongoDB Aggregation Framework
 
Mongodb Aggregation Pipeline
Mongodb Aggregation PipelineMongodb Aggregation Pipeline
Mongodb Aggregation Pipeline
 
MongoDB's New Aggregation framework
MongoDB's New Aggregation frameworkMongoDB's New Aggregation framework
MongoDB's New Aggregation framework
 

Similar to Sharding with MongoDB -- MongoDC 2012

MongoDB World 2018: Load Spikes, GDPR, & Natural Disasters... Oh My! Scaling ...
MongoDB World 2018: Load Spikes, GDPR, & Natural Disasters... Oh My! Scaling ...MongoDB World 2018: Load Spikes, GDPR, & Natural Disasters... Oh My! Scaling ...
MongoDB World 2018: Load Spikes, GDPR, & Natural Disasters... Oh My! Scaling ...MongoDB
 
Get expertise with mongo db
Get expertise with mongo dbGet expertise with mongo db
Get expertise with mongo dbAmit Thakkar
 
Battle of NoSQL stars: Amazon's SDB vs MongoDB vs CouchDB vs RavenDB
Battle of NoSQL stars: Amazon's SDB vs MongoDB vs CouchDB vs RavenDBBattle of NoSQL stars: Amazon's SDB vs MongoDB vs CouchDB vs RavenDB
Battle of NoSQL stars: Amazon's SDB vs MongoDB vs CouchDB vs RavenDBJesse Wolgamott
 
Leaving jsps in the dust
Leaving jsps in the dustLeaving jsps in the dust
Leaving jsps in the dustVeena Basavaraj
 
Building your first app with MongoDB
Building your first app with MongoDBBuilding your first app with MongoDB
Building your first app with MongoDBNorberto Leite
 
MongoDB + node.js で作るソーシャルゲーム
MongoDB + node.js で作るソーシャルゲームMongoDB + node.js で作るソーシャルゲーム
MongoDB + node.js で作るソーシャルゲームSuguru Namura
 
Building a Scalable Distributed Stats Infrastructure with Storm and KairosDB
Building a Scalable Distributed Stats Infrastructure with Storm and KairosDBBuilding a Scalable Distributed Stats Infrastructure with Storm and KairosDB
Building a Scalable Distributed Stats Infrastructure with Storm and KairosDBCody Ray
 
MongoDB and Ruby on Rails
MongoDB and Ruby on RailsMongoDB and Ruby on Rails
MongoDB and Ruby on Railsrfischer20
 
Ensuring High Availability for Real-time Analytics featuring Boxed Ice / Serv...
Ensuring High Availability for Real-time Analytics featuring Boxed Ice / Serv...Ensuring High Availability for Real-time Analytics featuring Boxed Ice / Serv...
Ensuring High Availability for Real-time Analytics featuring Boxed Ice / Serv...MongoDB
 
mongodb-introduction
mongodb-introductionmongodb-introduction
mongodb-introductionTse-Ching Ho
 
MongoDB Basic Concepts
MongoDB Basic ConceptsMongoDB Basic Concepts
MongoDB Basic ConceptsMongoDB
 
MongoDB, PHP and the cloud - php cloud summit 2011
MongoDB, PHP and the cloud - php cloud summit 2011MongoDB, PHP and the cloud - php cloud summit 2011
MongoDB, PHP and the cloud - php cloud summit 2011Steven Francia
 
MongoDB World 2019: Don't Panic - The Hitchhiker's Guide to the MongoDB Galaxy
MongoDB World 2019: Don't Panic - The Hitchhiker's Guide to the MongoDB GalaxyMongoDB World 2019: Don't Panic - The Hitchhiker's Guide to the MongoDB Galaxy
MongoDB World 2019: Don't Panic - The Hitchhiker's Guide to the MongoDB GalaxyMongoDB
 
MongoDB: Optimising for Performance, Scale & Analytics
MongoDB: Optimising for Performance, Scale & AnalyticsMongoDB: Optimising for Performance, Scale & Analytics
MongoDB: Optimising for Performance, Scale & AnalyticsServer Density
 
MongoDB: Intro & Application for Big Data
MongoDB: Intro & Application  for Big DataMongoDB: Intro & Application  for Big Data
MongoDB: Intro & Application for Big DataTakahiro Inoue
 
BreizhCamp 2013 - Pimp my backend
BreizhCamp 2013 - Pimp my backendBreizhCamp 2013 - Pimp my backend
BreizhCamp 2013 - Pimp my backendHoracio Gonzalez
 
A story of Netflix and AB Testing in the User Interface using DynamoDB - DAT3...
A story of Netflix and AB Testing in the User Interface using DynamoDB - DAT3...A story of Netflix and AB Testing in the User Interface using DynamoDB - DAT3...
A story of Netflix and AB Testing in the User Interface using DynamoDB - DAT3...Amazon Web Services
 
MongoDB at ZPUGDC
MongoDB at ZPUGDCMongoDB at ZPUGDC
MongoDB at ZPUGDCMike Dirolf
 

Similar to Sharding with MongoDB -- MongoDC 2012 (20)

MongoDB World 2018: Load Spikes, GDPR, & Natural Disasters... Oh My! Scaling ...
MongoDB World 2018: Load Spikes, GDPR, & Natural Disasters... Oh My! Scaling ...MongoDB World 2018: Load Spikes, GDPR, & Natural Disasters... Oh My! Scaling ...
MongoDB World 2018: Load Spikes, GDPR, & Natural Disasters... Oh My! Scaling ...
 
Get expertise with mongo db
Get expertise with mongo dbGet expertise with mongo db
Get expertise with mongo db
 
Battle of NoSQL stars: Amazon's SDB vs MongoDB vs CouchDB vs RavenDB
Battle of NoSQL stars: Amazon's SDB vs MongoDB vs CouchDB vs RavenDBBattle of NoSQL stars: Amazon's SDB vs MongoDB vs CouchDB vs RavenDB
Battle of NoSQL stars: Amazon's SDB vs MongoDB vs CouchDB vs RavenDB
 
Leaving jsps in the dust
Leaving jsps in the dustLeaving jsps in the dust
Leaving jsps in the dust
 
Building your first app with MongoDB
Building your first app with MongoDBBuilding your first app with MongoDB
Building your first app with MongoDB
 
MongoDB + node.js で作るソーシャルゲーム
MongoDB + node.js で作るソーシャルゲームMongoDB + node.js で作るソーシャルゲーム
MongoDB + node.js で作るソーシャルゲーム
 
Building a Scalable Distributed Stats Infrastructure with Storm and KairosDB
Building a Scalable Distributed Stats Infrastructure with Storm and KairosDBBuilding a Scalable Distributed Stats Infrastructure with Storm and KairosDB
Building a Scalable Distributed Stats Infrastructure with Storm and KairosDB
 
A Brief MongoDB Intro
A Brief MongoDB IntroA Brief MongoDB Intro
A Brief MongoDB Intro
 
MongoDB 101
MongoDB 101MongoDB 101
MongoDB 101
 
MongoDB and Ruby on Rails
MongoDB and Ruby on RailsMongoDB and Ruby on Rails
MongoDB and Ruby on Rails
 
Ensuring High Availability for Real-time Analytics featuring Boxed Ice / Serv...
Ensuring High Availability for Real-time Analytics featuring Boxed Ice / Serv...Ensuring High Availability for Real-time Analytics featuring Boxed Ice / Serv...
Ensuring High Availability for Real-time Analytics featuring Boxed Ice / Serv...
 
mongodb-introduction
mongodb-introductionmongodb-introduction
mongodb-introduction
 
MongoDB Basic Concepts
MongoDB Basic ConceptsMongoDB Basic Concepts
MongoDB Basic Concepts
 
MongoDB, PHP and the cloud - php cloud summit 2011
MongoDB, PHP and the cloud - php cloud summit 2011MongoDB, PHP and the cloud - php cloud summit 2011
MongoDB, PHP and the cloud - php cloud summit 2011
 
MongoDB World 2019: Don't Panic - The Hitchhiker's Guide to the MongoDB Galaxy
MongoDB World 2019: Don't Panic - The Hitchhiker's Guide to the MongoDB GalaxyMongoDB World 2019: Don't Panic - The Hitchhiker's Guide to the MongoDB Galaxy
MongoDB World 2019: Don't Panic - The Hitchhiker's Guide to the MongoDB Galaxy
 
MongoDB: Optimising for Performance, Scale & Analytics
MongoDB: Optimising for Performance, Scale & AnalyticsMongoDB: Optimising for Performance, Scale & Analytics
MongoDB: Optimising for Performance, Scale & Analytics
 
MongoDB: Intro & Application for Big Data
MongoDB: Intro & Application  for Big DataMongoDB: Intro & Application  for Big Data
MongoDB: Intro & Application for Big Data
 
BreizhCamp 2013 - Pimp my backend
BreizhCamp 2013 - Pimp my backendBreizhCamp 2013 - Pimp my backend
BreizhCamp 2013 - Pimp my backend
 
A story of Netflix and AB Testing in the User Interface using DynamoDB - DAT3...
A story of Netflix and AB Testing in the User Interface using DynamoDB - DAT3...A story of Netflix and AB Testing in the User Interface using DynamoDB - DAT3...
A story of Netflix and AB Testing in the User Interface using DynamoDB - DAT3...
 
MongoDB at ZPUGDC
MongoDB at ZPUGDCMongoDB at ZPUGDC
MongoDB at ZPUGDC
 

Recently uploaded

Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Hyundai Motor Group
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 

Recently uploaded (20)

Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 

Sharding with MongoDB -- MongoDC 2012

  • 1. Sharding with MongoDB Tyler Brock @TylerBrock
  • 5. Philosophy “Ruby is designed to make programmers happy.”
  • 6. Philosophy MongoDB is a database for developers.
  • 7. Philosophy Build
  • 8. Philosophy Build Scale
  • 9. Philosophy How does MongoDB scale?
  • 10. Philosophy > db.runCommand({enablesharding: "<dbname>" }) > db.runCommand({ shardcollection: "<namespace>", key: <shardkeypatternobject> })
  • 12. Simple Web Application app Read/Write datastore
  • 13. What happens when your working set exceeds memory?
  • 14. What happens if your write load is enormous?
  • 15. Vertical Scaling app datastore
  • 16. Vertical Scaling app datastore
  • 17. Vertical Scaling app app app datastore 68 GB Ram Raid10 EBS
  • 18. Vertical Scaling app app app datastore 512 GB Ram Raid10 SSD
  • 20. Horizontal Scaling app datastore 60gb
  • 21. Horizontal Scaling app datastore datastore datastore 20gb 20gb 20gb
  • 22. Horizontal Scaling app Routing Logic metadata datastore datastore datastore 20gb 20gb 20gb
  • 23. Horizontal Scaling app Routing Logic metadata datastore datastore datastore 60gb 20gb 20gb
  • 24. Horizontal Scaling app Routing Logic Balancer metadata datastore datastore datastore 60gb 20gb 20gb
  • 25. Horizontal Scaling app Routing Logic Balancer metadata datastore datastore datastore 30gb 30gb 30gb
  • 27. Shard Really is just a mongod (or replica set) mongod Where your data lives
  • 28. Config Server Mongod started with --configsvr option config Must have 3 (or 1 in development) Data is commited using 2 phase commit
  • 29. mongos Acts just like shard router / proxy One or as many as you want mongos Light weight -- can run on App servers Caches meta-data from config servers
  • 30. Routing Logic Balancing metadata datastore datastore datastore
  • 31. mongos metadata datastore datastore datastore
  • 32. app mongos metadata datastore datastore datastore
  • 33. app mongos config datastore datastore datastore
  • 34. app mongos config config config datastore datastore datastore
  • 35. app mongos config config config mongod mongod mongod
  • 36. app mongos config config config RS RS RS mongod mongod mongod mongod mongod mongod mongod mongod mongod
  • 37. app mongos config config config RS RS RS mongod mongod mongod mongod mongod mongod mongod mongod mongod
  • 39. Bring up mongods or Replica Sets mongod --shardsvr mongod --replSet --shardsvr RS RS RS mongod mongod mongod mongod mongod mongod mongod mongod mongod
  • 40. Bring up Config Servers mongod --configsvr config config config RS RS RS mongod mongod mongod mongod mongod mongod mongod mongod mongod
  • 41. Bring up Mongos mongos --configdb <list of configdb uris> mongos config config config RS RS RS mongod mongod mongod mongod mongod mongod mongod mongod mongod
  • 42. Connect to Mongos + Add Shards > use admin > db.runCommand({"addShard": <shard uri>}) +Enable Sharding > db.runCommand( { enablesharding : "<dbname>" } ); +Shard a Collection > db.runCommand( { shardcollection : "<namespace>", key : <key> });
  • 45. Keys test.users { name: “Joe”, email: “Joe@fake.com”, }, { name: “Bob”, email: “bob@fake.com”, }, { name: “Tyler”, email: “tyler@fake.com”, }
  • 46. Keys test.users { name: “Joe”, email: “Joe@fake.com”, > db.runCommand({ }, shardcollection: “test.users”, { key: { email: 1 } name: “Bob”, }) email: “bob@fake.com”, }, { name: “Tyler”, email: “tyler@fake.com”, }
  • 47. Keys test.users { name: “Joe”, email: “Joe@fake.com”, }, shardcollection: “test.users”, { key: { email: 1 } name: “Bob”, email: “bob@fake.com”, }, { name: “Tyler”, email: “tyler@fake.com”, }
  • 48. Keys test.users { name: “Joe”, email: “Joe@fake.com”, }, { key: { email: 1 } name: “Bob”, email: “bob@fake.com”, }, { name: “Tyler”, email: “tyler@fake.com”, }
  • 49. Chunks -∞ +∞
  • 50. Chunks -∞ +∞ joe@fake.com tyler@fake.com moe@fake.com
  • 51. Chunks Split! -∞ +∞ joe@fake.com tyler@fake.com moe@fake.com
  • 52. Chunks This is a Split! This is a chunk chunk -∞ +∞ joe@fake.com tyler@fake.com moe@fake.com
  • 53. Chunks -∞ +∞ joe@fake.com tyler@fake.com moe@fake.com
  • 54. Chunks -∞ +∞ joe@fake.com tyler@fake.com moe@fake.com
  • 55. Chunks Split! -∞ +∞ joe@fake.com tyler@fake.com moe@fake.com
  • 56. Splitting config mongos config config Shard 1 Shard 2 Shard 3 Shard 4
  • 57. Splitting config mongos config config Shard 1 Shard 2 Shard 3 Shard 4
  • 58. Splitting config mongos config config Shard 1 Shard 2 Shard 3 Shard 4
  • 59. Splitting config mongos config config Shard 1 Shard 2 Shard 3 Shard 4
  • 60. Splitting Split this big chunk into 2 config chunks mongos config config Shard 1 Shard 2 Shard 3 Shard 4
  • 61. Splitting config mongos config config Shard 1 Shard 2 Shard 3 Shard 4
  • 62. Splitting config mongos config These config chunks have split Shard 1 Shard 2 Shard 3 Shard 4
  • 63. Balancing config mongos config config Shard 1 Shard 2 Shard 3 Shard 4
  • 64. Balancing Shard1, move a chunk to config Shard2 mongos config config Shard 1 Shard 2 Shard 3 Shard 4
  • 65. Balancing config mongos config config Shard 1 Shard 2 Shard 3 Shard 4
  • 66. Balancing Shard1, move another chunk config to Shard3 mongos config config Shard 1 Shard 2 Shard 3 Shard 4
  • 67. Balancing config mongos config config Shard 1 Shard 2 Shard 3 Shard 4
  • 68. Balancing Shard1, move another chunk config to Shard4 mongos config config Shard 1 Shard 2 Shard 3 Shard 4
  • 69. Balancing config mongos config config Shard 1 Shard 2 Shard 3 Shard 4
  • 70. Balancing config mongos config config Shard 1 Shard 2 Shard 3 Shard 4
  • 71. How does MongoDB route my queries?
  • 72. Routed Request mongos shard shard shard
  • 73. Routed Request 1 1. Query arrives at Mongos mongos shard shard shard
  • 74. Routed Request 1 1. Query arrives at Mongos 2. Mongos routes query to a mongos single shard 2 shard shard shard
  • 75. Routed Request 1 1. Query arrives at Mongos 2. Mongos routes query to a mongos single shard 3. Shard returns results of 2 query 3 shard shard shard
  • 76. Routed Request 1 1. Query arrives at Mongos 4 2. Mongos routes query to a mongos single shard 3. Shard returns results of 2 query 4. Results returned to client 3 shard shard shard
  • 77. Scatter Gather Request mongos shard shard shard
  • 78. Scatter Gather Request 1 1. Query arrives at Mongos mongos shard shard shard
  • 79. Scatter Gather Request 1 1. Query arrives at Mongos 2. Mongos broadcasts query mongos to all shards 2 2 2 shard shard shard
  • 80. Scatter Gather Request 1 1. Query arrives at Mongos 2. Mongos broadcasts query mongos to all shards 3. Each shard returns results for query 2 2 2 3 3 3 shard shard shard
  • 81. Scatter Gather Request 1 1. Query arrives at Mongos 4 2. Mongos broadcasts query mongos to all shards 3. Each shard returns results for query 2 2 2 4. Results combined and 3 3 returned to client 3 shard shard shard
  • 82. Distributed Merge Sort Req. mongos shard shard shard
  • 83. Distributed Merge Sort Req. 1 1. Query arrives at Mongos mongos shard shard shard
  • 84. Distributed Merge Sort Req. 1 1. Query arrives at Mongos 2. Mongos broadcasts query mongos to all shards 2 2 2 shard shard shard
  • 85. Distributed Merge Sort Req. 1 1. Query arrives at Mongos 2. Mongos broadcasts query mongos to all shards 3. Each shard locally sorts results 2 2 2 shard shard shard 3 3 3
  • 86. Distributed Merge Sort Req. 1 1. Query arrives at Mongos 2. Mongos broadcasts query mongos to all shards 3. Each shard locally sorts results 2 2 2 4. Results returned to mongos 4 4 4 shard shard shard 3 3 3
  • 87. Distributed Merge Sort Req. 1 1. Query arrives at Mongos 2. Mongos broadcasts query mongos 5 to all shards 3. Each shard locally sorts results 2 2 2 4. Results returned to mongos 4 4 4 5. Mongos merges sorted results shard shard shard 3 3 3
  • 88. Distributed Merge Sort Req. 1 1. Query arrives at Mongos 6 2. Mongos broadcasts query mongos 5 to all shards 3. Each shard locally sorts results 2 2 2 4. Results returned to mongos 4 4 4 5. Mongos merges sorted results shard shard shard 3 3 3 6. Combined results returned to client
  • 89. Queries By Shard Key Routed db.users.find({email: “bob@10gen.com”}) Sorted by Routed in order db.users.find().sort({email:-1}) shard key Find by non Scatter Gather db.users.find({state:”NY”}) shard key Sorted by Distributed merge db.users.find().sort({state:1}) sort non shard key
  • 90. Writes Inserts Requires shard key db.users.insert({ name: “Bob”, email: “Bob@fake.com”}) Removes Routed db.users.delete({ email: “bob@fake.com”}) Scattered db.users.delete({name: “Bob”}) Updates Routed db.users.update( {email: “bob@10gen.com”}, {$set: { state: “NY”}}) Scattered db.users.update( {state: “CA”}, {$set:{ state: “NY”}} )
  • 91. How do I choose my shard key?
  • 92. Rule of Thumb Choose a field that is common to your queries.
  • 93. Cardinality Chunks should be able to split.
  • 94. Chunks should be able to split { node: "ny153.example.com", application: "apache", time: "2011-01-02T21:21:56Z", level: "ERROR", msg: "something is broken" } Bad {node: 1}
  • 95. Chunks should be able to split { node: "ny153.example.com", application: "apache", time: "2011-01-02T21:21:56Z", level: "ERROR", msg: "something is broken" } Bad {node: 1} Better {node:1, time:1}
  • 96. Write Scaling Writes should be distributed.
  • 97. Writes should be distributed { node: "ny153.example.com", application: "apache", time: "2011-01-02T21:21:56Z", level: "ERROR", msg: "something is broken" } Bad { time : 1 }
  • 98. Writes should be distributed { node: "ny153.example.com", application: "apache", time: "2011-01-02T21:21:56Z", level: "ERROR", msg: "something is broken" } Bad { time : 1 } Better {node:1, application:1, time:1}
  • 99. Query Isolation & Data Locality Queries should be routed to one shard.
  • 100. Queries should be routed to one shard { node: "ny153.example.com", application: "apache", time: "2011-01-02T21:21:56Z", level: "ERROR", msg: "something is broken” } Bad {msg: 1, node: 1}
  • 101. Queries should be routed to one shard { node: "ny153.example.com", application: "apache", time: "2011-01-02T21:21:56Z", level: "ERROR", msg: "something is broken” } Bad {msg: 1, node: 1} Better {node: 1, time: 1}
  • 102. > db.runCommand({enablesharding: "<dbname>" }) > db.runCommand({ shardcollection: "<namespace>", key: <shardkeypatternobject> })
  • 106. Config Servers mongod
  • 107. Config Servers mongod mongod mongod
  • 108. mongoDB Scaling - Single Node read node_a1 write
  • 109. Read scaling - add Replicas read node_b1 node_a1 write
  • 110. Read scaling - add Replicas read node_c1 node_b1 node_a1 write
  • 111. Write scaling - Sharding read shard1 node_c1 node_b1 node_a1 write
  • 112. Write scaling - add shards read shard1 shard2 node_c1 node_c2 node_b1 node_b2 node_a1 node_a2 write
  • 113. Write scaling - add shards read shard1 shard2 shard3 node_c1 node_c2 node_c3 node_b1 node_b2 node_b3 node_a1 node_a2 node_a3 write