Redis is an open source, advanced key-value store. It is often referred to as a data structure server, since the keys can contain strings, hashes, lists, sets and sorted sets. Redis is use in building high performance, scalable web applications.
Redis is written in C language.
Following are the key features of Redis:
● Redis holds its database entirely in the memory, using the disk only for persistence which makes it much faster.
● Redis has a relatively rich set of data types when compared to many key-value data stores.
● Redis can easily replicate data to any number of slaves.
Following are the advantages of Redis:
● Redis natively supports most of the datatypes so it is easy to integrate.
● Redis is very fast as it holds its database in memory.
● All Redis operations are atomic, which ensures that if two clients concurrently access, Redis server will receive the updated value.
● Redis is a multi-utility tool and can be used in a number of use cases such as caching, messaging-queues etc.
Following commands can be use to install Redis on Ubuntu:
$sudo apt-get update $sudo apt-get install redis-server
Following command can be use to set Redis configurations:
redis 127.0.0.1:6379> CONFIG GET CONFIG_SETTING_NAME
Following command can be use to edit Redis configurations:
redis 127.0.0.1:6379> CONFIG SET CONFIG_SETTING_NAME NEW_CONFIG_VALUE
Strings in Redis are binary safe means they have a known length not determined by any special terminating characters. Thus, you can store anything up to 512 megabytes in one string.
Redis hash is a collection of key value pairs. Redis Hashes are maps between string fields and string values. So, they are used to represent objects.
Every hash can store up to 2 power(32) - 1 field-value pairs.
HMSET is use to set the hash whereas HGETALL is use to get the value of the hash.
Redis Lists are simply lists of strings, sorted by insertion order. Elements can be added to a Redis List on the head or on the tail.
Redis List can be set using lpush command whereas this list can be access via lrange command.
Redis Sets are an unordered collection of strings. In Redis sets, values can be added, removed, and tested for the existence of members in O(1) time complexity.
Redis Set can be set using sadd command whereas this Set can be accessed via smembers command.
Redis Sorted Sets are similar to Redis Sets, non-repeating collections of Strings. The difference is, every member of a Sorted Set is associated with a score, that is used in order to take the sorted set ordered, from the smallest to the greatest score. While members are unique, the scores may be repeated.
Redis Sorted Set can be set using zadd command whereas this sorted Set can be accessed via ZRANGEBYSCORE command.
To run commands on Redis remote server, it need to connect to the server by client redis-cli. Below is the syntax:
$ redis-cli -h host -p port -a password
Redis keys commands are used for managing keys in Redis. Following is the syntax for using redis keys commands.
redis 127.0.0.1:6379> COMMAND KEY_NAME
DEL command is use to delete the keys in Redis whereas SET command is use to set the keys.
In Redis EXPIRE key command sets the expiry of the key after the specified time.
In Redis EXPIREAT sets the expiry of the key after the specified time. Here time is in Unix timestamp format.
KEYS pattern command finds all keys matching the specified pattern.
MOVE key command in Redis is use to move a key to another database.
PERSIST key command in Redis is use to removes the expiration from the key.
PTTL key command in Redis is use to gets the remaining time in keys expiry in milliseconds.
RANDOMKEY command in Redis is use to return a random key from Redis.
RENAMENX command in Redis is use to rename the key, if a new key doesn't exist.
GETRANGE command in Redis is use to get a substring of the string stored at a key.
GETBIT command in Redis is use to return the bit value at the offset in the string value stored at the key.
MGET command in Redis is use to get the values of all the given keys.
SETBIT command in Redis is use to sets or clears the bit at the offset in the string value stored at the key.
SETEX command in Redis is use to sets the value with the expiry of a key.
SETNX command in Redis is use to sets the value of a key, only if the key does not exist.
MSET command in Redis is use to set multiple keys to multiple values.
MSETNX command in Redis is use to set multiple keys to multiple values, only if none of the keys exist.
PSETEX command in Redis is use to set the value and expiration in milliseconds of a key.
INCR command in Redis is use to increment the integer value of a key by one.
INCRBY command in Redis is use to increment the integer value of a key by the given amount.
INCRBYFLOAT command in Redis is use to increment the float value of a key by the given amount.