SlideShare uma empresa Scribd logo
1 de 100
Baixar para ler offline
Sharding with
 MongoDB


          Tyler Brock
          tyler@10gen.com
          @TylerBrock
Philosophy
 Concepts
Architecture
 Mechanics
Philosophy
Philosophy




             MongoDB is a
             database for
             developers.
Philosophy




             Build
Philosophy




             Build
             Scale
Philosophy




       How to Draw an Owl
Philosophy

       How to Draw an Owl
Philosophy

                Draw Two Circles
> 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


                        128 GB Ram
                        Raid10 SSD
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
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.
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}
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}
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> });

Mais conteúdo relacionado

Destaque (20)

Practica 3
Practica 3Practica 3
Practica 3
 
PIGD
PIGDPIGD
PIGD
 
Manual do cuidador da pessoa idosa
Manual do cuidador da pessoa idosaManual do cuidador da pessoa idosa
Manual do cuidador da pessoa idosa
 
Branchless Banking in Pakistan
Branchless Banking in PakistanBranchless Banking in Pakistan
Branchless Banking in Pakistan
 
Alimentacao
AlimentacaoAlimentacao
Alimentacao
 
Liste laureats
Liste laureatsListe laureats
Liste laureats
 
TIC en Nicaragua
TIC en NicaraguaTIC en Nicaragua
TIC en Nicaragua
 
mantenimiento a nuestra pc
mantenimiento a nuestra pcmantenimiento a nuestra pc
mantenimiento a nuestra pc
 
Observacion dylan
Observacion dylanObservacion dylan
Observacion dylan
 
Libro La cooperación internacional publicación acep-kas
Libro La cooperación internacional   publicación acep-kasLibro La cooperación internacional   publicación acep-kas
Libro La cooperación internacional publicación acep-kas
 
Tercera sesión act. 1
Tercera sesión act. 1Tercera sesión act. 1
Tercera sesión act. 1
 
Ii petefinal
Ii  petefinalIi  petefinal
Ii petefinal
 
Dez passos para uma alimentação
Dez passos para uma alimentaçãoDez passos para uma alimentação
Dez passos para uma alimentação
 
Unidad 2. modelo entidad relacion
Unidad 2. modelo entidad relacionUnidad 2. modelo entidad relacion
Unidad 2. modelo entidad relacion
 
Intro psicologia y terapias cognitivas
Intro psicologia y terapias cognitivasIntro psicologia y terapias cognitivas
Intro psicologia y terapias cognitivas
 
Ensamble y desensamble del portatil
Ensamble y desensamble del portatilEnsamble y desensamble del portatil
Ensamble y desensamble del portatil
 
Unidad IV - La Evaluación
Unidad IV - La EvaluaciónUnidad IV - La Evaluación
Unidad IV - La Evaluación
 
Manual para ensamble y desensamble del pc
Manual para ensamble y desensamble del pcManual para ensamble y desensamble del pc
Manual para ensamble y desensamble del pc
 
Mysql Administracion
Mysql AdministracionMysql Administracion
Mysql Administracion
 
Posiciones anatomicas
Posiciones anatomicas Posiciones anatomicas
Posiciones anatomicas
 

Semelhante a Sharding MongoDB with MongoDB

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
 
Leaving jsps in the dust
Leaving jsps in the dustLeaving jsps in the dust
Leaving jsps in the dustVeena Basavaraj
 
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
 
CouchDB @ red dirt ruby conference
CouchDB @ red dirt ruby conferenceCouchDB @ red dirt ruby conference
CouchDB @ red dirt ruby conferenceleinweber
 
Sharding morning session
Sharding   morning sessionSharding   morning session
Sharding morning sessionMongoDB
 
MongoDB + node.js で作るソーシャルゲーム
MongoDB + node.js で作るソーシャルゲームMongoDB + node.js で作るソーシャルゲーム
MongoDB + node.js で作るソーシャルゲームSuguru Namura
 
MongoDB Basic Concepts
MongoDB Basic ConceptsMongoDB Basic Concepts
MongoDB Basic ConceptsMongoDB
 
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 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
 
JSON Web Tokens Will Improve Your Life
JSON Web Tokens Will Improve Your LifeJSON Web Tokens Will Improve Your Life
JSON Web Tokens Will Improve Your LifeJohn Anderson
 
SocialCaddy: "Clash of the Titans: Erlang Clusters and Google AppEngine"
SocialCaddy: "Clash of the Titans: Erlang Clusters  and Google AppEngine"SocialCaddy: "Clash of the Titans: Erlang Clusters  and Google AppEngine"
SocialCaddy: "Clash of the Titans: Erlang Clusters and Google AppEngine"SocialCaddy
 
Clearly, I Have Made Some Bad Decisions
Clearly, I Have Made Some Bad DecisionsClearly, I Have Made Some Bad Decisions
Clearly, I Have Made Some Bad DecisionsJonathan Hitchcock
 
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
 
BDD - Buzzword Driven Development - Build the next cool app for fun and for.....
BDD - Buzzword Driven Development - Build the next cool app for fun and for.....BDD - Buzzword Driven Development - Build the next cool app for fun and for.....
BDD - Buzzword Driven Development - Build the next cool app for fun and for.....Michele Orselli
 
BDD - Buzzword Driven Development - Build the next cool app for fun and for.....
BDD - Buzzword Driven Development - Build the next cool app for fun and for.....BDD - Buzzword Driven Development - Build the next cool app for fun and for.....
BDD - Buzzword Driven Development - Build the next cool app for fun and for.....Alessandro Cinelli (cirpo)
 

Semelhante a Sharding MongoDB with MongoDB (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 ...
 
Leaving jsps in the dust
Leaving jsps in the dustLeaving jsps in the dust
Leaving jsps in the dust
 
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...
 
CouchDB @ red dirt ruby conference
CouchDB @ red dirt ruby conferenceCouchDB @ red dirt ruby conference
CouchDB @ red dirt ruby conference
 
Sharding morning session
Sharding   morning sessionSharding   morning session
Sharding morning session
 
MongoDB + node.js で作るソーシャルゲーム
MongoDB + node.js で作るソーシャルゲームMongoDB + node.js で作るソーシャルゲーム
MongoDB + node.js で作るソーシャルゲーム
 
Opa hackathon
Opa hackathonOpa hackathon
Opa hackathon
 
MongoDB Basic Concepts
MongoDB Basic ConceptsMongoDB Basic Concepts
MongoDB Basic Concepts
 
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
 
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
 
Tactical Information Gathering
Tactical Information GatheringTactical Information Gathering
Tactical Information Gathering
 
JSON Web Tokens Will Improve Your Life
JSON Web Tokens Will Improve Your LifeJSON Web Tokens Will Improve Your Life
JSON Web Tokens Will Improve Your Life
 
mongoDB at Visibiz
mongoDB at VisibizmongoDB at Visibiz
mongoDB at Visibiz
 
SocialCaddy: "Clash of the Titans: Erlang Clusters and Google AppEngine"
SocialCaddy: "Clash of the Titans: Erlang Clusters  and Google AppEngine"SocialCaddy: "Clash of the Titans: Erlang Clusters  and Google AppEngine"
SocialCaddy: "Clash of the Titans: Erlang Clusters and Google AppEngine"
 
Clearly, I Have Made Some Bad Decisions
Clearly, I Have Made Some Bad DecisionsClearly, I Have Made Some Bad Decisions
Clearly, I Have Made Some Bad Decisions
 
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...
 
BDD - Buzzword Driven Development - Build the next cool app for fun and for.....
BDD - Buzzword Driven Development - Build the next cool app for fun and for.....BDD - Buzzword Driven Development - Build the next cool app for fun and for.....
BDD - Buzzword Driven Development - Build the next cool app for fun and for.....
 
BDD - Buzzword Driven Development - Build the next cool app for fun and for.....
BDD - Buzzword Driven Development - Build the next cool app for fun and for.....BDD - Buzzword Driven Development - Build the next cool app for fun and for.....
BDD - Buzzword Driven Development - Build the next cool app for fun and for.....
 
SFScon 2020 - Matteo Ghetta - DataPlotly - D3-like plots in QGIS
SFScon 2020 - Matteo Ghetta - DataPlotly - D3-like plots in QGISSFScon 2020 - Matteo Ghetta - DataPlotly - D3-like plots in QGIS
SFScon 2020 - Matteo Ghetta - DataPlotly - D3-like plots in QGIS
 
NoSQL databases
NoSQL databasesNoSQL databases
NoSQL databases
 

Último

Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 

Último (20)

Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 

Sharding MongoDB with MongoDB

  • 1. Sharding with MongoDB Tyler Brock tyler@10gen.com @TylerBrock
  • 4. Philosophy MongoDB is a database for developers.
  • 5. Philosophy Build
  • 6. Philosophy Build Scale
  • 7. Philosophy How to Draw an Owl
  • 8. Philosophy How to Draw an Owl
  • 9. Philosophy Draw Two Circles > db.runCommand({enablesharding: "<dbname>" }) > db.runCommand({ shardcollection: "<namespace>", key: <shardkeypatternobject> })
  • 11. Simple Web Application app Read/Write datastore
  • 12. What happens when your working set exceeds memory?
  • 13. What happens if your write load is enormous?
  • 14. Vertical Scaling app datastore
  • 15. Vertical Scaling app datastore
  • 16. Vertical Scaling app app app datastore 68 GB Ram Raid10 EBS
  • 17. Vertical Scaling app app app datastore 128 GB Ram Raid10 SSD
  • 18.
  • 19. Horizontal Scaling app datastore 60gb
  • 20. Horizontal Scaling app datastore datastore datastore 20gb 20gb 20gb
  • 21. Horizontal Scaling app Routing Logic metadata datastore datastore datastore 20gb 20gb 20gb
  • 22. Horizontal Scaling app Routing Logic metadata datastore datastore datastore 60gb 20gb 20gb
  • 23. Horizontal Scaling app Routing Logic Balancer metadata datastore datastore datastore 60gb 20gb 20gb
  • 24. Horizontal Scaling app Routing Logic Balancer metadata datastore datastore datastore 30gb 30gb 30gb
  • 26. Shard Really is just a mongod (or replica set) mongod Where your data lives
  • 27. Config Server Mongod started with --configsvr option config Must have 3 (or 1 in development) Data is commited using 2 phase commit
  • 28. 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
  • 29. Routing Logic Balancing metadata datastore datastore datastore
  • 30. mongos metadata datastore datastore datastore
  • 31. app mongos metadata datastore datastore datastore
  • 32. app mongos config datastore datastore datastore
  • 33. app mongos config config config datastore datastore datastore
  • 34. app mongos config config config mongod mongod mongod
  • 35. app mongos config config config RS RS RS mongod mongod mongod mongod mongod mongod mongod mongod mongod
  • 36. app mongos config config config RS RS RS mongod mongod mongod mongod mongod mongod mongod mongod mongod
  • 39. Keys test.users { name: “Joe”, email: “Joe@fake.com”, }, { name: “Bob”, email: “bob@fake.com”, }, { name: “Tyler”, email: “tyler@fake.com”, }
  • 40. 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”, }
  • 41. 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”, }
  • 42. Keys test.users { name: “Joe”, email: “Joe@fake.com”, }, { key: { email: 1 } name: “Bob”, email: “bob@fake.com”, }, { name: “Tyler”, email: “tyler@fake.com”, }
  • 43. Chunks -∞ +∞
  • 44. Chunks -∞ +∞ joe@fake.com tyler@fake.com moe@fake.com
  • 45. Chunks Split! -∞ +∞ joe@fake.com tyler@fake.com moe@fake.com
  • 46. Chunks This is a Split! This is a chunk chunk -∞ +∞ joe@fake.com tyler@fake.com moe@fake.com
  • 47. Chunks -∞ +∞ joe@fake.com tyler@fake.com moe@fake.com
  • 48. Chunks -∞ +∞ joe@fake.com tyler@fake.com moe@fake.com
  • 49. Chunks Split! -∞ +∞ joe@fake.com tyler@fake.com moe@fake.com
  • 50. Splitting config mongos config config Shard 1 Shard 2 Shard 3 Shard 4
  • 51. Splitting config mongos config config Shard 1 Shard 2 Shard 3 Shard 4
  • 52. Splitting config mongos config config Shard 1 Shard 2 Shard 3 Shard 4
  • 53. Splitting config mongos config config Shard 1 Shard 2 Shard 3 Shard 4
  • 54. Splitting Split this big chunk into 2 config chunks mongos config config Shard 1 Shard 2 Shard 3 Shard 4
  • 55. Splitting config mongos config config Shard 1 Shard 2 Shard 3 Shard 4
  • 56. Splitting config mongos config These config chunks have split Shard 1 Shard 2 Shard 3 Shard 4
  • 57. Balancing config mongos config config Shard 1 Shard 2 Shard 3 Shard 4
  • 58. Balancing Shard1, move a chunk to config Shard2 mongos config config Shard 1 Shard 2 Shard 3 Shard 4
  • 59. Balancing config mongos config config Shard 1 Shard 2 Shard 3 Shard 4
  • 60. Balancing Shard1, move another chunk config to Shard3 mongos config config Shard 1 Shard 2 Shard 3 Shard 4
  • 61. Balancing config mongos config config Shard 1 Shard 2 Shard 3 Shard 4
  • 62. Balancing Shard1, move another chunk config to Shard4 mongos config config Shard 1 Shard 2 Shard 3 Shard 4
  • 63. Balancing config mongos config config Shard 1 Shard 2 Shard 3 Shard 4
  • 64. Balancing config mongos config config Shard 1 Shard 2 Shard 3 Shard 4
  • 65. How does MongoDB route my queries?
  • 66. Routed Request mongos shard shard shard
  • 67. Routed Request 1 1. Query arrives at Mongos mongos shard shard shard
  • 68. Routed Request 1 1. Query arrives at Mongos 2. Mongos routes query to a mongos single shard 2 shard shard shard
  • 69. 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
  • 70. 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
  • 71. Scatter Gather Request mongos shard shard shard
  • 72. Scatter Gather Request 1 1. Query arrives at Mongos mongos shard shard shard
  • 73. Scatter Gather Request 1 1. Query arrives at Mongos 2. Mongos broadcasts query mongos to all shards 2 2 2 shard shard shard
  • 74. 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
  • 75. 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
  • 76. Distributed Merge Sort Req. mongos shard shard shard
  • 77. Distributed Merge Sort Req. 1 1. Query arrives at Mongos mongos shard shard shard
  • 78. Distributed Merge Sort Req. 1 1. Query arrives at Mongos 2. Mongos broadcasts query mongos to all shards 2 2 2 shard shard shard
  • 79. 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
  • 80. 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
  • 81. 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
  • 82. 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
  • 83. 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
  • 84. 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”}} )
  • 85. How do I choose my shard key?
  • 86. Rule of Thumb Choose a field that is common to your queries.
  • 87. Write Scaling Writes should be distributed.
  • 88. 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 }
  • 89. 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}
  • 90. Query Isolation & Data Locality Queries should be routed to one shard.
  • 91. 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}
  • 92. 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}
  • 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}
  • 97. Bring up mongods or Replica Sets mongod --shardsvr mongod --replSet --shardsvr RS RS RS mongod mongod mongod mongod mongod mongod mongod mongod mongod
  • 98. Bring up Config Servers mongod --configsvr config config config RS RS RS mongod mongod mongod mongod mongod mongod mongod mongod mongod
  • 99. 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
  • 100. 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> });