A Brief Introduction to Redis
Charles Anderson !
Salem Dynamic Languages User Group January 2014
Overview
• Redis is a networked “data structure server”
• Fast, powerful, simple
Abstract Data Types
• In the Before Time, we didn’t have classes and objects - we had abstract data types
• and, we liked it!
• Procedural interface
• First (or last) parameter is the ADT
Example: Stack
• push(stack, item)
• pop(stack): item
• size(stack): int
• etc.
Stack ADT
• We had a single, global name space. So:
• stack_push(stack, item)
• stack_pop(stack)
• stack_size(stack)
• This is what the Redis API looks like
Redis Types
• String - scalar value
• List
• Set
• Sorted Set
• Hash
Redis List
• A small subset of the operations:
• LPUSH key value
• LPOP key
• LLEN key
• LRANGE key start stop
Redis API
• Every object has a name/key
• First parameter to each call
• Every object has a type
• Operation must match the type of object
Using API via CLI$ redis-cliredis 127.0.0.1:6379> lpush mylist 1(integer) 1redis 127.0.0.1:6379> lpush mylist cat(integer) 2redis 127.0.0.1:6379> lpush mylist 3.14(integer) 3redis 127.0.0.1:6379> llen mylist(integer) 3redis 127.0.0.1:6379> lrange mylist 0 11) "3.14"2) "cat"redis 127.0.0.1:6379> lrange mylist 0 -11) "3.14"2) "cat"3) "1"
Redis Clients
• The wire protocol for Redis is dead-simple
• Therefore, lots of clients for lots of languages
Ruby Client Demo
irb(main):015:0> r.lpush('mylist', 1)=> 1irb(main):016:0> r.lpush('mylist', 'cat')=> 2irb(main):017:0> r.lpush('mylist', 3.14)=> 3irb(main):019:0> r.lrange('mylist', 0, -1)=> ["3.14", "cat", "1"]
Complete Redis API
• http://redis.io/commands
• Each command includes the time complexity of the operation - unique
Set
• SADD key member, SREM key member
• SCARD key
• SISMEMEBER key member
• SINTER, SUNION
Sorted Set
• ZADD key score member
• ZSCORE key member
• ZRANK key member
• ZRANGE key start stop
• Use case: leader board
Hash
• HSET key field value
• HGET key field
• HEXISTS key field, HKEYS, HLEN
• Use case: a structured record like User
Other Stuff
• Key Expiration
• Transactions/batches
• Publish-Subscribe
• Server-side scripting via Lua
• Replication
• Clustering
Implementation Details
• Everything is stored in memory
• Hella fast
• Not memcache - expiration is not LRU
• Persistent - tunable disk storage
• Single-threaded - simple, fast, single CPU
• Support (@antirez) is awesome
But, is it Web Scale?
• NoSQL key/value store
• Kickass benchmarks
• Simpler (smaller scope) than HBase or Cassandra
• Sharding done by clients
• Not usually a system of record
• I already work on a farm
More Information
• http://redis.io
• “The Little Redis Book” Seguin
• “Redis in Action” Carlson
• “Redis Cookbook” Macedo and Oliveira
Questions
• ?