2011 mongo FR - scaling with mongodb


Published on

For applications that outgrow the resources of a single database server, MongoDB can convert to a sharded cluster, automatically managing failover and balancing of nodes, with few or no changes to the original application code. This talk starts by discussing when to shard and continues on to describe MongoDB's sharding architecture. We'll describe how to configure a shard cluster and provide several example topologies. We'll also give some advice on schema design for sharding and how to pick the best shard key.

  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • What is scaling? Well - hopefully for everyone here.
  • ec2 goes up to 64gb, maybe mention 256gb box here??? ($30-40k) maybe can but 256gb box, but i spin up 10 ec2 64gb boxes in 10 minutes
  • Don’t pre-emptively shard - easy to add later
  • 2011 mongo FR - scaling with mongodb

    1. 1. Eliot Horowitz @eliothorowitz MongoUK March 21, 2011 Scaling with MongoDB
    2. 2. Scaling <ul><li>Storage needs only go up </li></ul><ul><li>Operations/sec only go up </li></ul><ul><li>Complexity only goes up </li></ul>
    3. 3. Horizontal Scaling <ul><li>Vertical scaling is limited </li></ul><ul><li>Hard to scale vertically in the cloud </li></ul><ul><li>Can scale wider than higher </li></ul>
    4. 4. Read Scaling <ul><li>One master at any time </li></ul><ul><li>Programmer determines if read hits master or a slave </li></ul><ul><li>Pro: easy to setup, can scale reads very well </li></ul><ul><li>Con: reads are inconsistent on a slave </li></ul><ul><li>Writes don’t scale </li></ul>
    5. 5. One Master, Many Slaves <ul><li>Custom Master/Slave setup </li></ul><ul><li>Have as many slaves as you want </li></ul><ul><li>Can put them local to application servers </li></ul><ul><li>Good for 90+% read heavy applications (Wikipedia) </li></ul>
    6. 6. Replica Sets <ul><li>High Availability Cluster </li></ul><ul><li>One master at any time, up to 6 slaves </li></ul><ul><li>A slave automatically promoted to master if failure </li></ul><ul><li>Drivers support auto routing of reads to slaves if programmer allows </li></ul><ul><li>Good for applications that need high write availability but mostly reads (Commenting System) </li></ul>
    7. 7. <ul><li>Many masters, even more slaves </li></ul><ul><li>Can scale in two dimensions </li></ul><ul><li>Add Shards for write and data size scaling </li></ul><ul><li>Add slaves for inconsistent read scaling and redundancy </li></ul>Sharding
    8. 8. Sharding Basics <ul><li>Data is split up into chunks </li></ul><ul><li>Shard: Replica sets that hold a portion of the data </li></ul><ul><li>Config Servers: Store meta data about system </li></ul><ul><li>Mongos: Routers, direct direct and merge requests </li></ul>
    9. 9. Architecture client mongos ... mongos mongod mongodddd ... Shards mongod mongod mongod Config Servers mongod mongod mongodddd mongod mongod mongodddd mongod client client client
    10. 10. Common Setup <ul><li>A common setup is 3 shards with 3 servers per shard: 3 masters, 6 slaves </li></ul><ul><li>Can add sharding later to an existing replica set with no down time </li></ul><ul><li>Can have sharded and non-sharded collections </li></ul>
    11. 11. Range Based <ul><li>collection is broken into chunks by range </li></ul><ul><li>chunks default to 64mb or 100,000 objects </li></ul>MIN MAX LOCATION A F shard1 F M shard1 M R shard2 R Z shard3
    12. 12. Config Servers <ul><li>3 of them </li></ul><ul><li>changes are made with 2 phase commit </li></ul><ul><li>if any are down, meta data goes read only </li></ul><ul><li>system is online as long as 1/3 is up </li></ul>
    13. 13. mongos <ul><li>Sharding Router </li></ul><ul><li>Acts just like a mongod to clients </li></ul><ul><li>Can have 1 or as many as you want </li></ul><ul><li>Can run on appserver so no extra network traffic </li></ul><ul><li>Cache meta data from config servers </li></ul>
    14. 14. Writes <ul><li>Inserts : require shard key, routed </li></ul><ul><li>Removes: routed and/or scattered </li></ul><ul><li>Updates: routed or scattered </li></ul>
    15. 15. Queries <ul><li>By shard key: routed </li></ul><ul><li>sorted by shard key: routed in order </li></ul><ul><li>by non shard key: scatter gather </li></ul><ul><li>sorted by non shard key: distributed merge sort </li></ul>
    16. 16. Splitting <ul><li>Take a chunk and split it in 2 </li></ul><ul><li>Splits on the median value </li></ul><ul><li>Splits only change meta data, no data change </li></ul>
    17. 17. Splitting T1 T2 T3 MIN MAX LOCATION A Z shard1 MIN MAX LOCATION A G shard1 G Z shard1 MIN MAX LOCATION A D shard1 D G shard1 G S shard1 S Z shard1
    18. 18. Balancing <ul><li>Moves chunks from one shard to another </li></ul><ul><li>Done online while system is running </li></ul><ul><li>Balancing runs in the background </li></ul>
    19. 19. Migrating T3 T4 T5 MIN MAX LOCATION A D shard1 D G shard1 G S shard1 S Z shard1 MIN MAX LOCATION A D shard1 D G shard1 G S shard1 S Z shard2 MIN MAX LOCATION A D shard1 D G shard1 G S shard2 S Z shard2
    20. 20. Choosing a Shard Key <ul><li>Shard key determines how data is partitioned </li></ul><ul><li>Hard to change </li></ul><ul><li>Most important performance decision </li></ul>
    21. 21. Use Case: User Profiles <ul><li>{ email : “ [email_address] ” , </li></ul><ul><li>addresses : [ { state : “NY” } ] </li></ul><ul><li>} </li></ul><ul><li>Shard by email </li></ul><ul><li>Lookup by email hits 1 node </li></ul><ul><li>Index on { “addresses.state” : 1 } </li></ul>
    22. 22. Use Case: Activity Stream <ul><li>{ user_id : XXX, event_id : YYY , data : ZZZ } </li></ul><ul><li>Shard by user_id </li></ul><ul><li>Looking up an activity stream hits 1 node </li></ul><ul><li>Writing even is distributed </li></ul><ul><li>Index on { “event_id” : 1 } for deletes </li></ul>
    23. 23. Use Case: Photos <ul><li>{ photo_id : ???? , data : <binary> } </li></ul><ul><li>What’s the right key? </li></ul><ul><li>auto increment </li></ul><ul><li>MD5( data ) </li></ul><ul><li>now() + MD5(data) </li></ul><ul><li>month() + MD5(data) </li></ul>
    24. 24. Use Case: Logging <ul><li>{ machine : “app.foo.com” , app : “apache” , </li></ul><ul><li>when : “2010-12-02:11:33:14” , data : XXX } </li></ul><ul><li>Possible Shard keys </li></ul><ul><li>{ machine : 1 } </li></ul><ul><li>{ when : 1 } </li></ul><ul><li>{ machine : 1 , app : 1 } </li></ul><ul><li>{ app : 1 } </li></ul>
    25. 25. Download MongoDB http://www.mongodb.org and let us know what you think @eliothorowitz @mongodb 10gen is hiring! http://www.10gen.com/jobs