Redis is an open-source, networked, in-memory, key-value data store with optional durability. It is written in ANSI C. The development of Redis has been sponsored by Pivotal since May 2013 until then, it was sponsored by VMWare. According to the monthly ranking by DB-Engines.com, Redis is the most popular key-value store. Before you install redis, there are a couple of prerequisites that need to be downloaded to make the installation as easy as possible. Start off by updating all of the apt-get packages :
[code language="ruby"] sudo apt-get update [/code]
By default Ubuntu provides redis server installation by
[code language="ruby"] sudo apt-get install redis-server [/code]
And you done with the installation of redis server. But if you want to install latest version of redis server, then you can go through the rest of post.
[code language="ruby"] wget http://redis.googlecode.com/files/redis-2.2.12.tar.gz # You can specify a link of redis-server which you want to install tar -zxf redis-2.2.12.tar.gz cd redis-2.2.12 make install [/code]
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 :
[code language="ruby"] cd utils [/code]
From there, run the Ubuntu/Debian install script :
[code language="ruby"] sudo ./install_server.sh [/code]
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) :
[code language="ruby"] sudo service redis_6379 start sudo service redis_6379 stop [/code]
You can then access the redis database by typing the following command :
[code language="ruby"] redis-cli [/code]
You now have Redis installed and running. The prompt will look like this :
[code language="ruby"] redis 127.0.0.1:6379> [/code]
To set Redis to automatically start at boot, run :
[code language="ruby"] sudo update-rc.d redis_6379 defaults [/code]
Redis Operations A simple command to add information to a string (the most basic redis datatype) could look like this :
[code language="ruby"] > SET users:MadaArya "job: Programmer, born:1991, dislikes: you" OK [/code]
In this case the command SET is followed by the key (users:GeorgeWashington), and then the value (the string itself).Colons in Redis have no bearing on its operations. However, they can be useful in describing the key to be filled.
We can retrieve the details of the new string with the command “GET”
[code language="ruby"] > GET users:MadaArya "job: Programmer, born:1991, dislikes: you" [/code]
1. Redis Data Types
Redis has five data types: Strings, Sets, Sorted Sets, Lists, Hashes
Strings are Redis’ most basic data type.
Some common commands associated with strings are: SET: sets a value to a key GET: gets a value from a key DEL: deletes a key and its value INCR: atomically increments a key INCRBY: increments a key by a designated values EXPIRE: the length of time that a key should exist (denoted in seconds)
Strings can be used to store objects, arranged by key. For example :
[code langauge="ruby"] > SET newkey "41studio is cool company" OK > GET newkey "41studio is cool company" > DEL newkey (nil) [/code]
If you want to combine strings, you can do that with REDIS sets, a collection of unordered strings.
Some common commands for Sets are: SADD: Add one or members to a set SMEMBERS: Get all set members SINTER: Find the intersection of multiple sets SISMEMBER: check if a value is in a set SRANDMEMBER: Get a random set member
Sets can be helpful in a variety of situations. Because each member of a set is unique, adding members to a set does not require a “check then add” operation. Instead the set will check whether the item is a duplicate whenever a SADD command is performed.
[code language="ruby"] > SADD team mada (integer) 1 redis 127.0.0.1:6379 > SADD team helmi (integer) 1 redis 127.0.0.1:6379 > SADD team fina (integer) 1 redis 127.0.0.1:6379 > SADD team fajar (integer) 0 redis 127.0.0.1:6379 > SMEMBERS team 1) "helmi" 2) "mada" 3) "fajar" 4) "fina" [/code]
Sets can be especially useful, for example, in checking for unique IP addresses visiting a page, or extracting elements at random with the SRANDMEMBER command.
4. Sorted Sets
Sorted sets have an intuitive name: they are a collection of strings associated with a number and are arranged by default in order of least to greatest.
This datatype works well with ranges, and, because they are ordered from the outset, adding, remove, or updating values can be done quickly.
Some common commands for Sorted Sets are: ZADD: Adds members to a sorted set ZRANGE: Displays the members of a sorted set arranged by index (with the default low to high) ZREVRANGE: Displays the members of a sorted set arranged by index (from high to low) ZREM: Removes members from a sorted set
Lists in Redis are a collection of ordered values. This is in contrast to Sets which are unordered. You can add elements to the beginning or end of a list (even when there are over ten million elements in the list) with great speed.
Some common commands associated with Lists are: LPUSH: Add a value to the begining of a list RPUSH: Add a value to the end of a list LPOP: Get and remove the first element in a list RPOP: Get and remove the last element in a list LREM: Remove elements from a list LRANGE: Get a range of elements from a list LTRIM: Modifies a list so leave only a specified range
We can create a list of people assigned to bring lunch each week :
[code langaage="ruby"] > rpush lunch.provider alice (integer) 1 > rpush lunch.provider bob (integer) 2 > rpush lunch.provider carol (integer) 3 > rpush lunch.provider don (integer) 4 > rpush lunch.provider emily (integer) 5 [/code]
If we wanted to push someone to the front of the queue, we could use the LPUSH command :
[code language="ruby"] lpush lunch.provider zoe (integer) 6 [/code]
The LRANGE command would then display our whole list :
[code language="ruby"] lrange lunch.provider 0 -1 1) "zoe" 2) "alice" 3) "bob" 4) "carol" 5) "don" 6) "emily" [/code]
Lists can often be used to create a timeline of events or maintain a collection of a limited number of elements.
Hashes in Redis are a useful tool to represent objects with many fields. They are set up to store vast amount of fields in a small amount of space. A hash can store more than 4 billion field-value pairs.
Some common Hash commands are: HMSET: Sets up multiple hash values HSET: Sets the hash field with a string value HGET: Retrieves the value of a hash field HMGET: Retrieves all of the values for given hash fields HGETALL: Retrieves all of the values for in a hash
We can use a hash to describe a sample site user.
[code language="ruby"] > hmset user:1 username jsmith password 4bAc0s email [email protected] OK > hgetall user:1 1) "username" 2) "jsmith" 3) "password" 4) "4bAc0s" 5) "email" 6) "[email protected]" [/code]