Javier Ramírez
@supercoco9
Fun with
and
What?
Redis is an open source, BSD licensed, advanced
key-value store. It is often referred to as a data
structure server since keys can contain strings, hashes,
lists, sets and sorted sets. (http://redis.io)
Salvatore
Sanfilippo
@antirez
Pieter
Noordhuis
@pnoordhuis
95 contributors at
https://github.com/antirez/redis
Who?
The Redis Manifesto
1.A DSL for Abstract Data Types
2.Memory storage is #1
3.Fundamental data structures for a fundamental API
4.Two levels of API
5.Code is like a poem; it's not just something we write
to reach some practical result
6.We're against complexity
7.We optimize for joy
Redis
is fast
unscientific benchmarks*
*on an AWS micro instance with the default install for postgresql and redis
PostgreSQL Redis
1K inserts 3.9s 0.07s
1K reads 0.113s 0.06s
10K inserts 42.9s 0.67s
10K reads 1.15s 0.6s
100K inserts 6.8m 7s
100K reads 11.59s 6.06s
100K pipelined
inserts
- 1.45s
100K pipelined
reads
- 1.22s
Redis is game changing
Disclaimer: after using it, you'll find yourself
doing things you wouldn't do before
Some will not make sense
As every other technology, Redis has its
sweet spot, but it's not a silver bullet
Abusing sidekiq/RESQUE
In memory data store
Redis stores all the data in memory all the
time. Plan your architecture accordingly
Persistance: RDB
Compact binary format
Saves snapshots every few minutes
Good for backups and synchronizing
If Redis crashes, a few minutes worth of
data will be lost
Durability: AOF
Log text format
Configurable durability
Large file, can slow down startup
If Redis crashes, typically one second of
data could be lost
Connecting from Ruby
redis-rb exposes calls to all the methods in the redis
protocol
other useful gems:
github.com/soveran/nest => helps with key naming
https://github.com/soveran/ohm => “ORM” for redis
github.com/obie/redis_props => properties for AR objects
github.com/nateware/redis-objects => map redis types to ruby
objects
github.com/agoragames/amico => redis-backed friendships
DATA TYPES
Strings
Hashes
Lists
Sets
Sorted Sets
Key handling
Transactions
Scripting
Pub/Sub
Server
Connection
untyped operations
STRING commands
string commands
append, get, getset, set
getrange, setrange, strlen
bitcount, bitop, getbit, setbit
mget, mset, msetnx
setnx, setex, psetex
incr, incrby, incrbyfloat, decr, decrby, decrbyfloat
LIST commands
llen, lindex, linsert, lrange, lrem, lset, ltrim
lpop, lpush, lpushx, rpop, , rpush, rpushx
blpop, brpop
rpoplpush, brpoplpush
List commands
Hash commands
hkeys, hvals, hlen, hgetall, hexists
hmget, hmset
hdel, hget, hset, hsetnx
hincrby, hincrbyfloat
at the hash level
at the attribute level
Set commands
sadd, scard, smembers,
sismember, srem, smove, srandmember, smove
sdiff, sinter, sunion
sdiffstore, sinterstore, sunionstore
Set commands
team.euruko.users javier diego matz nikos
team.teowaki.users javier diego alberto
team.euruko location:greece contact:nikos edition:2013
team.teowaki location:london contact:diego
tags.matz ruby mruby
tags.nikos ruby css
tags.javier ruby
tags.diego ruby chef
users.javier.teams euruko teowaki
users.alberto.teams teowaki
users.nikos.teams euruko
users.matz.teams euruko
users.diego.teams euruko teowaki
r.sunion r.smembers('team.euruko').map{|m| "tags.#{m}"}
=>["mruby", "ruby", "css", "chef"]
r.sinter r.smembers('team.euruko').map{|m| "tags.#{m}"}
=>["ruby"]
r.diff r.smembers('team.euruko').map{|m| "tags.#{m}"}
=>["mruby", "css", "chef"]
sorted Set commands
zadd, zcard, zcount
zincrby, zrank, zrem,zrevrank, zscore
zrange, zrangebyscore, zremrangebyrank, zrevrangebyscore, zrevrange,
zrevrangebyscore
zinterstore, zunionstore
Sorted set commands
counters
Atomic counters can be
safely invoked concurrently
from anywhere
You can create global sequences
with counters. Redis-objects
block semantics allow for
rewinding on errors
Scripting with lua*
You can use Lua for scripting Redis when you need to
atomically execute a sequence of commands in which the
output of a command is used as input for another
It reduces the need to use complex lock mechanisms and
simplifies dependencies between clients
You can even extend the functionality
of Redis by using Lua scripts
* no, mruby won't be supported
LUA example
Expiring attributes inside a Redis hash*
1
2
4
5
3
Temporary data
Very simple to implement
usage quota patterns
(even better if done in Lua)
It's possible to set self-expiring keys
Redis as a cache
maxmemory 128mb
maxmemory-policy allkeys-lru
#save 900 1
#save 300 10
#save 60 10000
Expire keys individually or turn off persistence and use
Redis as a cache system with automatic eviction
Multiple levels of cache by using Redis on the webserver/
middleware layer
http://wiki.nginx.org/HttpRedis
https://github.com/jodosha/redis-store
Redis as a PUBSUB system
redsmin: remote Monitor
Shameless self promotion
If you enjoyed this presentation, please thank me by
registering on http://teowaki.com
It's a site for developers, you can hang around for free, and
I think it's quite cool
<3 <3 <3
Javier Ramírez
@supercoco9

Fun with Ruby and Redis

  • 1.
  • 2.
    What? Redis is anopen source, BSD licensed, advanced key-value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets. (http://redis.io) Salvatore Sanfilippo @antirez Pieter Noordhuis @pnoordhuis 95 contributors at https://github.com/antirez/redis Who?
  • 3.
    The Redis Manifesto 1.ADSL for Abstract Data Types 2.Memory storage is #1 3.Fundamental data structures for a fundamental API 4.Two levels of API 5.Code is like a poem; it's not just something we write to reach some practical result 6.We're against complexity 7.We optimize for joy
  • 4.
  • 5.
    unscientific benchmarks* *on anAWS micro instance with the default install for postgresql and redis PostgreSQL Redis 1K inserts 3.9s 0.07s 1K reads 0.113s 0.06s 10K inserts 42.9s 0.67s 10K reads 1.15s 0.6s 100K inserts 6.8m 7s 100K reads 11.59s 6.06s 100K pipelined inserts - 1.45s 100K pipelined reads - 1.22s
  • 6.
    Redis is gamechanging Disclaimer: after using it, you'll find yourself doing things you wouldn't do before
  • 7.
    Some will notmake sense As every other technology, Redis has its sweet spot, but it's not a silver bullet
  • 8.
  • 9.
    In memory datastore Redis stores all the data in memory all the time. Plan your architecture accordingly
  • 10.
    Persistance: RDB Compact binaryformat Saves snapshots every few minutes Good for backups and synchronizing If Redis crashes, a few minutes worth of data will be lost
  • 11.
    Durability: AOF Log textformat Configurable durability Large file, can slow down startup If Redis crashes, typically one second of data could be lost
  • 12.
    Connecting from Ruby redis-rbexposes calls to all the methods in the redis protocol other useful gems: github.com/soveran/nest => helps with key naming https://github.com/soveran/ohm => “ORM” for redis github.com/obie/redis_props => properties for AR objects github.com/nateware/redis-objects => map redis types to ruby objects github.com/agoragames/amico => redis-backed friendships
  • 13.
    DATA TYPES Strings Hashes Lists Sets Sorted Sets Keyhandling Transactions Scripting Pub/Sub Server Connection untyped operations
  • 14.
    STRING commands string commands append,get, getset, set getrange, setrange, strlen bitcount, bitop, getbit, setbit mget, mset, msetnx setnx, setex, psetex incr, incrby, incrbyfloat, decr, decrby, decrbyfloat
  • 15.
    LIST commands llen, lindex,linsert, lrange, lrem, lset, ltrim lpop, lpush, lpushx, rpop, , rpush, rpushx blpop, brpop rpoplpush, brpoplpush List commands
  • 16.
    Hash commands hkeys, hvals,hlen, hgetall, hexists hmget, hmset hdel, hget, hset, hsetnx hincrby, hincrbyfloat at the hash level at the attribute level
  • 17.
    Set commands sadd, scard,smembers, sismember, srem, smove, srandmember, smove sdiff, sinter, sunion sdiffstore, sinterstore, sunionstore Set commands
  • 18.
    team.euruko.users javier diegomatz nikos team.teowaki.users javier diego alberto team.euruko location:greece contact:nikos edition:2013 team.teowaki location:london contact:diego tags.matz ruby mruby tags.nikos ruby css tags.javier ruby tags.diego ruby chef users.javier.teams euruko teowaki users.alberto.teams teowaki users.nikos.teams euruko users.matz.teams euruko users.diego.teams euruko teowaki r.sunion r.smembers('team.euruko').map{|m| "tags.#{m}"} =>["mruby", "ruby", "css", "chef"] r.sinter r.smembers('team.euruko').map{|m| "tags.#{m}"} =>["ruby"] r.diff r.smembers('team.euruko').map{|m| "tags.#{m}"} =>["mruby", "css", "chef"]
  • 19.
    sorted Set commands zadd,zcard, zcount zincrby, zrank, zrem,zrevrank, zscore zrange, zrangebyscore, zremrangebyrank, zrevrangebyscore, zrevrange, zrevrangebyscore zinterstore, zunionstore Sorted set commands
  • 20.
    counters Atomic counters canbe safely invoked concurrently from anywhere You can create global sequences with counters. Redis-objects block semantics allow for rewinding on errors
  • 21.
    Scripting with lua* Youcan use Lua for scripting Redis when you need to atomically execute a sequence of commands in which the output of a command is used as input for another It reduces the need to use complex lock mechanisms and simplifies dependencies between clients You can even extend the functionality of Redis by using Lua scripts * no, mruby won't be supported
  • 22.
    LUA example Expiring attributesinside a Redis hash* 1 2 4 5 3
  • 23.
    Temporary data Very simpleto implement usage quota patterns (even better if done in Lua) It's possible to set self-expiring keys
  • 24.
    Redis as acache maxmemory 128mb maxmemory-policy allkeys-lru #save 900 1 #save 300 10 #save 60 10000 Expire keys individually or turn off persistence and use Redis as a cache system with automatic eviction Multiple levels of cache by using Redis on the webserver/ middleware layer http://wiki.nginx.org/HttpRedis https://github.com/jodosha/redis-store
  • 25.
    Redis as aPUBSUB system
  • 26.
  • 27.
    Shameless self promotion Ifyou enjoyed this presentation, please thank me by registering on http://teowaki.com It's a site for developers, you can hang around for free, and I think it's quite cool <3 <3 <3 Javier Ramírez @supercoco9