Riak with Rails


Published on

Ruby on Rails is a powerful web framework that focuses on developer productivity. Riak is a friendly key-value store that is simple, flexible and scalable. Put them together and you have lots of exciting possibilities!

Published in: Technology

Riak with Rails

  1. 1. Riak with Rails Sean Cribbs Developer Advocate basho
  2. 2. Why Riak with Rails? Simple to understand and use Share-nothing, homogeneous Web-friendly Grows with your app (“scales”) Lots of use-cases basho
  3. 3. Use Cases Document database File / upload storage (DFS) Cache / object store Session store basho
  4. 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. 5. Rails 3 Setup # config/ripple.yml development: host: port: 8098 # config/application.rb require 'ripple/railtie' basho
  6. 6. Basic Client Operations basho
  7. 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. 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. 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. 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. 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. 12. 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
  13. 13. Use-Case: Document DB basho
  14. 14. 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
  15. 15. 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
  16. 16. Caveats No indexes for efficient lookups (other than key) Still pretty new Currently hard to clean up after tests basho
  17. 17. Ripple Demo basho
  18. 18. Use Case: File storage basho
  19. 19. Riak as a File Server Web-shaped storage - plays well with ops Authenticated downloads Serve files directly from Riak basho
  20. 20. File Upload / Serve Demo basho
  21. 21. Use-Case: Cache store basho
  22. 22. 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
  23. 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. 24. Caveats Not as fast as memcached or redis No per-object expiry (could be done client-side) basho
  25. 25. Riak::CacheStore Demo basho
  26. 26. Use-Case: Session store basho
  27. 27. 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
  28. 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. 29. Wrap-up basho
  30. 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. 31. Ripple Roadmap Testing assistance/automation Protocol buffers interface, HTTP enhancements More association types Get involved: http://github.com/seancribbs/ripple basho
  32. 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. 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