0
MongoDB, PHP & the Cloud
My name isSteve Francia     @spf13
• 15+ years building the  internet• Father, husband,  skateboarder• Chief Solutions Architect @  10gen• Author of upcoming...
Introduction to   MongoDB
Why MongoDB?
MongoDB Goals• Open source• Designed for today • Today’s hardware / environments • Today’s challenges• Great developer exp...
• Company behind MongoDB • AGPL license, own copyrights, engineering    team  • support, consulting, commercial license   ...
A bit of history
1974The relational database is created
1979
1979   1982-1996
1979   1982-1996   1995
Computers in 1995•Pentium 100 mhz•10base T•16 MB ram•200 MB HD
Cloud in 1995(Windows 95 cloud wallpaper)
Cell Phones in 2011•Dual core 1.5 Ghz•WiFi 802.11n (300+ Mbps)•1 GB ram•64GB Solid State
How about a DBdesigned for today?
MongoDB philosophy• Keep functionality when we can (key/  value stores are great, but we need more)• Non-relational (no jo...
MongoDB is:          Application      Document                           Oriented                           { author: “ste...
Under the hood•Written in C++•Runs on nearly anything•Data serialized to BSON•Extensive use of memory-mapped files
Database Landscape
This has led    some to say“MongoDB has the bestfeatures of key/ valuesstores, document databasesand relational databases ...
Use Cases
CMS / BlogNeeds:• Business needed modern data store for rapid development and  scaleSolution:• Use PHP & MongoDBResults:• ...
Photo Meta-DataProblem:• Business needed more flexibility than Oracle could deliverSolution:• Use MongoDB instead of Oracle...
Customer AnalyticsProblem:• Deal with massive data volume across all customer sitesSolution:• Use MongoDB to replace Googl...
Online DictionaryProblem:• MySQL could not scale to handle their 5B+ documentsSolution:• Switched from MySQL to MongoDBRes...
E-commerceProblem:• Multi-vertical E-commerce impossible to model (efficiently) in  RDBMSSolution:• Switched from MySQL to ...
Tons morePretty much if you can use a RDMBS or Key/Value             MongoDB is a great fit
In Good Company
MongoDB in PHP
Relational made normalized     data look like this
Document databases makenormalized data look like this
Terminology   RDBMS                    MongoTable, View     ➜   CollectionRow             ➜   JSON DocumentIndex          ...
Tables to Documents           {               title: ‘MongoDB’,               contributors: [                  { name: ‘El...
Documents to Arrays{                                   array(    title: ‘MongoDB’,                 "title" => MongoDB,    ...
Documents to Objects{                                   PostObject Object(    title: ‘MongoDB’,                   [title] ...
MongoDB speaks     PHP
This has led    Matt to say“The single best interface(API) of any phpextension.         Matthew Weier OPhinney
PHP DriverInstalling the driver $ pecl install mongoAdd to php.ini extension = mongo.so
Connecting to the DB$connection = new Mongo();$db = $connection->selectDB(blog);$posts = $db->post;If the DB or collection...
DocumentsBlog Post Document$p1 = array("author"   =>   "roger",            "date"     =>   new MongoDate(),            "te...
Queryingprint_r($posts->findOne());Array(    [_id] => MongoId Object(            [$id] => 4e9796764a18173a17000000        ...
Querying> db.posts.findOne()>   { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"),    author : "roger",      date : "Sat Jul 24...
Inserting Objectsclass PostObj {    public $comments = array();    public function __construct($author, $title, $content){...
MongoIdAn autogenerated primary key$x = array("foo" => 1, "bar" => 2);$db->baz->save($x);var_dump($x[_id]);Result:object(M...
MongoIdAn autogenerated primary keyobject(MongoId)#4 (1) {  ["$id"]=> string(24) "4e9cc76a4a1817fd21000000"}              ...
Update Operations $set, $unset, $inc, $push, $pushAll, $pull, $pullAll, $bit$change = array($push =>            array(comm...
Nested Documents    {   _id : ObjectId("4c4ba5c0672c685e5e8aabf3"),        author : "roger",        date : "Sat Apr 24 201...
Nested Documents    {   _id : ObjectId("4c4ba5c0672c685e5e8aabf3"),        author : "roger",        date : "Sat Apr 24 201...
Nested DocumentsArray(    [_id] => MongoId Object (            [$id] => 4e9796764a18173a17000000    )    [author] => roger...
Querying$posts = $blog->find(array(     "author" => "Roger"));$commentsByFred = $blog->find(array(     "comments.author" =...
It’s all about the $ $ instead of >, <, =, etc. $gt, $gte, $lt, $lte, $eq, $neq, $exists, $set, $mod, $where, $in, $nin, $...
Adjust the behavior           in php.ini or using ini_set()php.ini---------------mongo.cmd = ":"$c->find(array("x" => arra...
Indexing including secondary and compound indexes$people->ensureIndex(array("age" => 1));$people->ensureIndex(array(    "n...
Cursors$cursor = $c->find(array("foo" => "bar"))foreach ($cursor as $id => $value) {    echo "$id: ";    var_dump( $value ...
Paging$page_num = 3;$results_per_page = 10;$cursor = $results->find()    ->sort(array("ts" => -1))    ->skip($page_num * $...
Grid FS
Storing FilesUnder 16mb
Storing Big Files>16mb stored in 16mb chunks
Storing Big FilesWorks with replicatedand sharded systems
A better network FS• GridFS files are seamlessly sharded & replicated.• No OS constraints... • No file size limits • No nami...
MongoDB for the Cloud
Cloud vs Metal•Commodity          •Customizable•Horizontal scale   •Scale up or out•RAM + CPU          •Disk, RAM +       ...
IaaS
PaaS
MongoDBComponents
Replica SetsPrimary         Primary    PrimarySecondary      Secondary   SecondarySecondary       Arbiter    Secondary    ...
Sharding          App       App      App         Server    Server   Server         MongoS    MongoS    MongoS             ...
MongoDB on EC2
Amazon EC2Instance Types             32-bit = Don’t Use                 Typical MongoD                 ConfigD / Arbiter   ...
OS• Amazon OS now an option• Turn off atime• Raise file descriptor limits     cat >> /etc/security/limits.conf << EOF     *...
http://spf13.com                                http://github.com/spf13                                @spf13          Que...
MongoDB, PHP and the cloud - php cloud summit 2011
MongoDB, PHP and the cloud - php cloud summit 2011
MongoDB, PHP and the cloud - php cloud summit 2011
Upcoming SlideShare
Loading in...5
×

MongoDB, PHP and the cloud - php cloud summit 2011

15,699

Published on

An introduction to using MongoDB with PHP.
Walking through the basics of schema design, connecting to a DB, performing CRUD operations and queries in PHP.
MongoDB runs great in the cloud, but there are some things you should know. In this session we'll explore scaling and performance characteristics of running Mongo in the cloud as well as best practices for running on platforms like Amazon EC2.

Published in: Technology
1 Comment
13 Likes
Statistics
Notes
No Downloads
Views
Total Views
15,699
On Slideshare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
111
Comments
1
Likes
13
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Remember in 1995 there were around 10,000 websites. Mosiac, Lynx, Mozilla (pre netscape) and IE 2.0 were the only web browsers. \nApache (Dec &amp;#x2019;95), Java (&amp;#x2019;96), PHP (June &amp;#x2019;95), and .net didn&amp;#x2019;t exist yet. Linux just barely (1.0 in &amp;#x2019;94)\n
  • Remember in 1995 there were around 10,000 websites. Mosiac, Lynx, Mozilla (pre netscape) and IE 2.0 were the only web browsers. \nApache (Dec &amp;#x2019;95), Java (&amp;#x2019;96), PHP (June &amp;#x2019;95), and .net didn&amp;#x2019;t exist yet. Linux just barely (1.0 in &amp;#x2019;94)\n
  • Remember in 1995 there were around 10,000 websites. Mosiac, Lynx, Mozilla (pre netscape) and IE 2.0 were the only web browsers. \nApache (Dec &amp;#x2019;95), Java (&amp;#x2019;96), PHP (June &amp;#x2019;95), and .net didn&amp;#x2019;t exist yet. Linux just barely (1.0 in &amp;#x2019;94)\n
  • \n
  • \n
  • \n
  • \n
  • By reducing transactional semantics the db provides, one can still solve an interesting set of problems where performance is very important, and horizontal scaling then becomes easier.\n\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Transcript of "MongoDB, PHP and the cloud - php cloud summit 2011"

    1. 1. MongoDB, PHP & the Cloud
    2. 2. My name isSteve Francia @spf13
    3. 3. • 15+ years building the internet• Father, husband, skateboarder• Chief Solutions Architect @ 10gen• Author of upcoming O’Reilly publication “MongoDB and PHP”
    4. 4. Introduction to MongoDB
    5. 5. Why MongoDB?
    6. 6. MongoDB Goals• Open source• Designed for today • Today’s hardware / environments • Today’s challenges• Great developer experience• Reliable• Scalable
    7. 7. • Company behind MongoDB • AGPL license, own copyrights, engineering team • support, consulting, commercial license revenue• Management • Google/DoubleClick, Oracle, Apple, NetApp • Funding: Sequoia, Union Square, Flybridge • Offices in NYC, Redwood Shores & London • 60+ employees
    8. 8. A bit of history
    9. 9. 1974The relational database is created
    10. 10. 1979
    11. 11. 1979 1982-1996
    12. 12. 1979 1982-1996 1995
    13. 13. Computers in 1995•Pentium 100 mhz•10base T•16 MB ram•200 MB HD
    14. 14. Cloud in 1995(Windows 95 cloud wallpaper)
    15. 15. Cell Phones in 2011•Dual core 1.5 Ghz•WiFi 802.11n (300+ Mbps)•1 GB ram•64GB Solid State
    16. 16. How about a DBdesigned for today?
    17. 17. MongoDB philosophy• Keep functionality when we can (key/ value stores are great, but we need more)• Non-relational (no joins) makes scaling horizontally practical• Document data models are good• Database technology should run anywhere VMs, cloud, metal, etc
    18. 18. MongoDB is: Application Document Oriented { author: “steve”, High date: new Date(), text: “About MongoDB...”,Performance tags: [“tech”, “database”]} Fully Consistent Horizontally Scalable
    19. 19. Under the hood•Written in C++•Runs on nearly anything•Data serialized to BSON•Extensive use of memory-mapped files
    20. 20. Database Landscape
    21. 21. This has led some to say“MongoDB has the bestfeatures of key/ valuesstores, document databasesand relational databases inone. John Nunemaker
    22. 22. Use Cases
    23. 23. CMS / BlogNeeds:• Business needed modern data store for rapid development and scaleSolution:• Use PHP & MongoDBResults:• Real time statistics• All data, images, etc stored together, easy access, easy deployment, easy high availability• No need for complex migrations• Enabled very rapid development and growth
    24. 24. Photo Meta-DataProblem:• Business needed more flexibility than Oracle could deliverSolution:• Use MongoDB instead of OracleResults:• Developed application in one sprint cycle• 500% cost reduction compared to Oracle• 900% performance improvement compared to Oracle
    25. 25. Customer AnalyticsProblem:• Deal with massive data volume across all customer sitesSolution:• Use MongoDB to replace Google Analytics / Omniture optionsResults:• Less than one week to build prototype and prove business case• Rapid deployment of new features
    26. 26. Online DictionaryProblem:• MySQL could not scale to handle their 5B+ documentsSolution:• Switched from MySQL to MongoDBResults:• Massive simplification of code base• Eliminated need for external caching system• 20x performance improvement over MySQL
    27. 27. E-commerceProblem:• Multi-vertical E-commerce impossible to model (efficiently) in RDBMSSolution:• Switched from MySQL to MongoDBResults:• Massive simplification of code base• Rapidly build, halving time to market (and cost)• Eliminated need for external caching system• 50x+ improvement over MySQL
    28. 28. Tons morePretty much if you can use a RDMBS or Key/Value MongoDB is a great fit
    29. 29. In Good Company
    30. 30. MongoDB in PHP
    31. 31. Relational made normalized data look like this
    32. 32. Document databases makenormalized data look like this
    33. 33. Terminology RDBMS MongoTable, View ➜ CollectionRow ➜ JSON DocumentIndex ➜ IndexJoin ➜ Embedded DocumentPartition ➜ ShardPartition Key ➜ Shard Key
    34. 34. Tables to Documents { title: ‘MongoDB’, contributors: [ { name: ‘Eliot Horowitz’, email: ‘eh@10gen.com’ }, { name: ‘Dwight Merriman’, email: ‘dm@10gen.com’ } ], model: { relational: false, awesome: true } }
    35. 35. Documents to Arrays{ array( title: ‘MongoDB’, "title" => MongoDB, "contributors" => array( contributors: [ array( { name: ‘Eliot Horowitz’, name => Eliot Horowitz, email: ‘eh@10gen.com’ }, email => eh@10gen.com ), { name: ‘Dwight Merriman’, array( email: ‘dm@10gen.com’ } name => Dwight Merriman, email => dm@10gen.com ], ) model: { ), relational: false, "model" => array( relational => false, awesome: true awesome => true } )} )
    36. 36. Documents to Objects{ PostObject Object( title: ‘MongoDB’, [title] => MongoDB [contributors] => Array( contributors: [ [0] => PersonObject Object( { name: ‘Eliot Horowitz’, [name] => Eliot Horowitz email: ‘eh@10gen.com’ }, [email] => eh@10gen.com ) { name: ‘Dwight Merriman’, [1] => PersonObject Object( email: ‘dm@10gen.com’ } [name] => Dwight Merriman [email] => dm@10gen.com ], ) model: { ) relational: false, [model] => ModelObject Object( [relational] => awesome: true [awesome] => 1 } )} )
    37. 37. MongoDB speaks PHP
    38. 38. This has led Matt to say“The single best interface(API) of any phpextension.   Matthew Weier OPhinney
    39. 39. PHP DriverInstalling the driver $ pecl install mongoAdd to php.ini extension = mongo.so
    40. 40. Connecting to the DB$connection = new Mongo();$db = $connection->selectDB(blog);$posts = $db->post;If the DB or collection doesn’t exist,mongoDB will create it on connect.
    41. 41. DocumentsBlog Post Document$p1 = array("author" => "roger", "date" => new MongoDate(), "text" => "about mongoDB...", "tags" => array(tech, databases) );$posts->save($p1);
    42. 42. Queryingprint_r($posts->findOne());Array( [_id] => MongoId Object( [$id] => 4e9796764a18173a17000000 ) [author] => roger [date] => MongoDate Object( [sec] => 1318557302 [usec] => 581000 ) [text] => about mongoDB... [tags] => Array( [0] => tech Note: _id is unique, but can be anything [1] => databasesyou’d like) )
    43. 43. Querying> db.posts.findOne()> { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), author : "roger", date : "Sat Jul 24 2010 19:47:11", text : "About MongoDB...", tags : [ "tech", "databases" ] } Note: _id is unique, but can be anythingyou’d like
    44. 44. Inserting Objectsclass PostObj { public $comments = array(); public function __construct($author, $title, $content){ $this->author = $author; $this->title = $title; $this->content = $content; }}$posts->insert(new PostObj("Steve", "My first blog post", "Hello, World!"));
    45. 45. MongoIdAn autogenerated primary key$x = array("foo" => 1, "bar" => 2);$db->baz->save($x);var_dump($x[_id]);Result:object(MongoId)#4 (1) { ["$id"]=> string(24) "4e9cc76a4a1817fd21000000"}
    46. 46. MongoIdAn autogenerated primary keyobject(MongoId)#4 (1) { ["$id"]=> string(24) "4e9cc76a4a1817fd21000000"} 4e9cc76a4a1817fd21000000 |------||----||--||----| ts mac pid inc
    47. 47. Update Operations $set, $unset, $inc, $push, $pushAll, $pull, $pullAll, $bit$change = array($push => array(comments => array( author => Fred, created => new MongoDate(), comment => Best post ever!. ) ) );$posts->update(array("_id" => $id), $change);
    48. 48. Nested Documents { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), author : "roger", date : "Sat Apr 24 2011 19:47:11", text : "About MongoDB...", tags : [ "tech", "databases" ], comments : [ { author : "Fred", date : "Sat Apr 25 2010 20:51:03 GMT-0700", text : "Best Post Ever!" } ]}
    49. 49. Nested Documents { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), author : "roger", date : "Sat Apr 24 2011 19:47:11", text : "About MongoDB...", tags : [ "tech", "databases" ], comments : [ { author : "Fred", date : "Sat Apr 25 2010 20:51:03 GMT-0700", text : "Best Post Ever!" } ]}
    50. 50. Nested DocumentsArray( [_id] => MongoId Object ( [$id] => 4e9796764a18173a17000000 ) [author] => roger [comments] => Array( [0] => Array( [author] => Fred [created] => MongoDate Object( [sec] => 1318899392 [usec] => 176000 ) [comment] => Dumb post. ) ) [date] => MongoDate Object( [sec] => 1318557302 [usec] => 581000 ) [tags] => Array( [0] => tech [1] => databases ) [text] => about mongoDB...)
    51. 51. Querying$posts = $blog->find(array( "author" => "Roger"));$commentsByFred = $blog->find(array( "comments.author" => "Fred"));$commentsByFred = $blog->find(array( "comments.author" => new MongoRegex("/fred/i")));
    52. 52. It’s all about the $ $ instead of >, <, =, etc. $gt, $gte, $lt, $lte, $eq, $neq, $exists, $set, $mod, $where, $in, $nin, $inc $push, $pull, $pop, $pushAll, $popAll$c->find(array("x" => array($gt => 4)));$c->find(array("x" => array(“$gt” => 4)));
    53. 53. Adjust the behavior in php.ini or using ini_set()php.ini---------------mongo.cmd = ":"$c->find(array("x" => array(:gt => 4))); orini_set("mongo.cmd", ".");$c->find(array("x" => array(.gt => 4)));
    54. 54. Indexing including secondary and compound indexes$people->ensureIndex(array("age" => 1));$people->ensureIndex(array( "name" => -1, "ts" => -1, "comments.author" => 1));
    55. 55. Cursors$cursor = $c->find(array("foo" => "bar"))foreach ($cursor as $id => $value) { echo "$id: "; var_dump( $value );}$a = iterator_to_array($cursor);
    56. 56. Paging$page_num = 3;$results_per_page = 10;$cursor = $results->find() ->sort(array("ts" => -1)) ->skip($page_num * $results_per_page) ->limit($results_per_page);
    57. 57. Grid FS
    58. 58. Storing FilesUnder 16mb
    59. 59. Storing Big Files>16mb stored in 16mb chunks
    60. 60. Storing Big FilesWorks with replicatedand sharded systems
    61. 61. A better network FS• GridFS files are seamlessly sharded & replicated.• No OS constraints... • No file size limits • No naming constraints • No folder limits • Standard across different OSs• MongoDB automatically generate the MD5 hash of the file
    62. 62. MongoDB for the Cloud
    63. 63. Cloud vs Metal•Commodity •Customizable•Horizontal scale •Scale up or out•RAM + CPU •Disk, RAM + CPU•Multi-tenant •Dedicated
    64. 64. IaaS
    65. 65. PaaS
    66. 66. MongoDBComponents
    67. 67. Replica SetsPrimary Primary PrimarySecondary Secondary SecondarySecondary Arbiter Secondary Secondary Secondary
    68. 68. Sharding App App App Server Server Server MongoS MongoS MongoS ConfigD ConfigD ConfigDMongoD MongoD MongoD MongoDMongoD MongoD MongoD MongoDMongoD MongoD MongoD MongoD
    69. 69. MongoDB on EC2
    70. 70. Amazon EC2Instance Types 32-bit = Don’t Use Typical MongoD ConfigD / Arbiter Big MongoD 32-bit = Don’t Use High CPU not necessary
    71. 71. OS• Amazon OS now an option• Turn off atime• Raise file descriptor limits cat >> /etc/security/limits.conf << EOF * hard nofile 65536 * soft nofile 65536 EOF• DO NOT use large VM pages• Use ext4, xfs• Use RAID– RAID10 on MongoD– RAID1 on ConfigDB• Warning! Known problems with Ubuntu 10.04 & EBS• https://bugs.launchpad.net/ubuntu/+source/linux-ec2/+bug/ 614853– https://bugzilla.kernel.org/show_bug.cgi?id=16991
    72. 72. http://spf13.com http://github.com/spf13 @spf13 Questions? download at mongodb.orgPS: We’re hiring!! Contact us at jobs@10gen.com
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×