Thursday, 22 January 2015

Redis - data types and abstractions

Introduction:

  • Redis is written in c language.
  • Redis is an open source, BSD licensed, advanced key-value store.
  • Redis is not a plain key-value store. It is a data structure server since keys can contain strings, hashes, lists, sets and sorted sets. 

 Redis vs Memcached:

  • Both are in-memory key-value data stores, belong to NoSQL family.
  • Both keep all data in RAM
  • With respect to throughput and latency both exhibit identical characteristics.

Differences:

  • Data eviction policy
  • Memcached limits key names to 250 bytes, values to 1MB
  • Memcached work only with plain strings
  • Redis allows key/value to be as large 512 MB
  • In redis, data stores isn't opaque, meaning that the server can manipulate id directly.

Redis Keys:

  • Redis key are binary safe, meaning that you can use any binary sequence as a key. ("foo", JPEG).
  • The empty string is also a valid key


How to Install Redis Server:

Setup:

1. sudo apt-get update
2. sudo apt-get install build-essential
3. sudo apt-get install tcl

Installing Redis:

Download the latest version 2.8.9
 
wget http://download.redis.io/releases/redis-2.8.9.tar.gz
 Untar it and switch into that directory:
tar xzf redis-2.8.9.tar.gz
cd redis-2.8.9
Proceed to with the make command:
make
Run the recommended make test:
make test
Finish up by running make install, which installs the program system-wide.
sudo make install

Once the program has been installed, Redis comes with a built in script that sets up Redis to run as a background daemon.
To access the script move into the utils directory:
cd utils
From there, run the Ubuntu/Debian install script:
sudo ./install_server.sh
As the script runs, you can choose the default options by pressing enter. Once the script completes, the redis-server will be running in the background.
You can start and stop redis with these commands (the number depends on the port you set during the installation. 6379 is the default port setting):
sudo service redis_6379 start
sudo service redis_6379 stop
You can then access the redis database by typing the following command:
redis-cli
You now have Redis installed and running. The prompt will look like this:
redis 127.0.0.1:6379> 

Redis key - value in detail

Redis Strings:


  • The Redis String type is the simplest type of value you can associate with a Redis key.
  • The SET and the GET commands are the way we set and retrieve a string value.
  • SET will replace any existing value already stored into the key
  • We may ask SET to fail if the key already exists, or the opposite, that it only succeed if the key already exists:

Eg 1:

> set mykey somevalue
OK
> get mykey
"somevalue"

Eg 2:

> set mykey newval nx
(nil)
> set mykey newval xx
OK

Eg 3:

> set counter 100
OK
> incr counter
(integer) 101

Commands : INCR, INCRBY, DECR & DECRBY

Eg 4:

> mset a 10 b 20 c 30
OK
> mget a b c
1) "10"
2) "20"
3) "30"

Altering and Querying the key space:


There are commands that are not defined on particular types, but are useful in order to interact with the space of keys, and thus, can be used with keys of any type.

> set mykey hello
OK
> exists mykey
(integer) 1
> del mykey
(integer) 1
> exists mykey
(integer) 0

Redis expires:

Set a timeout for a key, which is a limited time to live (TTL). When the time to live elapses, the key is automatically destroyed, exactly as if the user called the DEL command with the key.

Setting an expire is trivial:

> set key some-value
OK
> expire key 5
(integer) 1
> get key (immediately)
"some-value"
> get key (after some time)
(nil)

We can also create keys with expires using other Redis commands. For example using SET options:

> set key 100 ex 10
OK
> ttl key
(integer) 9


Redis List:

Redis lists are implemented via Linked Lists. This means that even if you have millions of elements inside a list, the operation of adding a new element in the head or in the tail of the list is performed in constant time. The speed of adding a new element with the LPUSH command to the head of a list with ten elements is the same as adding an element to the head of list with 10 million elements.

> rpush mylist A
(integer) 1
> rpush mylist B
(integer) 2
> lpush mylist first
(integer) 3
> lrange mylist 0 -1
1) "first"
2) "A"
3) "B"


Note that LRANGE takes two indexes, the first and the last element of the range to return. Both the indexes can be negative, telling Redis to start counting from the end: so -1 is the last element, -2 is the penultimate element of the list, and so forth.

Automatic Creation and Removal of keys:

It is Redis' responsibility to delete keys when lists are left empty, or to create an empty list if the key does not exist. This is not specific to lists, it applies to all the Redis data types.

Basically we can summarize the behavior with three rules:

  1. When we add an element to an aggregate data type, if the target key does not exist, an empty aggregate data type is created before adding the element.
  2. When we remove elements from an aggregate data type, if the value remains empty, the key is automatically destroyed.

Redis Hash:

Redis hashes look exactly how one might expect a "hash" to look, with field-value pairs:


> hset myhash key1 val1
(integer) 1
> hget myhash key1
"val1"
> hmset user username antirez birthyear 1977 verified 1
OK
> hget user username
"antirez"
> hget user birthyear
"1977"
> hgetall user
1) "username"
2) "antirez"
3) "birthyear"
4) "1977"
5) "verified"
6) "1"


Redis Sets:

Redis Sets are unordered collections of strings. The SADD command adds new elements to a set. It's also possible to do a number of other operations against sets like testing if a given element already exists, performing the intersection, union or difference between multiple sets, and so forth.

> sadd myset 1 2 3
(integer) 3
> smembers myset
1. 3
2. 1
3. 2