Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Non-Relational Databases

1,442 views

Published on

San Francisco Perl Mongers presentation, September 3rd, 2009.

Published in: Technology, Business
  • Be the first to comment

  • Be the first to like this

Non-Relational Databases

  1. 1. Non-Relational Databases<br />San Francisco Perl Mongers<br />Kristina Chodorow<br />September 3, 2009<br />
  2. 2. Who am I?<br />Software Engineer at 10gen<br />Perl, PHP, Java drivers<br />
  3. 3.
  4. 4. Scaling<br />
  5. 5. Scaling<br />
  6. 6. Technique #1: <br />literally scale<br />
  7. 7. Technique #1: <br />literally scale<br />(Courtesy of Ask Bjorn Hansen)<br />$3,200<br />$4,000,000<br />x 1250 =<br />
  8. 8. Technique #2: shard<br />now-yesterday<br />yesterday-<br />the day before<br />the day before-<br />the day before that<br />
  9. 9. Technique #3: master-slave replication<br />R<br />W<br />W<br />R/W?<br />W<br />R<br />
  10. 10. Cassandra<br />Designed by Facebook<br />Distributed - eventually consistent<br />Column-oriented<br />
  11. 11. Cassandra<br />Writes ALWAYS work<br />Two types of reads: <br />high performance<br />guaranteed correct<br />R/W atomic within column family<br />
  12. 12. Cassandra<br />Row-oriented: <br />joe, 34, karen, 25, bob, 50<br />Column-oriented:<br />joe, karen, bob, 34, 25, 50<br />
  13. 13. Project Voldemort<br />Key/value store<br />Automatic replication/partitioning<br />75 Github forks<br />“It is basically a just a big, distributed, persistent, fault-tolerant hash table.”<br />
  14. 14. Tokyo Cabinet<br />Another key/value store<br />Blazing fast… theoretically<br />Lua for server-side scripting<br />
  15. 15. Tokyo Cabinet<br />Key/value pairs<br />Array store<br />Index-able Hash<br />{<br /> name : “Chris”<br /> age : 27,<br /> DOB : “January 1”<br />}<br />
  16. 16. CouchDB<br />Master-Master Replication<br />Map/Reduce<br />REST API<br />
  17. 17. CouchDB Futon<br />
  18. 18. MongoDB<br />
  19. 19. MongoDB<br />Ease of use<br />Scalable<br />Dynamic queries - similar “feel” to SQL<br />Speed of key/value stores (almost)<br />Power of RDBMSs (almost)<br />
  20. 20. The Perl Driver<br />Available at Github: <br />www.github.com/mongodb/mongo-perl-driver/<br />Install the Perl driver <br />$ perl Makefile.PL && sudo make install<br />Available on CPAN (but old):<br />http://search.cpan.org/~flora/MongoDB-0.01/<br />
  21. 21. Downloading MongoDB<br />www.mongodb.org<br />Binaries available for Linux, Mac, Windows, Solaris<br />
  22. 22. Start the DB Server<br />$ mkdir ~/db<br />$<br />$ tar zxvfmongodb-&lt;OS&gt;-1.0.0.tgz<br />$ cdmongodb-&lt;OS&gt;-1.0.0<br />$ bin/mongod --dbpath ~/db<br />
  23. 23. Connecting to the Database<br />use MongoDB;<br />my $connection = MongoDB::Connection-&gt;new;<br />my $db = $connection-&gt;get_database(‘test’);<br />my $collection = $db-&gt;get_collection(‘foo’);<br />
  24. 24. Inserting<br />my $id = $collection-&gt;insert(<br /> {<br /> title =&gt; ‘My first blog post’, <br /> author =&gt; ‘Joe’, <br /> content =&gt; ‘Hello, world!’<br /> comments =&gt; []<br /> }<br />);<br />
  25. 25. MongoDB::OID<br />an autogenerated primary key<br />my $id = $collection-&gt;insert({whatever});<br />print Dumper($id);<br />--------------------------------------------<br />$VAR1 = bless( { &apos;value&apos; =&gt; &apos;4a9700dba5f9107c5cbc9a9c&apos; }, &apos;MongoDB::OID&apos; )<br />
  26. 26. Updating<br />$collection-&gt;update({_id =&gt; $id}, <br />{‘$push’ =&gt; {comments =&gt; <br /> {<br /> ‘author’ =&gt; ‘Fred’,<br /> ‘comment’ =&gt; ‘Dumb post.”<br /> }<br />}});<br />
  27. 27. …which gives us:<br />print Dumper($collection-&gt;find_one());<br />--------------------------------------------<br />{<br /> title : ‘My first blog post’, <br /> author : ‘Joe’, <br /> content : ‘Hello, world!’<br /> comments : [{<br /> ‘author’ : ‘Fred’,<br /> ‘comment’ : ‘Dumb post’<br /> }]<br />}<br />
  28. 28. Magic (not the Perl kind)<br />$gt, $gte, $lt, $lte, $eq, $neq, $exists, <br />$set, $mod, $where, $in, $nin, $inc<br />$push, $pull, $pop, $pushAll, $popAll<br />$c-&gt;query({ x =&gt; {‘$gt’ =&gt; 4}})<br />
  29. 29. Querying<br />my $commented_by_fred = <br /> $collection-&gt;query({<br /> “comments.author” : “Fred”<br /> });<br />my $commented_by_fred = <br /> $collection-&gt;query({<br /> “comments.author” : qr/fred/i<br /> });<br />
  30. 30. $where<br />$collection-&gt;find_one({‘$where’ : <br /> ‘this.y == (this.x + this.z)’});<br />Will work:<br />{x =&gt; 1, y =&gt; 4, z =&gt; 3}<br />{x =&gt; “hi”, y =&gt; “hibye”, z =&gt; “bye”}<br />Won’t work:<br />{x =&gt; 1, y =&gt; 1}<br />
  31. 31. Optimizing $where<br />$collection-&gt;find_one({<br /> ‘name’ =&gt; ‘Sally’,<br /> ‘age’ =&gt; {‘$gt’ =&gt; 18},<br /> ‘$where’ =&gt; ‘Array.sort(this.interests)[2] == “volleyball”’});<br />
  32. 32. Speaking of indexing…<br />$collection-&gt;ensure_index({“age” =&gt; “ascending”});<br />$collection-&gt;ensure_index({<br /> “name” =&gt; “descending”,<br /> “ts” =&gt; “descending”,<br /> “comments.author” =&gt; “ascending”<br />});<br />
  33. 33. Cursors<br />my $cursor = $coll-&gt;query({“foo” =&gt; “bar”});<br />my $obj;<br />while (my $obj = $cursor-&gt;next) {<br /> ...<br />}<br />my @all = $cursor-&gt;all;<br />
  34. 34. Paging<br />my $cursor = $coll-&gt;query-&gt;<br /> sort({ts =&gt; -1})-&gt;<br /> skip($page_num * $results_per_page)-&gt;<br /> limit($results_per_page);<br />my @page = $cursor-&gt;all;<br />
  35. 35. Logging<br />insert/update/remove is fast<br />Capped collections<br />Schemaless<br />$inc for counts<br />
  36. 36. Thank you!<br />kristina@10gen.com<br />@kchodorow<br />@mongodb<br />irc.freenode.net#mongodb<br />www.mongodb.org<br />

×