Persistence Smoothie is a talk given at RubyNation 2010 about when, how, and why to use combinations of persistence engines (including both SQL and NoSQL options) with a live example. The code is available at http://github.com/mbleigh/persistence-smoothie
9. Map Reduce
• Massively parallel way to
process large datasets
• First you scour data and “map” a
new set of data
• Then you “reduce” the data
down to a salient result
Saturday, April 10, 2010
10. map = function() {
this.tags.forEach(function(tag) {
emit(tag, {count: 1});
});
}
reduce = function(key, values) {
var total = 0;
for (var i = 0; i < values.length; i++) {
total += values[i].count;
return {count: total};
}
Saturday, April 10, 2010
11. NoSQL tries to scale
(more) simply
Saturday, April 10, 2010
40. class Post
include MongoMapper::Document
key :title, String
key :body, String
key :tags, Array
key :user_id, Integer
def user
User.find_by_id(self.user_id)
end
def user=(some_user)
self.user_id = some_user.id
end
end
class User < ActiveRecord::Base
def posts(options = {})
Post.all({:conditions => {:user_id => self.id}}.merge(options))
end
end
Saturday, April 10, 2010
41. Pros & Cons
• Simple, maps to your domain
• Works for small, simple ORM intersections
• MUCH simpler in Rails 3
• Complex relationships are a mess
• Makes your models fat
• As DRY as the ocean
Saturday, April 10, 2010
43. DataMapper
• Generic, relational ORM
• Speaks pretty much everything
you’ve ever heard of
• Implements Identity Map
• Module-based inclusion
Saturday, April 10, 2010
44. DataMapper.setup(:default, "mysql://localhost")
DataMapper.setup(:mongodb, "mongo://localhost/posts")
class Post
include DataMapper::Resource
def self.default_repository_name; :mongodb; end
property :title, String
property :body, String
property :tags, Array
belongs_to :user
end
class User
include DataMapper::Resource
property :email, String
property :name, String
has n, :posts
end
Saturday, April 10, 2010
45. Pros & Cons
• The ultimate Polyglot ORM
• Simple relationships between persistence
engines are easy
• Jack of all trades, master of none
• Perpetuates (sometimes) false assumptions
• Legacy stuff is in ActiveRecord anyway
Saturday, April 10, 2010
46. Show and Tell:
Social Storefront
Saturday, April 10, 2010
48. The Application
• Dummy version of a store that lets
others “follow” your purchases (like a
less creepy version of Blippy)
• Four requirements:
• users
• purchasing
• listings
• social graph
Saturday, April 10, 2010
49. Users
• I already have an authentication
system
• I’m happy with it
• It’s Devise and ActiveRecord
• Stick with SQL
Saturday, April 10, 2010
50. Purchasing
• Users need to be able to purchase
items from my storefront
• I can’t lose their transactions
• I need full ACID
• SQL Again
Saturday, April 10, 2010
51. Social Graph
• I want activity streams and one
and two way relationships
• I need speed
• I don’t need consistency
• I’ll use Redis
Saturday, April 10, 2010
52. Product Listings
• I am selling both books about
Ruby and movies about zombies
• They have very different
properties
• Products are relatively non-
relational
• I’ll use MongoDB
Saturday, April 10, 2010