0
Sharding with MongoDB          Tyler Brock          @TylerBrock
Philosophy ConceptsArchitecture Mechanics
Philosophy
Philosophy
Philosophy      “Ruby is designed          to make        programmers           happy.”
Philosophy             MongoDB is a             database for             developers.
Philosophy             Build
Philosophy             Build             Scale
Philosophy          How does        MongoDB scale?
Philosophy> db.runCommand({enablesharding: "<dbname>" })> db.runCommand({   shardcollection: "<namespace>",   key: <shardk...
Concepts
Simple Web Application           app                 Read/Write         datastore
What happens whenyour working setexceeds memory?
What happens ifyour write loadis enormous?
Vertical Scaling        app      datastore
Vertical Scaling        app      datastore
Vertical Scalingapp           app            app            datastore   68 GB Ram                        Raid10 EBS
Vertical Scalingapp           app            app            datastore                        512 GB Ram                   ...
Horizontal Scaling
Horizontal Scaling         app       datastore       60gb
Horizontal Scaling              appdatastore   datastore   datastore20gb        20gb        20gb
Horizontal Scaling                            app                       Routing Logicmetadata           datastore      dat...
Horizontal Scaling                            app                       Routing Logicmetadata           datastore      dat...
Horizontal Scaling                            app                       Routing Logic                         Balancermeta...
Horizontal Scaling                            app                       Routing Logic                         Balancermeta...
Architecture
Shard         Really is just a mongod (or replica set)mongod         Where your data lives
Config Server         Mongod started with --configsvr optionconfig    Must have 3 (or 1 in development)         Data is commi...
mongos         Acts just like shard router / proxy         One or as many as you wantmongos         Light weight -- can ru...
Routing Logic                         Balancingmetadata           datastore      datastore    datastore
mongosmetadata           datastore    datastore   datastore
app                       mongosmetadata           datastore    datastore   datastore
app                    mongosconfig        datastore    datastore   datastore
app                    mongosconfigconfigconfig        datastore    datastore   datastore
app                 mongosconfigconfigconfig        mongod    mongod   mongod
app                 mongosconfigconfigconfig        RS         RS       RS        mongod    mongod   mongod        mongod    ...
app                 mongosconfigconfigconfig        RS         RS       RS        mongod    mongod   mongod        mongod    ...
Configuration
Bring up mongods or Replica Sets            mongod --shardsvr            mongod --replSet --shardsvr             RS       ...
Bring up Config Servers                  mongod --configsvr    config    config    config             RS           RS          ...
Bring up Mongos            mongos --configdb <list of configdb uris>                          mongos    config    config    co...
Connect to Mongos+ Add Shards     > use admin     > db.runCommand({"addShard": <shard uri>})+Enable Sharding    > db.runCo...
Mechanics
How does MongoDBbalance my data?
Keys            test.users{     name: “Joe”,     email: “Joe@fake.com”,},{     name: “Bob”,     email: “bob@fake.com”,},{ ...
Keys                                                 test.users                                       {                   ...
Keys                                              test.users                                    {                         ...
Keys                                     test.users                           {                                name: “Joe”...
Chunks-∞            +∞
Chunks-∞                                            +∞     joe@fake.com            tyler@fake.com              moe@fake.com
Chunks                    Split!-∞                                            +∞     joe@fake.com            tyler@fake.co...
Chunks     This is a              Split!        This is a      chunk                                chunk-∞               ...
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          ...
Splitting                                       config                mongos                 config                         ...
Splitting                                       config                mongos                 config                         ...
Splitting                                       config                mongos                 config                         ...
Splitting                                       config                mongos                 config                         ...
Splitting           Split  this big chunk into 2                           config         chunks                           ...
Splitting                                       config                mongos                 config                         ...
Splitting                                                config                         mongos                 config       ...
Balancing                                       config                mongos                 config                         ...
Balancing      Shard1,   move a chunk to                            config      Shard2                       mongos        ...
Balancing                                       config                mongos                 config                         ...
Balancing      Shard1, move another chunk                            config     to Shard3                        mongos    ...
Balancing                                       config                mongos                 config                         ...
Balancing      Shard1, move another chunk                            config     to Shard4                        mongos    ...
Balancing                                       config                mongos                 config                         ...
Balancing                                       config                mongos                 config                         ...
How does MongoDBroute my queries?
Routed Request         mongosshard     shard   shard
Routed Request          1                          1. Query arrives at Mongos         mongosshard     shard   shard
Routed Request             1                             1. Query arrives at Mongos                             2. Mongos ...
Routed Request                 1                                 1. Query arrives at Mongos                               ...
Routed Request                 1                                 1. Query arrives at Mongos                     4         ...
Scatter Gather Request        mongosshard   shard    shard
Scatter Gather Request         1                         1. Query arrives at Mongos        mongosshard   shard    shard
Scatter Gather Request             1                                 1. Query arrives at Mongos                           ...
Scatter Gather Request             1                                 1. Query arrives at Mongos                           ...
Scatter Gather Request             1                                     1. Query arrives at Mongos                     4 ...
Distributed Merge Sort Req.         mongos shard   shard    shard
Distributed Merge Sort Req.          1                          1. Query arrives at Mongos         mongos shard   shard   ...
Distributed Merge Sort Req.              1                                  1. Query arrives at Mongos                    ...
Distributed Merge Sort Req.              1                                      1. Query arrives at Mongos                ...
Distributed Merge Sort Req.              1                                      1. Query arrives at Mongos                ...
Distributed Merge Sort Req.              1                                          1. Query arrives at Mongos            ...
Distributed Merge Sort Req.              1                                          1. Query arrives at Mongos            ...
QueriesBy Shard Key    Routed            db.users.find({email: “bob@10gen.com”})Sorted by       Routed in order   db.users....
WritesInserts   Requires shard key   db.users.insert({                                name: “Bob”,                        ...
How do I choose myshard key?
Rule of Thumb      Choose a field that      is common to your      queries.
Cardinality        Chunks should be        able to split.
Chunks should be able to split  {      node:           "ny153.example.com",      application:    "apache",      time:     ...
Chunks should be able to split  {      node:           "ny153.example.com",      application:    "apache",      time:     ...
Write Scaling       Writes should be       distributed.
Writes should be distributed  {      node:           "ny153.example.com",      application:    "apache",      time:       ...
Writes should be distributed  {      node:           "ny153.example.com",      application:    "apache",      time:       ...
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:...
Queries should be routed to one shard  {      node:          "ny153.example.com",      application:   "apache",      time:...
> db.runCommand({enablesharding: "<dbname>" })> db.runCommand({   shardcollection: "<namespace>",   key: <shardkeypatterno...
Thanks!
Extra Slides
Config Servers
Config Servers   mongod
Config Servers   mongod    mongod    mongod
mongoDB Scaling - Single Noderead       node_a1                          write
Read scaling - add Replicasread       node_b1       node_a1                              write
Read scaling - add Replicasread       node_c1       node_b1       node_a1                              write
Write scaling - Shardingread       shard1       node_c1       node_b1       node_a1                           write
Write scaling - add shardsread       shard1    shard2       node_c1   node_c2       node_b1   node_b2       node_a1   node...
Write scaling - add shardsread       shard1    shard2    shard3       node_c1   node_c2   node_c3       node_b1   node_b2 ...
Upcoming SlideShare
Loading in...5
×

Sharding with MongoDB -- MongoDC 2012

1,660

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,660
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
70
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Sharding with MongoDB -- MongoDC 2012"

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

    Clipping is a handy way to collect important slides you want to go back to later.

×