54
Persistence Smoothie Blending SQL and NoSQL Flip Sasser @flipsasser

Persistence Smoothie

Embed Size (px)

Citation preview

Page 1: Persistence Smoothie

Persistence Smoothie

Blending  SQL  and  NoSQL

Flip  Sasser@flipsasser

Page 3: Persistence Smoothie

NooooooSQL

Page 4: Persistence Smoothie

Photo  credit:  h6p://www.flickr.com/photos/2493/

Page 5: Persistence Smoothie

Persistence

Page 6: Persistence Smoothie

No ACIDSacrifices  transacHons  and  consistency  for  performance

Page 7: Persistence Smoothie

Keep what you know

You  shouldn’t  be  running  *just*  NoSQL  at  all  Hmes

Page 8: Persistence Smoothie

•Key-Value Stores•Document Stores•Column-based Stores•Graph Stores

Page 9: Persistence Smoothie

•Key-Value Stores•Document Stores•Column-based Stores•Graph Stores

Page 10: Persistence Smoothie

•Redis•Riak

•Voldemort•Tokyo Cabinet•MemcachedDB

Page 11: Persistence Smoothie

•Key-Value Stores•Document Stores•Column-based Stores•Graph Stores

Page 12: Persistence Smoothie

•CouchDB•MongoDB

•Riak•FleetDB

Page 13: Persistence Smoothie

Map/Reducemap = 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};}

Page 14: Persistence Smoothie

•Key-Value Stores•Document Stores•Column-based Stores•Graph Stores

Page 15: Persistence Smoothie

•Cassandra•HBase

Page 16: Persistence Smoothie

•Key-Value Stores•Document Stores•Column-based Stores•Graph Stores

Page 17: Persistence Smoothie

•Neo4j•HypergraphDB•InfoGrid

Page 18: Persistence Smoothie

Use Cases

Page 19: Persistence Smoothie

Use CasesKey-­‐Value  Store:  Ugly  joins  =>  denormalized  data

Page 20: Persistence Smoothie

Use CasesDocument  Store:  Loose  schema,  no  ACID

Page 21: Persistence Smoothie

Use CasesGraph  Store:  Deep  relaHonships

Page 22: Persistence Smoothie

Ready to jump in?

Page 23: Persistence Smoothie

Too bad.You’re  already  using  SQL.

Page 24: Persistence Smoothie

Let’s mix it together

Page 25: Persistence Smoothie

First AttemptDo  it  by  hand

Page 26: 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

Page 27: Persistence Smoothie

SimpleI  mean,  it  “works”

Page 28: Persistence Smoothie

Fat, wet models

Page 29: Persistence Smoothie

Second AttemptDataMapper  to  the  rescue

Page 30: Persistence Smoothie

Photo  credit:  h6p://www.flickr.com/photos/myklrovenHne

Page 31: 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

Page 32: Persistence Smoothie

SimplerAnd  it  sHll  works

Page 33: Persistence Smoothie

Ready to refactor?I’ve  sHll  got  all  this  AcHveRecord  lying  around...

Page 34: Persistence Smoothie

Go easy

Page 35: Persistence Smoothie

Store App

Page 36: Persistence Smoothie

•Authentication•Products•Purchases•Activity stream

Page 37: Persistence Smoothie

•Authentication•Products•Purchases•Activity stream

Page 38: Persistence Smoothie

gem install deviseLooks  like  we  can  keep  our  SQL

Page 39: Persistence Smoothie

•Authentication•Products•Purchases•Activity stream

Page 40: Persistence Smoothie

Color

SKU

ISBN

YearSize Dimensions

Weight

Genre

Category

Producer

Author

Country  of  Origin

Ports

ASIN

Label

Page 41: Persistence Smoothie

MetadataDon’t  need  ACID;  need  flexibility:  Document  store

Page 42: Persistence Smoothie

•Authentication•Products•Purchases•Activity stream

Page 43: Persistence Smoothie

TransactionsOkay,  more  SQL

Page 44: Persistence Smoothie

•Authentication•Products•Purchases•Activity stream

Page 45: Persistence Smoothie

JOIN cityDenormalize  with  a  Key-­‐Value  store

Page 46: Persistence Smoothie

Okay, let’s code

Page 47: Persistence Smoothie
Page 48: Persistence Smoothie

Mix it upDon’t  be  afraid  to  build  something  with  a  lot  of  moving  

parts

Page 49: Persistence Smoothie

Drawbacks

Page 50: Persistence Smoothie

DataPortability.nil?#=>  true

Page 51: Persistence Smoothie

Photo  credit:  h6p://www.flickr.com/photos/francoisroche

Page 52: Persistence Smoothie

gem uninstall everythingSay  goodbye  to  your  fun  AR  mixins

Page 53: Persistence Smoothie

Okay, thanksNow  I  can  relax

Page 54: Persistence Smoothie

@flipsasserThanks  to  @mbleigh