17

Click here to load reader

MongoDB Sharding Fundamentals

Embed Size (px)

Citation preview

Page 1: MongoDB Sharding Fundamentals

MongoDB  Sharding  fundamentals  

Antonios  Giannopoulos    Database  Administrator  at  ObjectRocket  by  Rackspace  

Page 2: MongoDB Sharding Fundamentals

Background  

-­‐  14  years  in  databases  and  system  engineering  

-­‐  NoSQL  DBA  @  ObjectRocket  by  Rackspace    -­‐  MongoDB  CerFfied  DBA  

Page 3: MongoDB Sharding Fundamentals

What  is  sharding?  

-­‐  A  mechanism  for  horizontal  scaling    -­‐  Distributes  the  dataset  over  mulFple  servers  

(shards)    -­‐  Each  shard  is  an  independent  database    -­‐  All  shards  consists  a  single  logical  database  

Page 4: MongoDB Sharding Fundamentals

Why  Sharding?  

-­‐  Increases  cluster  throughput  –  Read/Write  Scaling  

 -­‐  Reduces  costs  -­‐  Many  small  servers  VS  one  big  

box  

-­‐  Eliminates  HW  and  SW  hard  limits  

Page 5: MongoDB Sharding Fundamentals

MongoDB  Sharding  -­‐  Consists  of  three  elements:  Shards,  Config  Servers  and  

Mongos    -­‐  Shards:  Hold  the  cluster  data,  databases,  collecFons,  

documents  (Data  nodes)  

-­‐  Config  Servers:  Hold  the  cluster  metadata,  map  the  cluster  architecture.  

-­‐   Mongos:  Serve  all  drivers  requests.  Route  each  request  to  a  shard  or  shards  (Router  nodes)  

Page 6: MongoDB Sharding Fundamentals

ApplicaFon  /  Driver  Layer    

Mongos01   Mongos02   MongosN  

ConfigSrv01  

ConfigSrv02  

ConfigSrv03  

Shard02   ShardN  

MongoDB  Sharded  Cluster  

Shard01   …  

…  

Page 7: MongoDB Sharding Fundamentals

How  Sharding  works?  

-­‐  Range  parFFoning  per  collecFon  (chunks)  

-­‐  Shard  key  to  define  chunks  (field(s))  

-­‐  Chunks  are  “metadata”  on  the  config  servers  

-­‐  Chunks  can  move,  split  and  merge  

Page 8: MongoDB Sharding Fundamentals

How  Sharding  works?  -­‐  Example  {  "name"  :  "Angelina",  "surname"  :  "Jolie",  "posiFon"  :  "Windows  Eng.",  "phone"  :  "555-­‐5555"  }  {  "name"  :  "Emma",  "surname"  :  "Stone",  "posiFon"  :  "Windows  Eng.",  "phone"  :  "555-­‐5555"  }  {  "name"  :  "Charlize",  "surname"  :  "Theron",  "posiFon"  :  "Linux  Eng.",  "phone"  :  "555-­‐5555"  }  {  "name"  :  "Olivia",  "surname"  :  "Wilde",  "posiFon"  :  "Linux  Eng.",  "phone"  :  "555-­‐5555"  }  {  "name"  :  "Jessica",  "surname"  :  "Alba",  "posiFon"  :  "Sr  Linux  Eng.",  "phone"  :  "555-­‐5555"  }  {  "name"  :  "Scarlef",  "surname"  :  "Johansson",  "posiFon"  :  "Sr  Windows  Eng.",  "phone"  :  "555-­‐5555"  }  {  "name"  :  "Megan",  "surname"  :  "Fox",  "posiFon"  :  "Networks  Eng.",  "phone"  :  "555-­‐5555"  }  {  "name"  :  "Mila",  "surname"  :  "Kunis",  "posiFon"  :  "Sr  Networks  Eng.",  "phone"  :  "555-­‐5555"  }  {  "name"  :  "Natalie",  "surname"  :  "Portman",  "posiFon"  :  "Database  Eng",  "phone"  :  "555-­‐5555"  }  {  "name"  :  "Anne",  "surname"  :  "Hathaway",  "posiFon"  :  "Sr  Database  Eng",  "phone"  :  "555-­‐5555"  }      

-­‐  CollecFon  employees  for  an  IT  company  -­‐  Shard  key  “posi-on”  

Page 9: MongoDB Sharding Fundamentals

How  Sharding  works?  -­‐  Example  {  "min"  :  {  "posiFon"  :  {  "$minKey"  :  1  }  },  "max"  :  {  "posiFon"  :  "Database  Eng"  },  "shard"  :  ”Shard01"  }    {  "min"  :  {  "posiFon"  :  "Database  Eng"  },  "max"  :  {  "posiFon"  :  "Sr  Database  Eng"  },  "shard"  :  "Shard01"  }    {  "min"  :  {  "posiFon"  :  "Sr  Database  Eng"  },  "max"  :  {  "posiFon"  :  "Windows  Eng."  },  "shard"  :  "Shard02"  }    {  "min"  :  {  "posiFon"  :  "Windows  Eng."  },  "max"  :  {  "posiFon"  :  {  "$maxKey"  :  1  }  },  "shard"  :  "Shard02"  }    

-­‐  Lower/upper  bound  and  shard  (server)        

Page 10: MongoDB Sharding Fundamentals

Choose  a  shard  key  

-­‐  High  Cardinality  

-­‐  Not  Null  values  

-­‐  Immutable  field(s)    -­‐  Not  Monotonically  increased  fields    

Page 11: MongoDB Sharding Fundamentals

Choose  a  shard  key  

-­‐  Even  read/write  distribuFon  

-­‐  Even  data  distribuFon    -­‐  Read  targeFng  

-­‐  Read  locality  

Page 12: MongoDB Sharding Fundamentals

Choose  a  shard  key  

-­‐  Hashed  shard  keys  for  randomness    -­‐  Compound  shard  keys  for  cardinality    -­‐  Unique  indexes  are  good    

-­‐  {_id:”hashed”}  scales  writes  

Page 13: MongoDB Sharding Fundamentals

LimitaFons  of  Sharding  

-­‐  Unique  indexes  –  Just  one…  

-­‐  IniFal  collecFon  size  –  Avoid  collecFons  >  256G,  hard  limit  is  a  funcFon  of  key  and  chunk  size  ,  for  64MB  chunk/512B  key  is  more  than  1TB  

 -­‐  Number  of  documents  per  chunk    (250K)    

Page 14: MongoDB Sharding Fundamentals

LimitaFons  of  Sharding  

-­‐  Shard  key  size  <  512  bytes  

-­‐  MulFkey,text,  geo  indexes  are  prohibited  

-­‐  Some  operaFons  won’t  run  (for  example  group,  db.eval(),  $isolated,  $snapshot,  geoSearch)  

Page 15: MongoDB Sharding Fundamentals

“Sharding”  –  Other  players  -­‐  ApplicaFon  level  sharding  

-­‐  Mysql  (MaxScale,  Fabric,…)    -­‐  Postgres  (pg_shard)  

-­‐  ElasFcSearch  (Document  ID  or  rouFng)  

-­‐  Cassandra  (Hash-­‐based  -­‐  Ring  topology)    

Page 16: MongoDB Sharding Fundamentals

Contact  

www.objectrocket.com  www.rackspace.co.uk/objectrocket/mongodb  [email protected]      We  are  hiring!  (DevOps,  DBAs  and  more)  hfp://objectrocket.com/careers  

Page 17: MongoDB Sharding Fundamentals

QuesFons?    

Thank  you!!!    

MongoDB  Meetup  What's  new  in  MongoDB  3.0  

 Tuesday,  November  10  ,  7:00  pm    @  Harokopio  University