Riak with Rails
   Sean Cribbs
   Developer Advocate




basho
Why Riak with Rails?

        Simple to understand and use
        Share-nothing, homogeneous
        Web-friendly
        Grows with your app (“scales”)
        Lots of use-cases



basho
Use Cases

        Document database
        File / upload storage (DFS)
        Cache / object store
        Session store




basho
Getting Started

   # Gemfile
   gem 'curb'    # Faster HTTP
   gem 'yajl-ruby' # Faster JSON
   gem 'riak-client', :require => 'riak'
   gem 'ripple'


   $ gem install curb yajl-ruby ripple


basho
Rails 3 Setup

   # config/ripple.yml
   development:
    host: 127.0.0.1
    port: 8098


   # config/application.rb
   require 'ripple/railtie'



basho
Basic Client Operations



basho
require ‘riak’
        Make a client object
        client = Riak::Client.new # or Ripple.client

        Get a bucket
        bucket = client.bucket(‘foo’) # Riak::Bucket

        Get an object from the bucket
        obj = bucket.get(‘bar’) # Riak::RObject

        Initialize a new object
        obj = bucket.new(‘baz’)


basho
Riak::RObject
        Get/set object key
        obj.key = “bar”

        Get/set content-type
        obj.content_type = ‘application/json’

        Get/set the object body data (JSON auto enc/dec)
        obj.data = {“name” => “Sean”}

        Store the object
        obj.store


basho
Using Links

        Create a link
        Riak::Link.new(“/riak/bucket/key”, “tag”)

        Read an object’s links
        obj.links # Set<Riak::Link>

        Convert an object to a link
        obj.links << obj2.to_link(“next”)
        obj.store



basho
Link-Walking
   obj = client[‘demo’][‘test1’]
   obj.walk(:keep => true)
     # Get all linked one-level

   obj.walk(:bucket => “demo”, :keep => true)
     # Get all linked in “demo” bucket

   obj.walk(:tag => ‘friend’, :keep => true)
     # Get all linked with tag “friend”

   obj.walk({},{:keep => true})
     # Get all linked at second level


basho
MapReduce

   mr = Riak::MapReduce.new(Ripple.client)
   mr.add(“people”,”sean”)
   mr.link(:tag => “friend”)
   mr.map(“Riak.mapValuesJson”,:keep => true)
   mr.run

   Riak::MapReduce.new(Ripple.client).
    add(“people”,”sean”).
    link(:tag => “friend”).
    map(“Riak.mapValuesJson”,:keep => true).run



basho
Load RObjects from M/R


        Javascript Identity Map Function
        function(v){ return [v]; }

        Load output
        Riak::RObject.load_from_mapreduce(c, mr.run)
         # Returns Array<Riak::RObject>




basho
Use-Case: Document DB



basho
Riak as Document DB

        Store semi-structured data as “documents” (JSON)
        Alternative to CouchDB or MongoDB
        Links and Link-walking for easy relationships
        MapReduce for more involved queries




basho
Ripple - Document OM

        Build rich, multi-level nested document models
        ActiveModel goodies: validations, callbacks, etc
        Associations -- embedded, linked
        Requires Rails 3.0.0.rc




basho
Caveats


        No indexes for efficient lookups (other than key)
        Still pretty new
        Currently hard to clean up after tests




basho
Ripple Demo



basho
Use Case: File storage



basho
Riak as a File Server


        Web-shaped storage - plays well with ops
        Authenticated downloads
        Serve files directly from Riak




basho
File Upload / Serve Demo



basho
Use-Case: Cache store



basho
Riak as a Cache Store
        It’s a K/V store - similar to memcached, redis
        Flexible serialization options: JSON, YAML, Marshal,
        text, binary
        Tune for performance over consistency
          Low quorums and replication
          Last-write-wins?
          In-memory backend


basho
Using Riak::CacheStore

        Implements ActiveSupport::Cache::Store
        Sets quorum/n_val defaults for you (tweakable)
          R=W=RW=1, DW=0, N=2
        Doesn’t set backend or last_write_wins (tweakable)
        config.cache_store = :riak_store



basho
Caveats


        Not as fast as memcached or redis
        No per-object expiry (could be done client-side)




basho
Riak::CacheStore Demo



basho
Use-Case: Session store



basho
Riak as a Session store

        Substitute for ActiveRecord or memcached sessions
        Use when you need larger or longer-lived sessions
        Tune for modest durability and performance
          Low quorums, modest N (2 or 3)
          Fast disk backend (bitcask) or memory (ets)



basho
Ripple::SessionStore (WIP)

        Sets quorum/n_val defaults for you
        Implements ActionDispatch::Session::AbstractStore
        config.session_store = ::Riak::SessionStore
        Coming in next release of Ripple (0.8.0)




basho
Wrap-up



basho
Riak with Rails
        Useful in many contexts
          Semi-structured data/documents
          Web-friendly storage
          Distributed cache / session storage
          Flexible K/V storage
        Grows with your app


basho
Ripple Roadmap

        Testing assistance/automation
        Protocol buffers interface, HTTP enhancements
        More association types
        Get involved: http://github.com/seancribbs/ripple




basho
Plug
   Interested in learning about support, consulting, or
   Enterprise features?
   Email info@basho.com or go to
   http://www.basho.com/contact.html to talk with us.

                       www.basho.com




basho
Plug
   Interested in learning about support, consulting, or
   Enterprise features?
   Email info@basho.com or go to
   http://www.basho.com/contact.html to talk with us.

                        www.basho.com



              sean@basho.com
                @seancribbs

basho

Riak with Rails

  • 1.
    Riak with Rails Sean Cribbs Developer Advocate basho
  • 2.
    Why Riak withRails? Simple to understand and use Share-nothing, homogeneous Web-friendly Grows with your app (“scales”) Lots of use-cases basho
  • 3.
    Use Cases Document database File / upload storage (DFS) Cache / object store Session store basho
  • 4.
    Getting Started # Gemfile gem 'curb' # Faster HTTP gem 'yajl-ruby' # Faster JSON gem 'riak-client', :require => 'riak' gem 'ripple' $ gem install curb yajl-ruby ripple basho
  • 5.
    Rails 3 Setup # config/ripple.yml development: host: 127.0.0.1 port: 8098 # config/application.rb require 'ripple/railtie' basho
  • 6.
  • 7.
    require ‘riak’ Make a client object client = Riak::Client.new # or Ripple.client Get a bucket bucket = client.bucket(‘foo’) # Riak::Bucket Get an object from the bucket obj = bucket.get(‘bar’) # Riak::RObject Initialize a new object obj = bucket.new(‘baz’) basho
  • 8.
    Riak::RObject Get/set object key obj.key = “bar” Get/set content-type obj.content_type = ‘application/json’ Get/set the object body data (JSON auto enc/dec) obj.data = {“name” => “Sean”} Store the object obj.store basho
  • 9.
    Using Links Create a link Riak::Link.new(“/riak/bucket/key”, “tag”) Read an object’s links obj.links # Set<Riak::Link> Convert an object to a link obj.links << obj2.to_link(“next”) obj.store basho
  • 10.
    Link-Walking obj = client[‘demo’][‘test1’] obj.walk(:keep => true) # Get all linked one-level obj.walk(:bucket => “demo”, :keep => true) # Get all linked in “demo” bucket obj.walk(:tag => ‘friend’, :keep => true) # Get all linked with tag “friend” obj.walk({},{:keep => true}) # Get all linked at second level basho
  • 11.
    MapReduce mr = Riak::MapReduce.new(Ripple.client) mr.add(“people”,”sean”) mr.link(:tag => “friend”) mr.map(“Riak.mapValuesJson”,:keep => true) mr.run Riak::MapReduce.new(Ripple.client). add(“people”,”sean”). link(:tag => “friend”). map(“Riak.mapValuesJson”,:keep => true).run basho
  • 12.
    Load RObjects fromM/R Javascript Identity Map Function function(v){ return [v]; } Load output Riak::RObject.load_from_mapreduce(c, mr.run) # Returns Array<Riak::RObject> basho
  • 13.
  • 14.
    Riak as DocumentDB Store semi-structured data as “documents” (JSON) Alternative to CouchDB or MongoDB Links and Link-walking for easy relationships MapReduce for more involved queries basho
  • 15.
    Ripple - DocumentOM Build rich, multi-level nested document models ActiveModel goodies: validations, callbacks, etc Associations -- embedded, linked Requires Rails 3.0.0.rc basho
  • 16.
    Caveats No indexes for efficient lookups (other than key) Still pretty new Currently hard to clean up after tests basho
  • 17.
  • 18.
    Use Case: Filestorage basho
  • 19.
    Riak as aFile Server Web-shaped storage - plays well with ops Authenticated downloads Serve files directly from Riak basho
  • 20.
    File Upload /Serve Demo basho
  • 21.
  • 22.
    Riak as aCache Store It’s a K/V store - similar to memcached, redis Flexible serialization options: JSON, YAML, Marshal, text, binary Tune for performance over consistency Low quorums and replication Last-write-wins? In-memory backend basho
  • 23.
    Using Riak::CacheStore Implements ActiveSupport::Cache::Store Sets quorum/n_val defaults for you (tweakable) R=W=RW=1, DW=0, N=2 Doesn’t set backend or last_write_wins (tweakable) config.cache_store = :riak_store basho
  • 24.
    Caveats Not as fast as memcached or redis No per-object expiry (could be done client-side) basho
  • 25.
  • 26.
  • 27.
    Riak as aSession store Substitute for ActiveRecord or memcached sessions Use when you need larger or longer-lived sessions Tune for modest durability and performance Low quorums, modest N (2 or 3) Fast disk backend (bitcask) or memory (ets) basho
  • 28.
    Ripple::SessionStore (WIP) Sets quorum/n_val defaults for you Implements ActionDispatch::Session::AbstractStore config.session_store = ::Riak::SessionStore Coming in next release of Ripple (0.8.0) basho
  • 29.
  • 30.
    Riak with Rails Useful in many contexts Semi-structured data/documents Web-friendly storage Distributed cache / session storage Flexible K/V storage Grows with your app basho
  • 31.
    Ripple Roadmap Testing assistance/automation Protocol buffers interface, HTTP enhancements More association types Get involved: http://github.com/seancribbs/ripple basho
  • 32.
    Plug Interested in learning about support, consulting, or Enterprise features? Email info@basho.com or go to http://www.basho.com/contact.html to talk with us. www.basho.com basho
  • 33.
    Plug Interested in learning about support, consulting, or Enterprise features? Email info@basho.com or go to http://www.basho.com/contact.html to talk with us. www.basho.com sean@basho.com @seancribbs basho