You are on page 1of 30

A Quick Intro to Redis

(with special guest appearances by RESQUE and VANITY)

Nick Plante | Mogotest


@zapnap @mogotest
NH.rb 08.19.10
What is Redis?
• It’s “NoSQL”. It’s a key value store.
– NoSQL is NOT a SQL replacement.

• Sort of.
– “A Better Memcached replacement”
– Has native list and set operations
– Non-volatile – periodic snapshots saved to disk
– Pub/sub and notifications (new)
Redis Backgrounder
• Creator / maintainer: Salvatore Sanfilippo
– Supported by VMWare, BSD licensed

– Development started in March, 2009


– Current release: 1.2
– Release candidate: 2.0

• http://code.google.com/p/redis/
Why Use Redis?
• It’s fast.
– Non-blocking I/O
– 100k+ sets/gets per second (on entry-level hardware)
• It scales.
– Use it on a single VPS or …
– Supports replication across several servers
• Getting started is simple.
– Complements existing (database) storage
– Use it for caching
– Use it for queueing
In-Memory
• Entire dataset must fit in RAM
• Virtual memory – Available in Redis 2.0
– Allows infrequently access keys to be saved to disk
– Enable it in redis.conf
Try It
• Install a Redis server on your system
– git clone git://github.com/antirez/redis.git
– cd redis
– make;
– ./redis-server

• Try it out in a browser (slightly out of date)


– http://try.redis-db.com/
Command Line Interface
• ./redis-cli
• redis>
• ./redis-cli -h 127.0.0.1 -p 6379
• ./redis-cli set the-answer 42
• ./redis-cli get the-answer
A Note About Keys
• Shorter keys perform better
• Common convention
– keys:with:colons:for:namespacing
– obj-type:id:attr
– mogo:html-pages:1202:contents

• MD5s are useful, too


Example: Store Download Counts
• key = MD5(file)
• Incr downloads-by-file:$key
• Incr downloads-by-day:2010-07-19
Data Types
• Data Types
– Binary-safe strings
– Lists
– Sets
– Sorted sets (Zsets)
– Hashes
– Pubsub channels!
Commands (on Strings)
• Exists / Keys / Type
• Del / Rename
• Expire / ExpireAt

• Set / Get / Mset / Mget


• Incr / Decr
• Append / Substr

• GetSet / SetNx (atomic)


• (lots more)
Other Commands
• Database Commands (FlushDB, Select, …)
• Commands on Sets (Slen, Sadd, Scard, …)
• Commands on Lists (Rpush, Lpop, Llen, …)

• Commands on Hashes, Sorted Sets, etc


• Persistence control, pub/sub, replication, sorting…

• Full reference
– http://code.google.com/p/redis/wiki/CommandReference
Use it with Rails!
• Session storage: REDIS-SESSION-STORE gem
– store session data + expire

• Use it for Rails cache: REDIS-STORE gem


– Cache model data
– Controller action caching
– View-level Fragment caching

• General Ruby interface: REDIS gem (redis-rb)


Caching Expensive Model Methods
def some_expensive_operation
Rails.cache.fetch(:expensive_op,
:expires_in => 2.weeks) do
calculate_whatever
or_hit_remote_service_and_cache_data
end
end
View-Level Fragment Caching
cache("site-#{@site.id}/summary") do
<div><%= helper_method %></div>
<%= link_to('whatever', items_link) %>
end
Use Redis for Job Queues: Resque
• GitHub’s background job processing library
• Parent/child fork model
• Multiple queues
• Jobs are stored as JSON in Redis!

• Gem install Resque


• git://github.com/defunkt/resque.git
Resque: Jobs
• Normal Ruby classes
• Must respond to perform method
• Arguments passed to perform method
• @queue determines queue
Resque: Jobs
class ArchiveJob
@queue = :file_serve

def self.perform(item_id)
item = Item.find(item_id)
item.archive
end
end

Resque.enqueue(ArchiveJob, @item.id)
Resque: Sinatra Web UI
Resque: Extensions
• quirkey/resque-status
– Provides simple progress tracking for jobs
• zapnap/resque_mailer
– Asynchronous email delivery with ActionMailer
• bvandenbos/resque-scheduler
– Lightweight job scheduling
A/B Testing with Vanity (Uses Redis)

• Experiment driven development framework


for Rails (A/B-testing)
• Uses Redis to store experiment data

• Gem install vanity


• git://github.com/assaf/vanity.git

• http://vanity.labnotes.org/
Example Experiment
A Vanity “Experiment”
ab_test "Price options" do
description ”Which is the best price?”
alternatives 19, 25, 29 metrics :signup
end
In Your View…
<h2>Get started for only $<%= ab_test :price_options %> a
month!</h2>
Track Conversions
class SignupController < ApplicationController
def signup
@account = Account.new(params[:account])
if @account.save
track! :signup
redirect_to @acccount
else
render action: :offer
end
end
end
Check Reports
• vanity report --output vanity.html
http://mogotest.com
• We Use Redis for many things:
– Resque / Job queue
– Vanity / Experiment data
– Storing test run job counts
– Model data caching
– Document caching
– View / fragment caches
– Storing aggregated statistics
– User session storage
• But remember: It’s not a replacement for SQL!
More Information
• http://code.google.com/p/redis/
• http://retwis.antirez.com/
• http://rubygems.org/gems/redis

• http://github.com/defunkt/resque
• http://vanity.labnotes.org/

You might also like