SlideShare una empresa de Scribd logo
1 de 32
Sharding
Jared Rosoff (@forjared)
Overview
• Architecture
• How it works
• Use Cases
SHARDING
ARCHITECTURE
Architecture
                             Shards

           mongod      mongod             mongod
                                                               ...
           mongod      mongod             mongod

           mongod      mongod             mongod
 Config
 Servers

mongod
                       mongos           mongos           ...
mongod

mongod
                    client    client   client   client
mongos
                                                                     • Shard Router
                             Shards
                                                                     • Acts just like a MongoD
           mongod      mongod             mongod

           mongod      mongod             mongod
                                                               ...
                                                                     • 1 or as many as you
 Config
           mongod      mongod             mongod                       want
 Servers

mongod
                                                                     • Can run on App
mongod
                       mongos           mongos           ...
                                                                       Servers
                                                                     • Caches meta-data from
mongod
                    client    client   client   client

                                                                       config servers
Config Server
                                                                     • 3 of them
                             Shards
                                                                     • Changes use 2 phase
           mongod      mongod             mongod

           mongod      mongod             mongod
                                                               ...     commit
 Config
           mongod      mongod             mongod                     • If any are down, meta
 Servers

mongod
                                                                       data goes read only
mongod
                       mongos           mongos           ...
                                                                     • System is online as
mongod
                    client    client   client   client
                                                                       long as 1/3 is up
HOW IT WORKS
Keys
> db.runCommand( { shardcollection: “test.users”,
                   key: { email: 1 }} )



      {
          name: “Jared”,
          email: “jsr@10gen.com”,
      }
      {
          name: “Scott”,
          email: “scott@10gen.com”,
      }
      {
          name: “Dan”,
          email: “dan@10gen.com”,
      }
Chunks


-∞            +
              ∞
Chunks


-∞                                              +
                                                ∞


     dan@10gen.com            scott@10gen.com

              jsr@10gen.com
Chunks
                     Split!



-∞                                              +
                                                ∞


     dan@10gen.com            scott@10gen.com

              jsr@10gen.com
Chunks
     This is a              Split!         This is a
      chunk                                 chunk


-∞                                                     +
                                                       ∞


            dan@10gen.com            scott@10gen.com

                     jsr@10gen.com
Chunks


-∞                                              +
                                                ∞


     dan@10gen.com            scott@10gen.com

              jsr@10gen.com
Chunks


-∞                                              +
                                                ∞


     dan@10gen.com            scott@10gen.com

              jsr@10gen.com
Chunks
     Split!



-∞                                              +
                                                ∞


     dan@10gen.com            scott@10gen.com

              jsr@10gen.com
Chunks
Min Key           Max Key           Shard
-∞                adam@10gen.com    1
adam@10gen.com    jared@10gen.com   1
jared@10gen.com   scott@10gen.com   1
scott@10gen.com   +∞                1


• Stored in the config servers
• Cached in MongoS
• Used to route requests and keep cluster
  balanced
Balancing
                                      mongos
                                                                                  config
                                      balancer
                                                                                  config
Chunks!
                                                                                  config




  1    2    3    4    13    14   15   16         25    26   27   28   37    38   39   40

  5    6    7    8    17    18   19   20         29    30   31   32   41    42   43   44

  9    10   11   12   21    22   23   24         33    34   35   36   45    46   47   48


      Shard 1              Shard 2                    Shard 3              Shard 4
Balancing
                                      mongos
                                                                                  config
                                      balancer
                                                                                  config

                    Imbalance
                     Imbalance                                                    config




1    2    3    4

5    6    7    8

9    10   11   12     21    22   23   24         33    34   35   36   45    46   47   48


    Shard 1                Shard 2                    Shard 3              Shard 4
Balancing
                                     mongos
                                                                                 config
                                     balancer
                                                                                 config

                               Move chunk 1                                      config
                               to Shard 2




1    2    3    4

5    6    7    8

9    10   11   12   21    22    23   24         33    34   35   36   45    46   47   48


    Shard 1              Shard 2                     Shard 3              Shard 4
Balancing
                                    mongos
                                                                                config
                                    balancer
                                                                                config

                                                                                config




1    2    3    4

5    6    7    8

9    10   11   12   21    22   23   24         33    34   35   36   45    46   47   48


    Shard 1              Shard 2                    Shard 3              Shard 4
Balancing
                                    mongos
                                                                                config
                                    balancer
                                                                                config
                                         Chunk 1 now lives
                                            on Shard 2                          config




     2    3    4

5    6    7    8    1

9    10   11   12   21    22   23   24         33    34   35   36   45    46   47   48


    Shard 1              Shard 2                    Shard 3              Shard 4
ROUTING
Routed Request
                         1

                                        1. Query arrives at
                          4
                     mongos                MongoS
                                        2. MongoS routes query
                                           to a single shard
                                        3. Shard returns results
                                           of query
                     2
                                        4. Results returned to
                                           client
                     3




Shard 1    Shard 2            Shard 3
Scatter Gather
                            1

                             4                 1. Query arrives at
                        mongos                    MongoS
                                               2. MongoS broadcasts
                                                  query to all shards
                                               3. Each shard returns
                                                  results for query
          2                                    4. Results combined
                    2            2                and returned to client
                                      3
          3             3



Shard 1       Shard 2                Shard 3
Distributed Merge Sort
                            1

                             6                1. Query arrives at
                        mongos                   MongoS
                                              2. MongoS broadcasts
                            5
                                                 query to all shards
                                              3. Each shard locally
                                                 sorts results
          2                                   4. Results returned to
                    2           2                mongos
          4             4            4        5. MongoS merge sorts
                                                 individual results
3                                             6. Combined sorted
              3                           3
                                                 result returned to
Shard 1       Shard 2               Shard 3      client
Writes

Inserts   Requires shard   db.users.insert({
          key                name: “Jared”,
                             email: “jsr@10gen.com”})
Removes   Routed           db.users.delete({
                             email: “jsr@10gen.com”})
          Scattered        db.users.delete({name: “Jared”})
Updates   Routed           db.users.update(
                             {email: “jsr@10gen.com”},
                             {$set: { state: “CA”}})
          Scattered        db.users.update(
                             {state: “FZ”},
                             {$set:{ state: “CA”}} )
Queries

By Shard      Routed            db.users.find(
                                  {email: “jsr@10gen.com”})
Key
Sorted by     Routed in order   db.users.find().sort({email:-1})
shard key
Find by non   Scatter Gather    db.users.find({state:”CA”})
shard key
Sorted by     Distributed       db.users.find().sort({state:1})
              merge sort
non shard
key
EXAMPLES
User Profiles
{
                              • Shard by email
    name: “Jared”,
    email: “jsr@10gen.com”,   • Lookup by email hits
    addresses: [                1 node
      {state: “CA”}
    ]                         • Index on
}                               {“addresses.state”:1}
Activity Stream
{
                                • Shard by user_id
    user_id: “jsr@10gen.com”,
    event_id: “Logged in”,      • Looking up a stream
    data: “…”                     hits 1 node
}
                                • Writing is evenly
                                  distributed
                                • Index on {“event_id”:1}
                                  for deletes
Photos
{
                         • What’s the right key?
    photo_id: ???,
    data: BinData(…)       –   Auto Increment?
}                          –   MD5( data )
                           –   Now() + MD5(data)
                           –   Month() + MD5(data)
Mongosv 2011 - Sharding

Más contenido relacionado

Similar a Mongosv 2011 - Sharding

Firefox3.5 And Next
Firefox3.5 And NextFirefox3.5 And Next
Firefox3.5 And NextChanny Yun
 
Style Guide Package
Style Guide PackageStyle Guide Package
Style Guide Package4Ward
 
The science behind breaking world records
The science behind breaking world recordsThe science behind breaking world records
The science behind breaking world recordsgwfestival
 
Web design annual plan annual plan
Web design annual plan annual planWeb design annual plan annual plan
Web design annual plan annual plancal1968
 
『Ficia』インフラとPerlにまつわるエトセトラ
『Ficia』インフラとPerlにまつわるエトセトラ『Ficia』インフラとPerlにまつわるエトセトラ
『Ficia』インフラとPerlにまつわるエトセトラMasaaki HIROSE
 
Business Startup Package
Business Startup PackageBusiness Startup Package
Business Startup Package4Ward
 
From java to rails
From java to railsFrom java to rails
From java to railsjokry
 
Business Corporate Pack
Business Corporate PackBusiness Corporate Pack
Business Corporate Pack4Ward
 
Webinar: Top 5 Mistakes Your Don't Want to Make When Moving to the Cloud
Webinar: Top 5 Mistakes Your Don't Want to Make When Moving to the CloudWebinar: Top 5 Mistakes Your Don't Want to Make When Moving to the Cloud
Webinar: Top 5 Mistakes Your Don't Want to Make When Moving to the CloudInternap
 
Intro to Table-Grouping™ technology
Intro to Table-Grouping™ technologyIntro to Table-Grouping™ technology
Intro to Table-Grouping™ technologyDavid McFarlane
 
クックパッドでのemr利用事例
クックパッドでのemr利用事例クックパッドでのemr利用事例
クックパッドでのemr利用事例Tatsuya Sasaki
 
iOS Architecture and MVC
iOS Architecture and MVCiOS Architecture and MVC
iOS Architecture and MVCMarian Ignev
 
Using Database Constraints Wisely
Using Database Constraints WiselyUsing Database Constraints Wisely
Using Database Constraints Wiselybarunio
 
OSC11 - The future is now for all your Business Processes
OSC11 - The future is now for all your Business ProcessesOSC11 - The future is now for all your Business Processes
OSC11 - The future is now for all your Business ProcessesEric D. Schabell
 
RIPE Atlas
RIPE AtlasRIPE Atlas
RIPE AtlasRIPE NCC
 
Introduction to Clojure's STM
Introduction to Clojure's STMIntroduction to Clojure's STM
Introduction to Clojure's STMFronx Wurmus
 
The Application Development Landscape - 2011
The Application Development Landscape -  2011The Application Development Landscape -  2011
The Application Development Landscape - 2011David Skok
 
Implementing Metadata Standards for a Digital Audiovisual Preservation Reposi...
Implementing Metadata Standards for a Digital Audiovisual Preservation Reposi...Implementing Metadata Standards for a Digital Audiovisual Preservation Reposi...
Implementing Metadata Standards for a Digital Audiovisual Preservation Reposi...Kara Van Malssen
 
HTW2011: Branko Milutinovic - Social gaming - lessons learned
HTW2011: Branko Milutinovic - Social gaming - lessons learnedHTW2011: Branko Milutinovic - Social gaming - lessons learned
HTW2011: Branko Milutinovic - Social gaming - lessons learnedHow to Web
 

Similar a Mongosv 2011 - Sharding (20)

Struds overview
Struds overviewStruds overview
Struds overview
 
Firefox3.5 And Next
Firefox3.5 And NextFirefox3.5 And Next
Firefox3.5 And Next
 
Style Guide Package
Style Guide PackageStyle Guide Package
Style Guide Package
 
The science behind breaking world records
The science behind breaking world recordsThe science behind breaking world records
The science behind breaking world records
 
Web design annual plan annual plan
Web design annual plan annual planWeb design annual plan annual plan
Web design annual plan annual plan
 
『Ficia』インフラとPerlにまつわるエトセトラ
『Ficia』インフラとPerlにまつわるエトセトラ『Ficia』インフラとPerlにまつわるエトセトラ
『Ficia』インフラとPerlにまつわるエトセトラ
 
Business Startup Package
Business Startup PackageBusiness Startup Package
Business Startup Package
 
From java to rails
From java to railsFrom java to rails
From java to rails
 
Business Corporate Pack
Business Corporate PackBusiness Corporate Pack
Business Corporate Pack
 
Webinar: Top 5 Mistakes Your Don't Want to Make When Moving to the Cloud
Webinar: Top 5 Mistakes Your Don't Want to Make When Moving to the CloudWebinar: Top 5 Mistakes Your Don't Want to Make When Moving to the Cloud
Webinar: Top 5 Mistakes Your Don't Want to Make When Moving to the Cloud
 
Intro to Table-Grouping™ technology
Intro to Table-Grouping™ technologyIntro to Table-Grouping™ technology
Intro to Table-Grouping™ technology
 
クックパッドでのemr利用事例
クックパッドでのemr利用事例クックパッドでのemr利用事例
クックパッドでのemr利用事例
 
iOS Architecture and MVC
iOS Architecture and MVCiOS Architecture and MVC
iOS Architecture and MVC
 
Using Database Constraints Wisely
Using Database Constraints WiselyUsing Database Constraints Wisely
Using Database Constraints Wisely
 
OSC11 - The future is now for all your Business Processes
OSC11 - The future is now for all your Business ProcessesOSC11 - The future is now for all your Business Processes
OSC11 - The future is now for all your Business Processes
 
RIPE Atlas
RIPE AtlasRIPE Atlas
RIPE Atlas
 
Introduction to Clojure's STM
Introduction to Clojure's STMIntroduction to Clojure's STM
Introduction to Clojure's STM
 
The Application Development Landscape - 2011
The Application Development Landscape -  2011The Application Development Landscape -  2011
The Application Development Landscape - 2011
 
Implementing Metadata Standards for a Digital Audiovisual Preservation Reposi...
Implementing Metadata Standards for a Digital Audiovisual Preservation Reposi...Implementing Metadata Standards for a Digital Audiovisual Preservation Reposi...
Implementing Metadata Standards for a Digital Audiovisual Preservation Reposi...
 
HTW2011: Branko Milutinovic - Social gaming - lessons learned
HTW2011: Branko Milutinovic - Social gaming - lessons learnedHTW2011: Branko Milutinovic - Social gaming - lessons learned
HTW2011: Branko Milutinovic - Social gaming - lessons learned
 

Último

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
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 

Último (20)

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
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 

Mongosv 2011 - Sharding

  • 2. Overview • Architecture • How it works • Use Cases
  • 4. Architecture Shards mongod mongod mongod ... mongod mongod mongod mongod mongod mongod Config Servers mongod mongos mongos ... mongod mongod client client client client
  • 5. mongos • Shard Router Shards • Acts just like a MongoD mongod mongod mongod mongod mongod mongod ... • 1 or as many as you Config mongod mongod mongod want Servers mongod • Can run on App mongod mongos mongos ... Servers • Caches meta-data from mongod client client client client config servers
  • 6. Config Server • 3 of them Shards • Changes use 2 phase mongod mongod mongod mongod mongod mongod ... commit Config mongod mongod mongod • If any are down, meta Servers mongod data goes read only mongod mongos mongos ... • System is online as mongod client client client client long as 1/3 is up
  • 8. Keys > db.runCommand( { shardcollection: “test.users”, key: { email: 1 }} ) { name: “Jared”, email: “jsr@10gen.com”, } { name: “Scott”, email: “scott@10gen.com”, } { name: “Dan”, email: “dan@10gen.com”, }
  • 9. Chunks -∞ + ∞
  • 10. Chunks -∞ + ∞ dan@10gen.com scott@10gen.com jsr@10gen.com
  • 11. Chunks Split! -∞ + ∞ dan@10gen.com scott@10gen.com jsr@10gen.com
  • 12. Chunks This is a Split! This is a chunk chunk -∞ + ∞ dan@10gen.com scott@10gen.com jsr@10gen.com
  • 13. Chunks -∞ + ∞ dan@10gen.com scott@10gen.com jsr@10gen.com
  • 14. Chunks -∞ + ∞ dan@10gen.com scott@10gen.com jsr@10gen.com
  • 15. Chunks Split! -∞ + ∞ dan@10gen.com scott@10gen.com jsr@10gen.com
  • 16. Chunks Min Key Max Key Shard -∞ adam@10gen.com 1 adam@10gen.com jared@10gen.com 1 jared@10gen.com scott@10gen.com 1 scott@10gen.com +∞ 1 • Stored in the config servers • Cached in MongoS • Used to route requests and keep cluster balanced
  • 17. Balancing mongos config balancer config Chunks! config 1 2 3 4 13 14 15 16 25 26 27 28 37 38 39 40 5 6 7 8 17 18 19 20 29 30 31 32 41 42 43 44 9 10 11 12 21 22 23 24 33 34 35 36 45 46 47 48 Shard 1 Shard 2 Shard 3 Shard 4
  • 18. Balancing mongos config balancer config Imbalance Imbalance config 1 2 3 4 5 6 7 8 9 10 11 12 21 22 23 24 33 34 35 36 45 46 47 48 Shard 1 Shard 2 Shard 3 Shard 4
  • 19. Balancing mongos config balancer config Move chunk 1 config to Shard 2 1 2 3 4 5 6 7 8 9 10 11 12 21 22 23 24 33 34 35 36 45 46 47 48 Shard 1 Shard 2 Shard 3 Shard 4
  • 20. Balancing mongos config balancer config config 1 2 3 4 5 6 7 8 9 10 11 12 21 22 23 24 33 34 35 36 45 46 47 48 Shard 1 Shard 2 Shard 3 Shard 4
  • 21. Balancing mongos config balancer config Chunk 1 now lives on Shard 2 config 2 3 4 5 6 7 8 1 9 10 11 12 21 22 23 24 33 34 35 36 45 46 47 48 Shard 1 Shard 2 Shard 3 Shard 4
  • 23. Routed Request 1 1. Query arrives at 4 mongos MongoS 2. MongoS routes query to a single shard 3. Shard returns results of query 2 4. Results returned to client 3 Shard 1 Shard 2 Shard 3
  • 24. Scatter Gather 1 4 1. Query arrives at mongos MongoS 2. MongoS broadcasts query to all shards 3. Each shard returns results for query 2 4. Results combined 2 2 and returned to client 3 3 3 Shard 1 Shard 2 Shard 3
  • 25. Distributed Merge Sort 1 6 1. Query arrives at mongos MongoS 2. MongoS broadcasts 5 query to all shards 3. Each shard locally sorts results 2 4. Results returned to 2 2 mongos 4 4 4 5. MongoS merge sorts individual results 3 6. Combined sorted 3 3 result returned to Shard 1 Shard 2 Shard 3 client
  • 26. Writes Inserts Requires shard db.users.insert({ key name: “Jared”, email: “jsr@10gen.com”}) Removes Routed db.users.delete({ email: “jsr@10gen.com”}) Scattered db.users.delete({name: “Jared”}) Updates Routed db.users.update( {email: “jsr@10gen.com”}, {$set: { state: “CA”}}) Scattered db.users.update( {state: “FZ”}, {$set:{ state: “CA”}} )
  • 27. Queries By Shard Routed db.users.find( {email: “jsr@10gen.com”}) Key Sorted by Routed in order db.users.find().sort({email:-1}) shard key Find by non Scatter Gather db.users.find({state:”CA”}) shard key Sorted by Distributed db.users.find().sort({state:1}) merge sort non shard key
  • 29. User Profiles { • Shard by email name: “Jared”, email: “jsr@10gen.com”, • Lookup by email hits addresses: [ 1 node {state: “CA”} ] • Index on } {“addresses.state”:1}
  • 30. Activity Stream { • Shard by user_id user_id: “jsr@10gen.com”, event_id: “Logged in”, • Looking up a stream data: “…” hits 1 node } • Writing is evenly distributed • Index on {“event_id”:1} for deletes
  • 31. Photos { • What’s the right key? photo_id: ???, data: BinData(…) – Auto Increment? } – MD5( data ) – Now() + MD5(data) – Month() + MD5(data)