Need an expert to help you with
" Installing and Using Redis" ?
" Installing and Using Redis" ?
Installing and Using Redis
About RedisRedis, developed in 2009, is a flexible, open-source, key value data store. Following in the footsteps of other NoSQL databases such as Cassandra, CouchDB, and MongoDB, Redis allows the user to store vast amounts of data without the limits of a relational database. Additionally, Redis has also been compared to memcache and can be used with its basic elements as a cache with persistence.
SetupBefore you get started on installing redis, there are a couple of prerequisites that would have to be downloaded if you would like to make the installation as easy as possible. Begin by updating all of the apt-get packages.
sudo apt - get updateAfter the process is finished, download a compiler with build essential; this will help you install Redis from source.
sudo apt - get install build - essentialNow, you have to download tcl.
sudo apt-get install tcl8.5
Installing RedisOnce all of the prerequisites and dependencies have been downloaded into the server, you ca start installing Redis from source. Download the latest stable release tarball from Redis.io .
wget http://download.redis.io/releases/redis-stable.tar.gzUntar it and switch over into that directory.
tar xzf redis-stable.tar.gz cd redis-stableContinue using the make command.
makeExecute the recommended make test.
make testFinish it by executing ‘make install’, that will install the program system-wide.
sudo make installRedis will come with a built in script which sets Redis to run as a background daemon. To access the script, navigate to the ‘utils’ directory.
cd utilsOnce you are there, execute the ‘Ubuntu/Debian Install’ script.
sudo ./install_server.shWhile the script runs, you may select the default options by pressing ‘Enter’. After the script completes, the redis-server should be running in the background. You should now be able to start and stop Redis using these commands; the number will depend on the port you set while doing the installation 6379 is the default port setting.
sudo service redis_6379 startsudo service redis_6379 stopNow you can access the Redis database by entering the command below.
redis-cli RedisThis should now be installed and running. The prompt is going to look something like the below.
redis 127.0.0.1:6379>To tell Redis to automatically start at boot, execute the following command.
sudo update-rc.d redis_6379 defaults
Securing RedisAs default the Redis server will allow connections from pretty much anywhere that is considered insecure. Binding to ‘localhost’ should restrict access to the server itself and is a good start in protecting your server. Open the Redis configuration file to modify it.
sudo nano /etc/redis/6379.confLook for this line and ensure its uncommented, remove the ‘#’ in case it exists.
bind 127.0.0.1Redis Operations An easy command to append information to a string (the most basic redis datatype) might look like the below.
> SET users:AdamJones "job: computer engineer, born:1983, dislikes: apples" OKIn our case, the command ‘set’ is followed by the key (users:adamjones) and then the value (the string itself). Colons in Redis will have no bearing on its operations. However, they can prove to be very useful in describing the key to filled. We will receive the details of the new string using the command ‘GET’.
GET users:AdamJones "job: Computer Engineer, born:1983, dislikes: apples"Ranges: After receiving data, you may define the range using 2 parameters: the first and the last element (the first element is considered 0). If your end parameter is ‘-1’, all of the elements through the end of the list are going to be included. As an example, if a list is contained with the 6 colors of the rainbow and organized with the classic ROYGBV, you should be seeing the results shown in the following: > LRANGE ROYGBV 0 3 1) “red” 2) “orange” 3) “yellow” 4) “green” > LRANGE ROYGBV 0 -1 1) “red” 2) “orange” 3) “yellow” 4) “green” 5) “blue” 6) “violet” > LRANGE ROYGBV 3 -1 1) “green” 2) “blue” 3) “violet” Expiration: Redis has proved to be very helpful with storing information, as it is able to also be used to systematically expire data. The time that a key should exist can be selected/named either in seconds or with a Unix Time stamp (seconds since 1/1/1970). There are a few helpful commands which are able to control expiration and they include ‘EXPIRE’ which will set the length of time that a key will exist, and ‘TTL’ which will present the time left before the key expires.
> SET classified:information "Secret Stuff" OK > EXPIRE classified:information 45 (integer) 1 > TTL classified:information (integer) 31 Trying to get the information after It’s been expired will result in “nil” > GET classified:information (nil)Incrementing: Redis has the capability to increment strings in its database in an atomic operation. If a process happens to be incrementing a value, there is no other command which is able to perform this at the same time and the numbers can stay consistent across the database.
> SET population 6 OK > INCRBY population 10 (integer) 16 > INCR population (integer) 17Transactions: Redis has the option to also perform transactions, however, they must abide by two rules:
- The commands need to be done in order. They will not be stopped during the process by other requests.
- The transactions will be processed in their entirety.
> MULTI OK > SET population 6 QUEUED > INCRBY population 10 QUEUED > INCR population QUEUED redis 127.0.0.1:6379> EXEC 1) OK 2) (integer) 16 3) (integer) 1 4) (integer) 17Redis Data Types Redis has five data types: Strings, Sets, Sorted Sets, Lists, Hashes Strings Certain 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. As an example:
> SET newkey "the redis string begins" OK > GET newkey "the redis string begins"Sets If you would like to combine strings then you can do this using Redis sets, it is a collection of unordered strings. Certain 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 are helpful in many situations and, since every member of a set is unique, appending members to a set will not require a ‘check then add’ action. Instead, the set will check if the item is a duplicate at any time a ‘SADD’ command is performed.
> SADD colors red (integer) 1 redis 127.0.0.1:6379> SADD colors orange (integer) 1 redis 127.0.0.1:6379> SADD colors yellow (integer) 1 redis 127.0.0.1:6379> SADD colors orange (integer) 0 redis 127.0.0.1:6379> SMEMBERS colors 1) "red" 2) "yellow" 3) "orange"Sets are especially useful, for example, in checking for unique IP addresses visiting a page or extracting elements at random using the ‘SRANDMEMBER’ command. Sorted Sets Sorted sets should be created with an intuitive name; they are a collection of strings accompanied with a number and arranged by default in order of least to greatest. The datatype will work well with ranges and since is ordered from the outset, adding, remove, or updating values is done at ease. Some common commands for Sorted Sets are: ZADD: Appends members to a sorted set ZRANGE: Presents the members of a sorted set arranged by index (with the default low to high) ZREVRANGE: Presents the members of a sorted set arranged by index (from high to low) ZREM: Deletes members from a sorted set We will create a sample sorted set with the sizes (in square miles) of the world’s smallest countries.
> zadd countries 9 Tuvalu (integer) 1 > zadd countries 62 Liechtenstein (integer) 1 > zadd countries .7 Monaco (integer) 1 > zadd countries .2 VaticanCity (integer) 1 > zadd countries 107 Seychelles (integer) 1 redis 127.0.0.1:6379> zrange countries 0 -1 1) "VaticanCity" 2) "Monaco" 3) "Tuvalu" 4) "Liechtenstein" 5) "Seychelles"Lists Lists in Redis are a collection of ordered values in contrast to sets and they are unordered. You may append elements to each start or end of a list, even if there are over ten million elements in the list, with great speed. Any common commands accompanied with Lists are: LPUSH: Append a value to the beginning of a list RPUSH: Append a value to the end of a list LPOP: Get and deletes the first element in a list RPOP: Get and deletes the last element in a list LREM: Deletes elements from a list LRANGE: Get a range of elements from a list LTRIM: Edits a list so leave only a specified range You can make a list of people in charge to bring lunch each week.
> rpush lunch.provider adam (integer) 1 > rpush lunch.provider joe (integer) 2 > rpush lunch.provider lena (integer) 3 > rpush lunch.provider mike (integer) 4 > rpush lunch.provider axel (integer) 5If you would like to move someone to the front of the queue, use the ‘LPUSH’ command:
lpush lunch.provider zoe (integer) 6The ‘LRANGE’ command will then present our entire list.
lrange lunch.provider 0 -1 1) "zoe" 2) "adam" 3) "joe" 4) "lena" 5) "mike" 6) "axel"Lists are usually used to create a timeline of events or hold a collection of a selected number of elements. Hashes Hashes in Redis are usually a useful tool which represents objects with many fields. They are usually set up to store vast amount of field in a tiny amount of space. A hash is able to store more than 4 billion field-value pairs. Some common Hash commands are shown here: 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 You can use a hash to detail a sample site user.
> hmset user:1 username mikethompson password 54Adfs email [email protected] OK > hgetall user:1 1) "username" 2) "mikethompson" 3) "password" 4) "54Adfs" 5) "email" 6) "[email protected]"If you want to search for specific information, then ‘HMGET’ presents the values for only the requested fields.
> hmget user:1 username email 1) "mikethompson" "[email protected]"Conclusion Since the release of Redis, it has quickly gained popularity in a large amount, this has been harnessed by the likes of github, flickr, Disqus, and Craigslist. Additionally, Redis is used with most programming languages.
Updated on 02 Nov 2017
Add a comment