Sharding with MongoDB -- MongoNYC 2012

1,455 views

Published on

Published in: Technology, Business
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,455
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
20
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Sharding with MongoDB -- MongoNYC 2012

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

×