Dropping ACID with MongoDB

  • 7,539 views
Uploaded on

http://yapc2010.com/yn2010/talk/2578

http://yapc2010.com/yn2010/talk/2578

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
7,539
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
79
Comments
1
Likes
15

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • MongoDB is fast for especially k/v pair lookupFast inserts - send-and-forgetIn-place updates
  • MongoDB is fast for especially k/v pair lookupFast inserts - send-and-forgetIn-place updates
  • MongoDB is document oriented
  • Read performance

Transcript

  • 1.
  • 2. Who am I?
  • 3. what is
    ?
  • 4. Application
    Perl
    Apache
    Linux
  • 5. Application
    Perl
    Database
    Apache
    Linux
  • 6. Application
    Perl
    Apache
    Windows
  • 7. Application
    Perl
    Apache
    Linux
  • 8. Application
    Perl
    Apache
    Linux
  • 9. Application
    Perl
    Apache
    Linux
  • 10. Application
    The world's most
    popular open source
    database
    Perl
    Apache
    Linux
  • 11. Application
    Perl
    Apache
    Linux
    The world's most
    advanced open source
    database
  • 12.
  • 13. databases
    contain tables
    contain rows
  • 14. schema
    databases
    contain tables
    contain rows
  • 15. schema
    databases
    contain tables
    contain rows
    joins
  • 16. schema
    databases
    contain tables
    contain rows
    joins
  • 17.
  • 18.
  • 19.
  • 20.
  • 21. Application
    Perl
    Apache
    Linux
  • 22. getting started
  • 23. www.mongodb.org
  • 24. cpan -i MongoDB
  • 25. $conn = MongoDB::Connection->new;
  • 26. $conn = MongoDB::Connection->new;
    $db = $conn->foo;
  • 27. $conn = MongoDB::Connection->new;
    $db = $conn->foo;
    NO ADMINISTRATION
  • 28. $conn = MongoDB::Connection->new;
    $db = $conn->foo;
    $table = $db->bar;
  • 29. $conn = MongoDB::Connection->new;
    $db = $conn->foo;
    $table = $db->bar;
  • 30. server
  • 31. server
    databases
  • 32. server
    databases
    contain tables
  • 33. server
    databases
    contain tables
  • 34. server
    databases
    collections
    contain tables
  • 35. server
    databases
    collections
    contain tables
    contain rows
  • 36. server
    databases
    collections
    contain tables
    contain rows
  • 37. server
    databases
    collections
    contain tables
    documents
    contain rows
  • 38. server
    schema
    databases
    collections
    contain tables
    documents
    contain rows
  • 39. server
    schema
    databases
    collections
    contain tables
    documents
    contain rows
    joins
  • 40. server
    databases
    contain collections
    contain documents
  • 41. $conn = MongoDB::Connection->new;
    $db = $conn->foo;
    $collection = $db->bar;
  • 42. $conn = MongoDB::Connection->new;
    $db = $conn->foo;
    $collection = $db->bar;
    NO ADMINISTRATION
  • 43. $conn = MongoDB::Connection->new;
    $db = $conn->foo
    $collection = $db->bar;
    $doc = {"name" => "kristina",
    "contact info" => {
    "twitter" => "@kchodorow",
    "email" => "kristina@10gen.com"
    },
    "friends" => 400232,
    "pic" => $file_str,
    "member since" => DateTime->now};
  • 44. $conn = MongoDB::Connection->new;
    $db = $conn->foo
    $collection = $db->bar;
    $doc = {"name" => "kristina",
    "contact info" => {
    "twitter" => "@kchodorow",
    "email" => "kristina@10gen.com"
    },
    "friends" => 400232,
    "pic" => $file_str,
    "member since" => DateTime->now};
    $collection->insert($doc);
  • 45. mongo is type-sensitive
    and type-rich
  • 46.
  • $collection->find_one({
    "name" => "kristina"});
  • 60. $collection->find_one({
    "name" => "kristina"});
    $collection->find_one({
    "contact.twitter" => "@kchodorow"});
  • 61. $collection->find_one({
    "name" => "kristina"});
    $collection->find_one({
    "contact.twitter" => "@kchodorow"});
    $collection->find({
    "member since" => {
    '$gt' => $yesterday,
    '$lt' => $now
    }});
  • 62. $collection->find_one({
    "name" => "kristina"});
    $collection->find_one({
    "contact.twitter" => "@kchodorow"});
    $collection->find({
    "member since" => {
    '$gt' => $yesterday,
    '$lt' => $now
    }})->sort({"friends" => 1});
  • 63. $collection->find_one({
    "name" => "kristina"});
    $collection->find_one({
    "contact.twitter" => "@kchodorow"});
    $collection->find({
    "member since" => {
    '$gt' => $yesterday,
    '$lt' => $now
    }})->sort({"friends" => 1})->limit(10);
  • 64. $collection->find_one({
    "name" => "kristina"});
    $collection->find_one({
    "contact.twitter" => "@kchodorow"});
    $collection->find({
    "member since" => {
    '$gt' => $yesterday,
    '$lt' => $now
    }})->sort({"friends" => 1})->limit(10)
    ->skip(100);
  • 65. $collection->find_one({
    "name" => "kristina"});
    $collection->find_one({
    "contact.twitter" => "@kchodorow"});
    $cursor = $collection->find({
    "member since" => {
    '$gt' => $yesterday,
    '$lt' => $now
    }})->sort({"friends" => 1})->limit(10)
    ->skip(100);
  • 66. while (my $doc = $cursor->next) {
    # ...
    }
    my @results = $cursor->all;
  • 67. x < 3
    {"x" => {'$lt' => 3}}
  • 68. $gt
    $lte
    $ne
    $mod
    $exists
    $type
    ...
  • 69. $collection->update(
    {"name" => "kristina"},
  • 70. $collection->update(
    {"name" => "kristina"},
    {'$set' =>
  • 71. $collection->update(
    {"name" => "kristina"},
    {'$set' =>
    {"contact.website" =>
    "snailinaturtleneck.com"
    }});
  • 72. updates are...
    • atomic
    • 73. in-place (when possible)
    • 74. able to create and delete fields
    • 75. able to create documents
  • $collection->update(
    {"url" => "example.com"},
    {'$inc' => {"pageviews" => 1}},
    {"upsert" => 1}
    );
  • 76.
  • 77.
  • 78. $set
    $inc
    $push
    $pop
    $pull
    ...
  • 79. $set
    $inc
    $push
    $pop
    $pull
    ...
  • 80. $set
    $inc
    $push
    $pop
    $pull
    ...
  • 81. $set
    $inc
    $push
    $pop
    $pull
    ...
  • 82. "$gte"
  • 83. "<null>"
  • 84. ""
  • 85. '$gte' or "$gte"
  • 86. Or define your own!
    ":gte"
    "=gte"
    "?gte"
  • 87. Or define your own!
    " gte"
  • 88. Or define your own!
    " gte"
    " ♕ gte"
  • 89. Or define your own!
    " gte"
    " ♕ gte"
    "xgte"
  • 90. features
  • 91. I suddenly have a deep love for stored procedures
  • 92. I suddenly have a deep love for stored procedures
    now with stored procedures!
  • 93. $db->system->js->insert({
    "_id" => "x",
    "value" => 3});
    $db->system->js->insert({
    "_id" => "y",
    "value" => 4});
    $db->eval("return x+y");
  • 94. my $log = <<<LOG
    function(msg, level) {
    var date = "[" + new Date() + "] ";
    varlvl = level ? level+" " : "";
    print(date + lvl + msg);
    }
    LOG;
    $db->system->js->insert({
    "_id" => "log",
    "value" => $log});
  • 95. $db->eval("log('all your base are
    belong to us', 'FATAL');");
  • 96. $db->eval("log('all your base are
    belong to us', 'FATAL');");
    [Fri May 19 18:34:57] FATAL all your
    base are belong to us
  • 97.
  • 98.
  • 99. J
    J
    J
    J
    J
    J
    J
    J
    J
  • 100.
  • 101. $metadata = {
    "_id" => " "
    "date" => DateTime->now
    "downloads" => 0,
    "user" => $userId,
    "filename" => "rickroll.flv"
    }
    J
  • 102. $grid = $db->get_gridfs;
    $grid->put($fh, $metadata);
  • 103. $grid = $db->get_gridfs;
    $grid->put($fh, $metadata);
    $file = $grid->find_one({
    "filename" => qr/rickroll/i
    });
  • 104. $grid = $db->get_gridfs;
    $grid->put($fh, $metadata);
    $file = $grid->find_one({
    "filename" => qr/rickroll/i
    });
    $file->print($fh2);
  • 105. Benefits:
    • Automatic backups
    • 106. Unlimited metadata
    • 107. Millions of files per directory
    • 108. Random access
    • 109. Better disk locality
  • Geospatial Indexes
  • 110.
  • 111. $coll->insert({location => (-40, 78)});
  • 112. $coll->insert({location => (-40, 78)});
    $coll->find({location => {
    '$near' => (-40, 77)}})
  • 113. $coll->insert({location => (-40, 78)});
    $coll->find({location => {
    '$near' => (-40, 77)}})->limit(10)
  • 114. what doesn't it have?
  • 115. transactions
  • 116. are you practicing safe
    data storage?
  • 117.
  • 118. Insert this.
    Okay, got it.
    Phew, my data's safe.
  • 119. Write this to disk
    I know better than he does, I'll just let this sit in a buffer for a while.
    All over it!
  • 120.
  • 121. ?
    I have no idea what you're talking about.
  • 122. trust no one!
    ...trust a bunch of ones. Mostly.
  • 123. prod1.example.com
    prod2.example.com
  • 124. prod1.example.com
    prod2.example.com
  • 125. prod1.example.com
    prod2.example.com
  • 126. prod2.example.com
    prod1.example.com
  • 127.
  • 128. prod1.example.com
    prod2.example.com
  • 129. prod1.example.com
    prod2.example.com
  • 130. prod1.example.com
    ?
    prod2.example.com
  • 131. prod1.example.com
    prod2.example.com
  • 132. ?
    prod1.example.com
    prod2.example.com
  • 133. prod1.example.com
    prod2.example.com
  • 134. prod1.example.com
    prod2.example.com
  • 135. primary
    secondary
    secondary
    passive
  • 136.
  • 137.
  • 138.
  • 139.
  • 140. okay
  • 141. ?
  • 142. make sure two slaves have this
  • 143.
  • 144.
  • 145.
  • 146. all set
  • 147.
  • 148.
  • 149. EXCEPTION
  • 150. EXCEPTION
  • 151. EXCEPTION
  • 152.
  • 153. $conn->w(3);
    $collection->insert({"x" => 1},
    {"safe" => 1});
  • 154. scaling
  • 155.
  • 156. sometimes read-only
  • 157. sometimes read-only
    sometimes inconsistent
  • 158. U - Z
    K - O
    A - E
    F - J
    P - T
  • 159. U - Z
    K - O
    A - E
    F - J
    P - T
  • 160. U - Z
    K - O
    A - E
    F - J
    P - T
  • 161.
  • 162. mongod
    mongod
    mongod
    config
    mongos
  • 163. mongod
    mongod
    mongod
    mongos
    I want posts from April 2009 - June 2009
  • 164. mongod
    mongod
    mongod
    April and May is on #2. June is on #3.
    mongos
  • 165. mongod
    mongod
    mongod
    mongos
  • 166. mongod
    mongod
    mongod
    mongos
  • 167. mongod
    mongod
    mongod
    mongos
    Add this post for
    March 26, 2010.
  • 168. mongod
    mongod
    mongod
    #3 hold June 2009-present
    mongos
  • 169. mongod
    mongod
    mongod
    config
    So, what's going on?
    mongos
  • 170. mongod
    mongod
    mongod
    config
    mongos
  • 171. $conn = MongoDB::Connection->new;
    # ...
  • 172.
  • 173. try.mongodb.org
  • 174. www.mongodb.org
  • 175. thank you!
    @kchodorow
    http://www.snailinaturtleneck.com