Redis is an advanced key-valued store, it is referred to as data-structure server. It is a No-SQL database. It is written in C language and development is sponsored by VMware. Redis is very easy to set up, and very fast and has an interesting approach to persistence. Redis runs in RAM , but syncs to disk every Y seconds or after every X operations. Redis server support master-slave replication. A data from redis-server is replicated to any number of slaves. A slave can be master of other slaves. Since the data is replicated , no risk of data loosing. It is very faster, it perform write operation better than read operation. It has non-blocking IO-operations and is single threaded.
How to setup redis-server
You can install redis directly from Source code. Code is available in Github. For ubuntu, install it by sudo apt-get install redis-server. Run the redis server by redis-server, we can specify our own configuration by passing additional parameter redis-server path-to/redis.conf. You can use redis-cli to play with Redis in command-line. When you run this without parameter conncect to local server. We can connect to remote server by providing extra argument eg:- redis-cli -h 127.0.0.1 -p 5369.
Redis uses key-value store. This means redis stored data as a value within a key. We can access the value using valid key. A valid key shouldn’t contain white space. Redis support following data types as values.
- Binary-safe strings.
- Lists of binary-safe strings.
- Sets of binary-safe strings, that are collection of unique unsorted elements. You can think at this as a Ruby hash where all the keys are set to the ‘true’ value.
- Sorted sets, similar to Sets but where every element is associated to a floating number score. The elements are taken sorted by score. You can think of this as Ruby hashes where the key is the +element and the value is the score, but where elements are always taken in order without requiring a sorting operation.
Binary-safe string means that values are essentially byte-string, that contains any byte including null byte.
Redis’s keys are binary safe, an empty string is accepted as valid key. Redis provides useful commands. TYPE is the command return type of the key that we passed as argument. It can be a string, list, set, zset, hash or none.
We will discuss few of the Redis data types and its commands .
SET is command is used to store a value at a key
SET user:name “Raju” # it will store value.
We can retrieve value using the GET command GET user:name #=> “Raju”
INCR is used to increment the value at specified key
SET no 12
INCR no #=> 13
INCR no #=> 14
INCR is short-cut of
SET no x
DEL is used to delete key and associated value.
We can specify the key should be available for certain time, for this EXPIRE and TTL command.
SET count 10
EXPIRE count 30
This means that count is deleted in 30 seconds.
TTL command test how long it exists.
TTL count #=> 11
if it returns -1, means it will never expire.
A list is series of ordered value. RPUSH is push the value at end of the list and LPUSH insert value at start of the list.
RPUSH emp “ASHA CM”
LPUSH emp “Sreejesh KP”
LPUSH emp “Shamith C”
LRANGE gives a subset of the list, It takes index of first element and index of last element in the list that you want to retrieve.
LRANGE emp 0 -1 #=> if last one is -1, the give full list from index we specified a start element.1) “SHAMITH C 2) “Sreejesh KP” 3) “ASHA CM”
LRANGE emp 2 3 #=> 1) “ASHA CM”
LRANGE emp 0 1 # => 1) “SHAMITH C” 2) “Sreejesh KP”
LLEN returns current length of lists. LPOP removes key and associated value from last. RPOP removes key and associated value from first.
LLEN emp #=> (integer) 3
LPOP emp #=> “SHAMITH C”
LLEN emp #=> (integer) 2
RPOP emp “ASHA cm”
LLEN emp #=> (integer) 1
It is similar to list,but no specific order.
SADD adds the given value to the set.
SADD user “Sreejith”
SADD user “Sherin”
SADD user “Shine”
SREM to removes specified value from set.
SREM user “Sherin”
SISMEMBER tests if the given value is in the set.
SISMEMBER user “Sherin” #=> false
SISMEMBER user “Shine” #=> true
SMEMBERS returns a list of all the members of this set.
SMEMBERS user #=> 1) “Shine” 2) “Sreejith”
SUNION combines two or more sets and return alll members.
Last data type is sorted set. It is similar to ordinary set, but now each value has an associated score.Score is used to sort element in list.
ZADD user 1913 abc
ZADD user 1914 cde
ZADD user 1915 efg
Here 1913, 1914,1915 are the score and abc, cde,efg are values.
ZRANGE user 0 -1 #=> 1) “qwe” 2) “qwr” 3) “qwt”
In short, we can use Redis straight away to do things. This will make the system more faster, simpler and the site more responsive. We don’t need to change the current setup, just need to start Redis server to do those things that were otherwise not possible, or hard, or too costly.