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

laurenceputra

July 11, 2012
Tweet

More Decks by laurenceputra

Other Decks in Technology

Transcript

  1. PHP + MongoDB
    by @laurenceputra
    for Singapore PHP User Group

    View full-size slide

  2. Instasyncer
    HA Database, with automatic fallover

    View full-size slide

  3. Geekcamp.SG
    HA Database, with automatic fallover

    View full-size slide

  4. Including freelance work, and personal projects
    Other random projects

    View full-size slide

  5. HA for WHAT?
    Doesn’t it cost more?

    View full-size slide

  6. What if AWS goes down?

    View full-size slide

  7. If you want web-scale db’s

    View full-size slide

  8. 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

    View full-size slide

  9. So why should YOU use
    MongoDB?

    View full-size slide

  10. It’s AWESOME
    It’s FLEXIBLE
    It’s FAST

    View full-size slide

  11. 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
    ...

    View full-size slide

  12. ...
    Add in the fact that you can add in new
    fields any time.
    Fast.
    ...

    View full-size slide

  13. ...
    and backup your entire database without
    facing performance issues

    View full-size slide

  14. 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."

    View full-size slide

  15. Plus, does this look familiar?
    $mysqli->query("INSERT blah blah blah");
    $mysqli->query("UPDATE blah blah blah");

    View full-size slide

  16. So... Are you convinced?

    View full-size slide

  17. MongoDB
    mongodb.org

    View full-size slide

  18. 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, ...)

    View full-size slide

  19. And to use it you simply need to type
    /path/to/mongo  

    View full-size slide

  20. 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

    View full-size slide

  21. PHP Drivers
    (sudo)  pecl  install  mongo
    Plus add in extension=mongo.so in
    php.ini

    View full-size slide

  22. That’s it. Your PHP Webapp can now use
    MongoDB
    Setup done.

    View full-size slide

  23. That wasn’t that long right?

    View full-size slide

  24. It gets better.

    View full-size slide

  25. Basic syntax
    $conn  =  new  Mongo();
    $db  =  $conn-­‐>php_meetup;
    $collection  =  $db-­‐>members;

    View full-size slide

  26. $member  =  array(
    'name'  =>  'Laurence',  
    'age'  =>  22
    );
    $settings  =  array('safe'  =>  
    MONGO_SAFE_LEVEL);
    $collection-­‐>insert($member,  $settings);

    View full-size slide

  27. 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.

    View full-size slide

  28. $query  =  array('name'  =>  'Laurence');
    $update  =  array(
    '$set'  =>  array(
    'twitter'  =>  '@laurenceputra'
    ));
    $settings  =  array('safe'  =>  MONGO_SAFE_LEVEL);
    $collection-­‐>update($query,  $update,  
    $settings);
    With MongoDB

    View full-size slide

  29. Now to Scale!

    View full-size slide

  30. Architecture of a production system
    mongod -configsvr

    View full-size slide

  31. In MongoDB, sharding is the tool for scaling a
    system, and replication is the tool for data safety,
    high availability, and disaster recovery.

    View full-size slide

  32. What is sharding?

    View full-size slide

  33. Splitting of data into logical Chunks based on a
    predefined Shard Key.
    Max 64 MB per chunk
    Choose your shard key VERY carefully

    View full-size slide

  34. 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

    View full-size slide

  35. 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

    View full-size slide

  36. Want to know more?

    View full-size slide

  37. Replica Sets?

    View full-size slide

  38. Simply creates multiple copies of the data on multiple
    servers/nodes, such that if any server/node goes down,
    the database is still up.

    View full-size slide

  39. Of course, if 2012 happens, you’re out of luck ;)

    View full-size slide

  40. mongod  -­‐-­‐port  27017  -­‐-­‐dbpath  $HOME/mongodb/
    data/  -­‐-­‐logpath  $HOME/mongodb/logs/
    mongodb.log  -­‐-­‐logappend  -­‐-­‐fork  -­‐-­‐replSet  
    demo1  -­‐-­‐oplogSize  50  -­‐-­‐keyFile  $HOME/
    mongokey

    View full-size slide

  41. rs.initiate()
    rs.add('mongodb2.cloudapp.net:27017')
    rs.add('mongodb3.cloudapp.net:27017')
    var  c  =  rs.config()
    c.members[0].priority  =  7
    rs.reconfig(c)

    View full-size slide

  42. db.addUser('admin','demopassword')
    db.auth('admin','demopassword')

    View full-size slide

  43. mongo -u admin -p demopassword --host
    YOUR_URL_HERE --port 27017 admin

    View full-size slide

  44. Demo Servers provided by
    They actually have customer support

    View full-size slide

  45. Architecture of a production system
    (recap)
    mongod -configsvr

    View full-size slide

  46. 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

    View full-size slide

  47. 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

    View full-size slide

  48. 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

    View full-size slide

  49. Automated tasks
    0  0  *  *  *  killall  -­‐SIGUSR1  mongod

    View full-size slide

  50. Automated tasks
    0  1  1  *  *  rm  $HOME/webapps/mongodb/logs/
    mongodb.log.*

    View full-size slide

  51. limitations of MongoDB
    No full text search
    More than 100 shards untested
    No random sorting

    View full-size slide

  52. GeekcampSG - 18th August
    Website - Geekcamp.SG

    View full-size slide

  53. The End
    @laurenceputra
    geeksphere.net

    View full-size slide