SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Ähnlich wie 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
 

Ähnlich wie 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
 

Kürzlich hochgeladen

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
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
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
 
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
 
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
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
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
 
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
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
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
 
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
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 

Kürzlich hochgeladen (20)

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
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
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
 
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
 
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.
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
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
 
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
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
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
 
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
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 

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)