Persistence Smoothie: Blending SQL and NoSQL (RubyNation Edition)

59
Persistence Smoothie Blending SQL and NoSQL Michael Bleigh Intridea, Inc. photo by Nikki L. via Flickr Saturday, April 10, 2010

description

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

Transcript of Persistence Smoothie: Blending SQL and NoSQL (RubyNation Edition)

Page 1: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

Persistence SmoothieBlending SQL and NoSQL

Michael BleighIntridea, Inc.

photo by Nikki L. via Flickr

Saturday, April 10, 2010

Page 2: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

Saturday, April 10, 2010

Page 3: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

present.ly

Saturday, April 10, 2010

Page 4: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

The Buzz

Saturday, April 10, 2010

Page 5: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

You’ve (probably)heard a lot about

NoSQL

Saturday, April 10, 2010

Page 6: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

NoSQL is a new way to think about

persistence

Saturday, April 10, 2010

Page 7: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

AtomicityConsistency

IsolationDurability

Saturday, April 10, 2010

Page 8: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

DenormalizationEventual Consistency

Schema-FreeHorizontal Scale

Map Reduce

Saturday, April 10, 2010

Page 9: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

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

Page 10: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

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

Page 11: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

NoSQL tries to scale (more) simply

Saturday, April 10, 2010

Page 12: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

NoSQL is going mainstream

Saturday, April 10, 2010

Page 13: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

New York TimesBusiness InsiderBBC ShopWikiGitHub Meebo

Disqus SourceForgeSony Digg

Saturday, April 10, 2010

Page 14: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

...but not THAT mainstream.

Saturday, April 10, 2010

Page 15: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

A word of caution...

Saturday, April 10, 2010

Page 16: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

Saturday, April 10, 2010

Page 17: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

NoSQL can divide by zero

NoSQL doesn’t

sleep, it waits

NoSQL counted to infinity, twiceSaturday, April 10, 2010

Page 18: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

NoSQL is a (growing) collection of tools, not

a new way of life

Saturday, April 10, 2010

Page 19: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

The Ecosystem

Saturday, April 10, 2010

Page 20: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

Key-Value Stores

•Redis

•Riak

• Voldemort

• Tokyo Cabinet

•MemcachedDB

Saturday, April 10, 2010

Page 21: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

Document Stores

•MongoDB

•CouchDB• Riak

• FleetDB

Saturday, April 10, 2010

Page 22: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

Column(ish) Stores

•Cassandra

•HBase

Saturday, April 10, 2010

Page 23: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

Graph Databases

•Neo4j

•HypergraphDB

•InfoGrid

Saturday, April 10, 2010

Page 24: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

When should I use this stuff?

Saturday, April 10, 2010

Page 25: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

Complex, slow joins for “activity stream”

Saturday, April 10, 2010

Page 26: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

Complex, slow joins for “activity stream”

Denormalize, use Key-Value Store

Saturday, April 10, 2010

Page 27: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

Variable schema,vertical interaction

Saturday, April 10, 2010

Page 28: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

Variable schema,vertical interaction

Document Databaseor Column Store

Saturday, April 10, 2010

Page 29: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

Modeling deep relationships

Saturday, April 10, 2010

Page 30: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

Modeling deep relationships

Graph Database

Saturday, April 10, 2010

Page 31: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

NoSQL solves real scalability and data

design issues

Saturday, April 10, 2010

Page 32: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

Ben Scofieldbit.ly/state-of-nosql

Saturday, April 10, 2010

Page 33: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

Ready to go?

Saturday, April 10, 2010

Page 34: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

Just one problem...

Saturday, April 10, 2010

Page 35: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

Your data is already in a SQL database

Saturday, April 10, 2010

Page 36: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

So now we need to ask the question...

Saturday, April 10, 2010

Page 37: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

Saturday, April 10, 2010

Page 38: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

Yeah, it blends.

Saturday, April 10, 2010

Page 39: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

The “Hard” Way:Do it by hand.

Saturday, April 10, 2010

Page 40: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

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 endend

class User < ActiveRecord::Base def posts(options = {}) Post.all({:conditions => {:user_id => self.id}}.merge(options)) endend

Saturday, April 10, 2010

Page 41: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

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

Page 42: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

The “Easy” Way:DataMapper

Saturday, April 10, 2010

Page 43: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

DataMapper

•Generic, relational ORM

• Speaks pretty much everything you’ve ever heard of

• Implements Identity Map

•Module-based inclusion

Saturday, April 10, 2010

Page 44: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

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 :userend

class User include DataMapper::Resource property :email, String property :name, String has n, :postsend

Saturday, April 10, 2010

Page 45: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

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

Page 46: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

Show and Tell: Social Storefront

Saturday, April 10, 2010

Page 47: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

Saturday, April 10, 2010

Page 48: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

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

Page 49: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

Users

• I already have an authentication system

• I’m happy with it

• It’s Devise and ActiveRecord

• Stick with SQL

Saturday, April 10, 2010

Page 50: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

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

Page 51: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

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

Page 52: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

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

Page 53: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

Demo and Walkthrough

Saturday, April 10, 2010

Page 54: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

Wrapping Up

Saturday, April 10, 2010

Page 55: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

These systems can (and should) live and

work together

Saturday, April 10, 2010

Page 56: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

Most important step is to actually think about data design

Saturday, April 10, 2010

Page 57: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

When you have a whole bag of tools, things stop looking

like nails

Saturday, April 10, 2010

Page 58: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

@mbleigh

Saturday, April 10, 2010

Page 59: Persistence  Smoothie: Blending SQL and NoSQL (RubyNation Edition)

Questions?

Saturday, April 10, 2010