Redis

Redis is an in-memory database, good to be used as

  • a cache
  • a message queue


Installation

Ubuntu

Installing Redis on ubuntu

  • sudo apt-get install redis-tools
  • default port is 6379
  • Use redis-cli to enter redis commands
  • if you need redis instance be outside, do nano /etc/redis/redis.conf and replace bind 127.0.0.1 to bind 0.0.0.0
  • restart the server service redis-server restart
  • to check if you can connect to redis from another server, do redis-cli -h 192.168.x.x ping, should get PONG in return


Python

Python library:

  • sudo pip install redis or
  • sudo easy_install install redis or


Building Blocks

Databases

  • Each redis server can have several databases
  • they are indexed by integers: 0, 1, 2, ...
  • 0 is the default db
  • to choose the 1st db, use select 1
  • redis keeps all the data in memory, but by default, redis also snapshots DB to disk after each $X$ changes
  • there's also append-only mode


Key/Value Pairs

String key/values

  • key can be anything, e.g. users:ololo, : doesn't have any specific meaning, but can be used to separate "table name" and "key name"
  • command set users:ololo '{"name":"ololo", ...}' - the content can be any string, not necessarily JSON
  • to get the value, use get users:ololo
  • you can query only by key, never by value


String functions:

  • strlen key returns the lenth of the value associated with key key
  • getrange key 0 10 returns the substring from 0 to 10 of the value stored by key
  • append key value2 appends 'value2' to the current value stored by key


Counters

  • You can store counters in redis
  • incr key increment the integer value stored in key, or create a new key with value 1
  • decr key decrement the value
  • incrby and decrby are used to increase/decrease the values by some number


Bitmaps

  • you can store byte arrays in redis and set/get their specific bits
  • commands: setbit and getbit
  • this can be used for implementing Sketching Algorithms, e.g. Bloom Filters


Hashes

  • hashes in redis are associative arrays
  • to store a hash, not a string, use hset and hget
  • hset hashname field 30 is approximately equal to hashname[field] = 30
  • if hash hashname doesn't exist, it will be created. If exists, but field doesn't, field will be created
  • hgetall hashname returns all fields of the hash hashname
  • hdel hashname field removes field from hashname


Lists

  • lists keep an array of values associated with a specific key
  • lpush listname new_value adds the new value to the front of listname
  • ltrim listname 0 49 removes first 50 values from the list
  • lrange listname 0 9 returns first 10 values


Sets

Unordered sets

  • sadd set1 val1 val2 val3 adds values "val1" "val2" "val3" to set setkey
  • smember set1 val1
  • sinter set1 set2 intersection of two sets set1 and set2
  • sinterstore set1 set2 new_set store instersection of two sets in a new set new_set


Ordered sets

  • the same commands, but they start with "z":
  • zadd, zmember, ...


Usage Patterns

  • in redis it's fine to issue several requests ("round trips" to db) - because it's so fast


Avoiding Duplication

  • suppose users can log in using their ids or emails - so you have two keys for the same user
  • how can you avoid storing the same user twice?
  • first, keep the user data at user:id, e.g. users:9001
  • use hash to lookup the id for email: hget user:emails ololo@mlwiki.org


Expiration

  • you can set time to live of a key
  • expire key 30 where 30 is the time to live in seconds
  • expireat key unix_timestamp
  • ttl key to see how much time the key has left to live


Publish/Subscribe

  • it's easy to implement the Publish-Subscribe Model in redis - which makes it a lightweight message queue
  • blpop and brpop - return and remove first (last) element of a list - or blocks until something is avialable
  • can use it to implement a Queue
  • also can use subscribe q_id and publish q_it message


A P/S model implemented in Scala: https://gist.github.com/debasishg/7056696


Lua Scripting

  • finally, it's possible to write stored procedures in Lua


Sources