REDIS

"an advanced key-value store"

Compiegne.rb - 2012-12-20

Philippe Lafoucrière / Tech-Angels / @plafoucriere

What is Redis?

From Redis homepage:

Redis is an open source, 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

Developed by Salvatore Sanfilippo / @antirez

Redis means REmote DIctionary Server.

What is really Redis?

  • A simple and lightweight daemon (Written in C).
  • In-memory database that persists on disk.
  • Incredibly fast
  • Atomic operations
  • Actively maintained

Supported data types

  • Strings
  • Hashes
  • Lists
  • Sets
  • Sorted Sets

Special features

Pub/Sub

"Publish–subscribe pattern"

Transactions

Using the MULTI and EXEC commands, it is possible to send several commands at once to Redis.
Anyway, Redis transactions are very different from SQL transactions: In case of failure, there is NO rollback. Commands are evaluated during queue process, and EXEC will refuse to run.

Scripting

"EVAL and EVALSHA are used to evaluate scripts using the Lua interpreter built into Redis starting from version 1.6.0."

Basic Commands


redis 127.0.0.1:6379[1]> set mykey "compiegne RB"
OK
redis 127.0.0.1:6379[1]> get mykey
"compiegne RB"
            

>> require 'rubygems'
=> false
>> require 'redis'
=> true
>> r = Redis.new
=> #
>> r.ping
=> "PONG"
>> r.set('foo','bar')
=> "OK"
>> r.get('foo')
=> "bar"
            

Hashes


> hset configuration secret_key donttellanyone
(integer) 0
> hget configuration secret_key
"donttellanyone"
              

Lists


> rpush my_queue "do_the_job:[123,\"another_params\"]"
(integer) 1
> rpop my_queue
"do_the_job:[123,\"another_params\"]"
> rpop my_queue
(nil)
              

Sets


> sadd user:123 "Dark Knight"
(integer) 1
> sadd user:123 "Abyss"
(integer) 1
> sadd user:123 "Abyss"
(integer) 0
> smembers user:123
1) "Abyss"
2) "Dark Knight"
> sadd user:456 "Dark Knight"
(integer) 1
> sadd user:456 "Paul"
(integer) 1
> sinter user:123 user:456
1) "Dark Knight"
              

Common use cases

  • Counters
  • Queues
  • Cache
  • Chat / Real-time
  • Recommendation systems
  • Online users/friends
  • Statistics
  • Centralized configuration for clusters

Counters


> incr package:123:downloads
(integer) 1
> incr package:123:downloads
(integer) 2
> incr package:123:downloads
(integer) 3
> incrby package:123:downloads 3
(integer) 6
              
Time complexity: O(1)

Message Queue

Connection A:


> blpop job_queue 10
1) "job_queue"
2) "do_the_job:[123,\"another_params\"]"
(2.87s)
              
Connection B:

> rpush job_queue "do_the_job:[123,\"another_params\"]"
(integer) 1
> rpush job_queue "do_the_job:[456,\"another_params\"]"
(integer) 1
> rpush job_queue "do_the_job:[789,\"another_params\"]"
(integer) 2
              
Time complexity: O(1)

Online Friends

Set list of friends:


> sadd user:123:friends 456 789
(integer) 2
> smembers user:123:friends
1) "456"
2) "789"
              
After sign-in, add user id to a set (1 set every 10 minutes):

> sadd online_users:2012-12-20-20:00 123 789
(integer) 2
> sinter  user:123:friends online_users:2012-12-20-20:00
1) "789"
              
Time complexity: O(1)

Drawbacks

  • All data will reside in MEMORY
  • Persistence requires Redis RAM X3
  • No Map/Reduce
  • Another component in your architecture
  • No authentication
  • No named bases

Online Resources