Slide 1

Slide 1 text

Scaling   Sergey  Gavruk   @gavruk  

Slide 2

Slide 2 text

Scaling   • Ver2cal   • Horizontal   • By  op2miza2on   – Op2mize  your  queries,  schema,  indexes   – Tune  you  file  system   – Choose  right  disks  

Slide 3

Slide 3 text

Share  nothing  architecture   •  Michael  Stonebraker     First  implementa2on  in  1983     Google  calls  this  “Sharding”  

Slide 4

Slide 4 text

Sharding  goals   • App  doesn’t  know  about  clusters   • Cluster  should  always  be   available  for  reads  and  writes   • Cluster  should  grow  easily  

Slide 5

Slide 5 text

Sharding  features   •  Range-­‐based  data  par22oning   •  Automa2c  data  volume  distribu2on   •  Transparent  query  rou2ng  

Slide 6

Slide 6 text

[“a”,  “g”)   [“g”,  “m”)   [“m”,  “s”)   [“s”,  “z”)  

Slide 7

Slide 7 text

[“a”,  “g”)   [“g”,  “m”)   [“m”,  “s”)   [“s”,  “z”)   [“d”,  “g”)   100  GB   500  GB   100  GB   100  GB   100  GB   400  GB   200  GB   100  GB  

Slide 8

Slide 8 text

[“a”,  “g”)   [“g”,  “m”)   [“m”,  “s”)   [“s”,  “z”)  

Slide 9

Slide 9 text

[“a”,  “d”)  300     [“g”,  “k”)  300   [“m”,  “s”)   [“s”,  “z”)   400  GB   400  GB   100  GB   100  GB   [“d”,  “g”)  100   [“k”,  “m”)  100  

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Chunks   -­‐∞   +∞  

Slide 12

Slide 12 text

Chunks   -­‐∞   +∞  

Slide 13

Slide 13 text

null     Numbers   Strings   Objects   Arrays   binary  data   ObjectIds   booleans   Dates   regular  expressions   smaller   bigger  

Slide 14

Slide 14 text

Balancing   mongos   balancer   Config  server   Config  server   Config  server   Shard  1   Shard  2  

Slide 15

Slide 15 text

Balancer  goals   • keep  data  distributed   • minimize  the  amount  of  data  transfered  

Slide 16

Slide 16 text

Balancing   mongos   balancer   Config  server   Config  server   Config  server   Shard  1   Shard  2   Move  chunk  X   to  shard  2  

Slide 17

Slide 17 text

Balancing   Number  of  chunks   Migra:on  threshold   <  20   2   21-­‐80   4   80+   8  

Slide 18

Slide 18 text

Balancing  schedule   db.seangs.update({  _id  :  "balancer"  },     {     $set  :     {     ac2veWindow  :  {  start  :  "23:00",  stop  :  "6:00"  }     }     },  true  )    

Slide 19

Slide 19 text

Routed  Request   mongos   Shard  1   Shard  2   Shard  3  

Slide 20

Slide 20 text

mongos   Shard  1   Shard  2   Shard  3   Request  without  shard  key  

Slide 21

Slide 21 text

Without  shard  key  +  sor2ng   mongos   Shard  1   Shard  2   Shard  3  

Slide 22

Slide 22 text

Consider  the  shard  cluster  if:   •  Data  exceeds  the  storage  capacity  of  a  single  node   •  Size  of  working  set  will  soon  exceed  your  RAM   •  Large  amount  of  writes  

Slide 23

Slide 23 text

Restric2ons   •  You  cannot  update  a  shard  key   •  You  must  use  a  shard  key  for  a  single  update   •  Index  on  shard  key  

Slide 24

Slide 24 text

Ideal  shard  key   •  easily  divisible.   •  will  distribute  write  opera2ons  among  the   cluster   •  will  make  it  possible  for  the  mongos  to  return   most  query  opera2ons  directly  from  a  single   specific  mongod  instance  

Slide 25

Slide 25 text

Choosing  a  shard  key   {          _id:  "1",          user_id:  "2345652221",          date_2me:  "2012-­‐10-­‐04“,          tweet_text:  “Hello  world”   }   Reliability  

Slide 26

Slide 26 text

Choosing  a  shard  key   Ascending   {   TimeStamp:    12355232,   …   }  

Slide 27

Slide 27 text

Choosing  a  shard  key   Low-­‐cardinality  key     {   Con:nent:  “Europe”,   Name:  “Tom”,   …   }     Zip  code?  

Slide 28

Slide 28 text

Demo  

Slide 29

Slide 29 text

Any questions? mailto:  [email protected]