Upgrade to Pro — share decks privately, control downloads, hide ads and more …

MongoDB for PHP User Group SG

MongoDB for PHP User Group SG

An introduction to MongoDB, with emphasis on HA


July 11, 2012

More Decks by laurenceputra

Other Decks in Technology


  1. Go and use /dev/null It has the fastest write speeds,

    and is replicated on ALL unix based systems all over the world. Instant replication :D
  2. And it’s really easy to do all those hardcore DevOps

    stuff that you’ve been hearing about. Stuff like High Availability, Sharding, and Backups ...
  3. ... Add in the fact that you can add in

    new fields any time. Fast. ...
  4. Unsafe writes? "We try to make it clear in the

    documentation, but some people never notice that there’s a “safe” option for writes (that defaults to false), and then get very pissed when something wasn’t written."
  5. Installing and using MongoDB is as simple as downloading the

    file and typing /path/to/mongod in your terminal (starts the mongo server) Of course there's a whole lot of other ways to do it (RPM, Brew, ...)
  6. And MongoDB is now set up on your server. Tons

    of awesome documentation over at http://www.mongodb.org/display/DOCS/ Home *they have some of the best documentation I’ve ever seen
  7. $member  =  array( 'name'  =>  'Laurence',   'age'  =>  22

    ); $settings  =  array('safe'  =>   MONGO_SAFE_LEVEL); $collection-­‐>insert($member,  $settings);
  8. Oops, I forgot a field! And I now have a

    billion members, which will make "Alter   TABLE" on mySQL take forever to run, making my webapp super laggy for the next hour or so.
  9. $query  =  array('name'  =>  'Laurence'); $update  =  array( '$set'  =>

     array( 'twitter'  =>  '@laurenceputra' )); $settings  =  array('safe'  =>  MONGO_SAFE_LEVEL); $collection-­‐>update($query,  $update,   $settings); With MongoDB
  10. In MongoDB, sharding is the tool for scaling a system,

    and replication is the tool for data safety, high availability, and disaster recovery.
  11. Splitting of data into logical Chunks based on a predefined

    Shard Key. Max 64 MB per chunk Choose your shard key VERY carefully
  12. Machine 1 Machine 2 Machine 3 Alabama → Arizona Colorado

    → Florida Arkansas → California Indiana → Kansas Idaho → Illinois Georgia → Hawaii Maryland → Michigan Kentucky → Maine Minnesota → Missouri Montana → Montana Nebraska → New Jersey Ohio → Pennsylvania New Mexico → North Dakota Rhode Island → South Dakota Tennessee → Utah Vermont → West Virgina Wisconsin → Wyoming
  13. mongos : to determine which mongod to send request to

    mongod --configsvr : stores information on what each shard contains (consumes little resources) mongod : a shard (normally a part of a replica set) containing the data
  14. Simply creates multiple copies of the data on multiple servers/nodes,

    such that if any server/node goes down, the database is still up.
  15. mongod  -­‐-­‐port  27017  -­‐-­‐dbpath  $HOME/mongodb/ data/  -­‐-­‐logpath  $HOME/mongodb/logs/ mongodb.log  -­‐-­‐logappend

     -­‐-­‐fork  -­‐-­‐replSet   demo1  -­‐-­‐oplogSize  50  -­‐-­‐keyFile  $HOME/ mongokey
  16. The whole stack (mine) Request App server mongod App server

    mongod Server Server Server specs Space: 100GB RAM: 256MB Bandwidth: 600GB RAM quota excludes OS/ Apache/mySQL/PgSQL Sign up here: http://bit.ly/Nz252v mongod
  17. Automated tasks 0  0  *  *  *  $HOME/webapps/mongodb/mongo/ mongodump  -­‐u

     backup  -­‐p  MYPASSWORD!  -­‐-­‐oplog   -­‐-­‐port  19904  -­‐o  $HOME/dump;  git  -­‐-­‐git-­‐dir= $HOME/dump/.git  -­‐-­‐work-­‐tree=$HOME/dump  add   -­‐A;  git  -­‐-­‐git-­‐dir=$HOME/dump/.git  -­‐-­‐work-­‐ tree=$HOME/dump  commit  -­‐m  "updated  backup";   git  -­‐-­‐git-­‐dir=$HOME/dump/.git  -­‐-­‐work-­‐tree= $HOME/dump  push
  18. Automated tasks 5,15,25,35,45,55  *  *  *  *  $HOME/webapps/ mongodb/mongo/mongod  -­‐-­‐port

     19904  -­‐-­‐dbpath   $HOME/webapps/mongodb/data/  -­‐-­‐logpath   $HOME/webapps/mongodb/logs/mongodb.log  -­‐-­‐ logappend  -­‐-­‐fork  -­‐-­‐replSet  laurenceputra1   -­‐-­‐oplogSize  50  -­‐-­‐keyFile  $HOME/mongokey