persistence smoothie

76
Persistence Smoothie Blending SQL and NoSQL Michael Bleigh Intridea, Inc. photo by Nikki L. via Flickr Thursday, March 11, 2010

Upload: michael-bleigh

Post on 09-May-2015

3.410 views

Category:

Technology


3 download

DESCRIPTION

Talk given at Confoo 2010 in Montreal describing how to integrate NoSQL systems with SQL in Ruby applications.

TRANSCRIPT

Page 1: Persistence Smoothie

Persistence SmoothieBlending SQL and NoSQL

Michael BleighIntridea, Inc.

photo by Nikki L. via Flickr

Thursday, March 11, 2010

Page 2: Persistence Smoothie

Thursday, March 11, 2010

Page 3: Persistence Smoothie

Thursday, March 11, 2010

Page 4: Persistence Smoothie

present.ly

Thursday, March 11, 2010

Page 5: Persistence Smoothie

tweetstream hashieacts-as-taggable-on

subdomain-fu seed-fumustache_json

github.com/intridea

Thursday, March 11, 2010

Page 6: Persistence Smoothie

@mbleigh

Thursday, March 11, 2010

Page 7: Persistence Smoothie

You’ve (probably)heard a lot about

NoSQL

Thursday, March 11, 2010

Page 8: Persistence Smoothie

NoSQL is a new way to think about

persistence

Thursday, March 11, 2010

Page 9: Persistence Smoothie

AtomicityConsistency

IsolationDurability

Thursday, March 11, 2010

Page 10: Persistence Smoothie

DenormalizationEventual Consistency

Schema-FreeHorizontal Scale

Thursday, March 11, 2010

Page 11: Persistence Smoothie

NoSQL tries to scale (more) simply

Thursday, March 11, 2010

Page 12: Persistence Smoothie

NoSQL is going mainstream

Thursday, March 11, 2010

Page 13: Persistence Smoothie

New York TimesBusiness InsiderBBC ShopWikiGitHub Meebo

Disqus SourceForgeSony Digg

Thursday, March 11, 2010

Page 14: Persistence Smoothie

...but not THAT mainstream.

Thursday, March 11, 2010

Page 15: Persistence Smoothie

A word of caution...

Thursday, March 11, 2010

Page 16: Persistence Smoothie

NoSQL can divide by zero

Thursday, March 11, 2010

Page 17: Persistence Smoothie

NoSQL can divide by zero

NoSQL doesn’t

sleep, it waits

NoSQL counted to infinity, twiceThursday, March 11, 2010

Page 18: Persistence Smoothie

NoSQL is a (growing) collection of tools, not

a new way of life

Thursday, March 11, 2010

Page 19: Persistence Smoothie

Key-Value StoresDocument Databases

Column StoresGraph Databases

Thursday, March 11, 2010

Page 20: Persistence Smoothie

Key-Value Stores

Thursday, March 11, 2010

Page 21: Persistence Smoothie

Redis

• Key-value store + datatypes

• Lists, (Scored) Sets, Hashes

• Cache-like functions (expiration)

• (Mostly) In-Memory

Thursday, March 11, 2010

Page 22: Persistence Smoothie

Riak

• Combo key-value store and document database

• HTTP REST interface

• “Link walking”

• Map-Reduce

Thursday, March 11, 2010

Page 23: Persistence Smoothie

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

Thursday, March 11, 2010

Page 24: Persistence Smoothie

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};}

Thursday, March 11, 2010

Page 25: Persistence Smoothie

Tokyo CabinetDynomite

MemcachedDBVoldemort

Thursday, March 11, 2010

Page 26: Persistence Smoothie

Document Databases

Thursday, March 11, 2010

Page 27: Persistence Smoothie

MongoDB

• Document store that speaks BSON (Binary JSON)

• Indexing, simple query syntax

• GridFS

• Deliberate MapReduce

Thursday, March 11, 2010

Page 28: Persistence Smoothie

CouchDB

• JSON Document Store

• HTTP REST Interface

• Incremental MapReduce

• Intelligent Replication

Thursday, March 11, 2010

Page 29: Persistence Smoothie

Column-Oriented Datastores

Thursday, March 11, 2010

Page 30: Persistence Smoothie

Cassandra

• Built by Facebook, used by Twitter

• Pure horizontal scalability

• Schemaless

Thursday, March 11, 2010

Page 31: Persistence Smoothie

Graph Databases

Thursday, March 11, 2010

Page 32: Persistence Smoothie

Neo4J

Thursday, March 11, 2010

Page 33: Persistence Smoothie

When should I use this stuff?

Thursday, March 11, 2010

Page 34: Persistence Smoothie

Complex, slow joins for “activity stream”

Thursday, March 11, 2010

Page 35: Persistence Smoothie

Complex, slow joins for “activity stream”

Denormalize, use Key-Value Store

Thursday, March 11, 2010

Page 36: Persistence Smoothie

Variable schema,vertical interaction

Thursday, March 11, 2010

Page 37: Persistence Smoothie

Variable schema,vertical interaction

Document Databaseor Column Store

Thursday, March 11, 2010

Page 38: Persistence Smoothie

Modeling multi-step relationships

Thursday, March 11, 2010

Page 39: Persistence Smoothie

Modeling multi-step relationships

Graph Database

Thursday, March 11, 2010

Page 40: Persistence Smoothie

NoSQL solves real scalability and data

design issues

Thursday, March 11, 2010

Page 41: Persistence Smoothie

Ben Scofieldbit.ly/state-of-nosql

Thursday, March 11, 2010

Page 42: Persistence Smoothie

Ready to go?

Thursday, March 11, 2010

Page 43: Persistence Smoothie

Just one problem...

Thursday, March 11, 2010

Page 44: Persistence Smoothie

Your data is already in a SQL database

Thursday, March 11, 2010

Page 45: Persistence Smoothie

We CAN all just get along.

Thursday, March 11, 2010

Page 46: Persistence Smoothie

Three Ways

Thursday, March 11, 2010

Page 47: Persistence Smoothie

The Hard(ish) Way

Thursday, March 11, 2010

Page 48: Persistence Smoothie

The Easy Way

Thursday, March 11, 2010

Page 49: Persistence Smoothie

A Better Way?

Thursday, March 11, 2010

Page 50: Persistence Smoothie

The Hard Way:Do it by hand.

Thursday, March 11, 2010

Page 51: Persistence Smoothie

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

Thursday, March 11, 2010

Page 52: Persistence Smoothie

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

Thursday, March 11, 2010

Page 53: Persistence Smoothie

The Easy Way:DataMapper

Thursday, March 11, 2010

Page 54: Persistence Smoothie

DataMapper

• Generic, relational ORM

• Speaks pretty much everything you’ve ever heard of

• Implements Identity Map

• Module-based inclusion

Thursday, March 11, 2010

Page 55: Persistence Smoothie

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

Thursday, March 11, 2010

Page 56: Persistence Smoothie

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

Thursday, March 11, 2010

Page 57: Persistence Smoothie

Is there a better way?

Thursday, March 11, 2010

Page 58: Persistence Smoothie

Maybe.

Thursday, March 11, 2010

Page 59: Persistence Smoothie

Gloo: Cross-ORM Relationship Mapper

github.com/intridea/gloo

Thursday, March 11, 2010

Page 60: Persistence Smoothie

0.0.0.prealpha.1

Thursday, March 11, 2010

Page 61: Persistence Smoothie

Can’t we just sit down and talk to

each other?

Thursday, March 11, 2010

Page 62: Persistence Smoothie

class Post include MongoMapper::Resource key :title, String key :body, String key :tags, Array gloo :active_record do belongs_to :user endend

class User < ActiveRecord::Base gloo :mongo_mapper do many :posts endend

Thursday, March 11, 2010

Page 63: Persistence Smoothie

Goals/Status

• Be able to define relationships on the terms of any ORM from any class, ORM or not

• Right Now: Partially working ActiveRecord relationships

• Doing it wrong? Maybe

Thursday, March 11, 2010

Page 64: Persistence Smoothie

Code Time: Schema4Less

Thursday, March 11, 2010

Page 65: Persistence Smoothie

Social Storefront

• Dummy application of a store that lets others “follow” your purchases (a less creepy Blippy?)

• Four requirements:

• users

• purchasing

• listings

• social graph

Thursday, March 11, 2010

Page 66: Persistence Smoothie

Users

• I already have an authentication system

• I’m happy with it

• It’s Devise and ActiveRecord

• Stick with SQL

Thursday, March 11, 2010

Page 67: Persistence Smoothie

Purchasing

• Users need to be able to purchase items from my storefront

• I can’t lose their transactions

• I need full ACID

• I’ll use MySQL

Thursday, March 11, 2010

Page 68: Persistence Smoothie

Social Graph

• I want activity streams and one and two way relationships

• I need speed

• I don’t need consistency

• I’ll use Redis

Thursday, March 11, 2010

Page 69: Persistence Smoothie

Product Listings

• I am selling both movies and books

• They have very different properties

• Products are relatively non-relational

• I’ll use MongoDB

Thursday, March 11, 2010

Page 70: Persistence Smoothie

Demo and Walkthrough

Thursday, March 11, 2010

Page 71: Persistence Smoothie

Thursday, March 11, 2010

Page 72: Persistence Smoothie

Wrapping Up

Thursday, March 11, 2010

Page 73: Persistence Smoothie

These systems can (and should) live and

work together

Thursday, March 11, 2010

Page 74: Persistence Smoothie

Most important step is to actually think about data design

Thursday, March 11, 2010

Page 75: Persistence Smoothie

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

like nails

Thursday, March 11, 2010

Page 76: Persistence Smoothie

Questions?

Thursday, March 11, 2010