Saturday 25 October 2014

Redis

Redis Tutorial

Redis is an open source, BSD licensed, advanced key-value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets. Redis is written in c.
This tutorial will give you great understanding on Redis concepts needed to create and deploy a highly scalable and performance oriented system.

Redis - Overview

Redis is an open source, advanced key-value store and a serious solution for building high-performance, scalable web applications.
Redis has three main peculiarities that set it apart from much of its competition:
  • Redis holds its database entirely in memory, using the disk only for persistence.
  • Redis has a relatively rich set of data types when compared to many key-value data stores.
  • Redis can replicate data to any number of slaves.

Redis Advantages

  • Exceptionally Fast : Redis is very fast and can perform about 110000 SETs per second, about 81000 GETs per second.
  • Supports Rich data types : Redis natively supports most of the datatypes that most developers already know like list, set, sorted set, hashes. This makes it very easy to solve a variety of problems because we know which problem can be handled better by which data type.
  • Operations are atomic : All the Redis operations are atomic, which ensures that if two clients concurrently access Redis server will get the updated value.
  • MultiUtility Tool : Redis is a multi utility tool and can be used in a number of usecases like caching, messaging-queues (Redis natively supports Publish/ Subscribe ), any short lived data in your application like web application sessions, web page hit counts, etc.

Why Redis is different compared to other key-value stores?

  • Redis is a different evolution path in the key-value DBs where values can contain more complex data types, with atomic operations defined on those data types.
  • Redis is an in-memory but persistent on disk database, so it represents a different trade off where very high write and read speed is achieved with the limitation of data sets that can't be larger than memory. Another advantage of in memory databases is that the memory representation of complex data structures is much simpler to manipulate compared to the same data structure on disk, so Redis can do a lot, with little internal complexity.

 Redis - Environment

Install Redis on Ubuntu

To install the Redis on Ubuntu, go to terminal and type the following commands:
$sudo apt-get update
$sudo apt-get install redis-server
This will install redis on your machine.
Start Redis
$redis-server
Check if redis is working?
$redis-cli
This will open a redis prompt, as shown below:
redis 127.0.0.1:6379>
In the above prompt 127.0.0.1 is your machine's IP address and 6379 is port on which redis server is running. Now type the PING command as shown below.
redis 127.0.0.1:6379> ping
PONG
This shows that you have successfully installed redis on your machine.

Install Redis Desktop Manager on Ubuntu

To install redis dessktop manager on ubuntu, just download the package from http://redisdesktop.com/download Open the downloaded package and install it.
Redis desktop manager will give you UI to manage your redis keys and data.

Redis - Configuration

Redis Configuration

In Redis there is configuration file (redis.conf) available at root directory of redis. Although you can get and set all redis configurations by redis CONFIG command.

Syntax

Basic syntax of redis CONFIG command is shown below:
redis 127.0.0.1:6379> CONFIG GET CONFIG_SETTING_NAME

Example

redis 127.0.0.1:6379> CONFIG GET loglevel

1) "loglevel"
2) "notice"

To get all configuration settings just use * in place of CONFIG_SETTING_NAME

Example

redis 127.0.0.1:6379> CONFIG GET *

  1) "dbfilename"
  2) "dump.rdb"
  3) "requirepass"
  4) ""
  5) "masterauth"
  6) ""
  7) "unixsocket"
  8) ""
  9) "logfile"
 10) ""
 11) "pidfile"
 12) "/var/run/redis.pid"
 13) "maxmemory"
 14) "0"
 15) "maxmemory-samples"
 16) "3"
 17) "timeout"
 18) "0"
 19) "tcp-keepalive"
 20) "0"
 21) "auto-aof-rewrite-percentage"
 22) "100"
 23) "auto-aof-rewrite-min-size"
 24) "67108864"
 25) "hash-max-ziplist-entries"
 26) "512"
 27) "hash-max-ziplist-value"
 28) "64"
 29) "list-max-ziplist-entries"
 30) "512"
 31) "list-max-ziplist-value"
 32) "64"
 33) "set-max-intset-entries"
 34) "512"
 35) "zset-max-ziplist-entries"
 36) "128"
 37) "zset-max-ziplist-value"
 38) "64"
 39) "hll-sparse-max-bytes"
 40) "3000"
 41) "lua-time-limit"
 42) "5000"
 43) "slowlog-log-slower-than"
 44) "10000"
 45) "latency-monitor-threshold"
 46) "0"
 47) "slowlog-max-len"
 48) "128"
 49) "port"
 50) "6379"
 51) "tcp-backlog"
 52) "511"
 53) "databases"
 54) "16"
 55) "repl-ping-slave-period"
 56) "10"
 57) "repl-timeout"
 58) "60"
 59) "repl-backlog-size"
 60) "1048576"
 61) "repl-backlog-ttl"
 62) "3600"
 63) "maxclients"
 64) "4064"
 65) "watchdog-period"
 66) "0"
 67) "slave-priority"
 68) "100"
 69) "min-slaves-to-write"
 70) "0"
 71) "min-slaves-max-lag"
 72) "10"
 73) "hz"
 74) "10"
 75) "no-appendfsync-on-rewrite"
 76) "no"
 77) "slave-serve-stale-data"
 78) "yes"
 79) "slave-read-only"
 80) "yes"
 81) "stop-writes-on-bgsave-error"
 82) "yes"
 83) "daemonize"
 84) "no"
 85) "rdbcompression"
 86) "yes"
 87) "rdbchecksum"
 88) "yes"
 89) "activerehashing"
 90) "yes"
 91) "repl-disable-tcp-nodelay"
 92) "no"
 93) "aof-rewrite-incremental-fsync"
 94) "yes"
 95) "appendonly"
 96) "no"
 97) "dir"
 98) "/home/deepak/Downloads/redis-2.8.13/src"
 99) "maxmemory-policy"
100) "volatile-lru"
101) "appendfsync"
102) "everysec"
103) "save"
104) "3600 1 300 100 60 10000"
105) "loglevel"
106) "notice"
107) "client-output-buffer-limit"
108) "normal 0 0 0 slave 268435456 67108864 60 pubsub 33554432 8388608 60"
109) "unixsocketperm"
110) "0"
111) "slaveof"
112) ""
113) "notify-keyspace-events"
114) ""
115) "bind"
116) ""

Edit configuration

To update configuration you can edit redis.conf file directly or can update configurations via CONFIG set command

Syntax

Basic syntax of CONFIG SET command is shown below:
redis 127.0.0.1:6379> CONFIG SET CONFIG_SETTING_NAME NEW_CONFIG_VALUE

Example

redis 127.0.0.1:6379> CONFIG SET loglevel "notice"
OK
redis 127.0.0.1:6379> CONFIG GET loglevel

1) "loglevel"
2) "notice"

Redis - Data Types
 Redis supports 5 types of data types, which are described below:

Strings

Redis string is a sequence of bytes. Strings in Redis are binary safe, meaning they have a known length not determined by any special terminating characters, so you can store anything up to 512 megabytes in one string.

Example

redis 127.0.0.1:6379> SET name "tutorialspoint"
OK
redis 127.0.0.1:6379> GET name
"tutorialspoint"
In the above example SET and GET are redis commands, name is the key used in redis and tutorialspoint is the string value that is stored in redis.
NOTE: A String value can be at max 512 Megabytes in length.

Hashes

A 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

Example

redis 127.0.0.1:6379> HMSET user:1 username tutorialspoint password tutorialspoint points 200
OK
redis 127.0.0.1:6379> HGETALL user:1

1) "username"
2) "tutorialspoint"
3) "password"
4) "tutorialspoint"
5) "points"
6) "200"

In the above example hash data type is used to store user's object whichh contais basic information of user. Here HMSET, HEGTALL are commands for redis while user:1 is the key.
Every hash can store up to 232 - 1 field-value pairs (more than 4 billion).

Lists

Redis Lists are simply lists of strings, sorted by insertion order. You can add elements to a Redis List on the head or on the tail.

Example

redis 127.0.0.1:6379> lpush tutoriallist redis
(integer) 1
redis 127.0.0.1:6379> lpush tutoriallist mongodb
(integer) 2
redis 127.0.0.1:6379> lpush tutoriallist rabitmq
(integer) 3
redis 127.0.0.1:6379> lrange tutoriallist 0 10

1) "rabitmq"
2) "mongodb"
3) "redis"

The max length of a list is 232 - 1 elements (4294967295, more than 4 billion of elements per list).

Sets

Redis Sets are an unordered collection of Strings. In redis you can add, remove, and test for existence of members in O(1) time complexity.

Example

redis 127.0.0.1:6379> sadd tutoriallist redis
(integer) 1
redis 127.0.0.1:6379> sadd tutoriallist mongodb
(integer) 1
redis 127.0.0.1:6379> sadd tutoriallist rabitmq
(integer) 1
redis 127.0.0.1:6379> sadd tutoriallist rabitmq
(integer) 0
redis 127.0.0.1:6379> smembers tutoriallist

1) "rabitmq"
2) "mongodb"
3) "redis"

NOTE: In the above example rabitmq is added twice but due to unique property of set it is added only once.
The max number of members in a set is 232 - 1 (4294967295, more than 4 billion of members per set).

Sorted Sets

Redis Sorted Sets are, similarly to Redis Sets, non repeating collections of Strings. The difference is that every member of a Sorted Set is associated with score, that is used in order to take the sorted set ordered, from the smallest to the greatest score. While members are unique, scores may be repeated.

Example

redis 127.0.0.1:6379> zadd tutoriallist 0 redis
(integer) 1
redis 127.0.0.1:6379> zadd tutoriallist 0 mongodb
(integer) 1
redis 127.0.0.1:6379> zadd tutoriallist 0 rabitmq
(integer) 1
redis 127.0.0.1:6379> zadd tutoriallist 0 rabitmq
(integer) 0
redis 127.0.0.1:6379> ZRANGEBYSCORE tutoriallist 0 1000

1) "redis"
2) "mongodb"
3) "rabitmq"


Redis - Commands

Redis commands are used to perform some operations on redis server.
To run commands on redis server you need a redis client. Redis client is available in redis package, which we have installed earlier.

Syntax

Basic syntax of redis client is as follows:
$redis-cli

Example

Following example explains how we can start redis client.
To start redis client, open terminal and type the command redis-cli. This will connect to your local server and now you can run any command.
$redis-cli
redis 127.0.0.1:6379>
redis 127.0.0.1:6379> PING

PONG

In the above example we connect to redis server running on local machine and executes a command PING, that checks whether server is running or not.

Run commands on remote server

To run commands on redis remote server you need to connect to server by same client redis-cli

Syntax

$ redis-cli -h host -p port -a password

Example

Following example shows how to connect to redis remote server running on host 127.0.0.1, port 6379 and has password mypass.
$redis-cli -h 127.0.0.1 -p 6379 -a "mypass"
redis 127.0.0.1:6379>
redis 127.0.0.1:6379> PING
PONG


Redis - Keys

Redis keys commands are used for managing keys in redis. Syntax for using redis keys commands is shown below:

Syntax

redis 127.0.0.1:6379> COMMAND KEY_NAME

Example

redis 127.0.0.1:6379> SET tutorialspoint redis
OK
redis 127.0.0.1:6379> DEL tutorialspoint
(integer) 1
In the above example DEL is the command, while tutorialspoint is the key. If the key is deleted, then output of the command will be (integer) 1, otherwise it will be (integer) 0

Redis keys commands

Below given table shows some basic commands related to keys:
S.N.Command & Description
1DEL key
This command deletes the key, if exists
2DUMP key
This command returns a serialized version of the value stored at the specified key.
3EXISTS key
This command checks whether the key exists or not.
4EXPIRE key seconds
Expires the key after the specified time
5EXPIREAT key timestamp
Expires the key after the specified time. Here time is in Unix timestamp format
6PEXPIRE key milliseconds
Set the expiry of key in milliseconds
7PEXPIREAT key milliseconds-timestamp
Set the expiry of key in unix timestamp specified as milliseconds
8KEYS pattern
Find all keys matching the specified pattern
9MOVE key db
Move a key to another database
10PERSIST key
Remove the expiration from the key
11PTTL key
Get the remaining time in keys expiry in milliseconds.
12TTL key
Get the remaining time in keys expiry.
13RANDOMKEY
Return a random key from redis
14RENAME key newkey
Change the key name
15RENAMENX key newkey
Rename key, if new key doesn't exist
16TYPE key
Return the data type of value stored in key.

Redis - Strings

Redis strings commands are used for managing string values in redis. Syntax for using redis string commands is shown below:

Syntax

redis 127.0.0.1:6379> COMMAND KEY_NAME

Example

redis 127.0.0.1:6379> SET tutorialspoint redis
OK
redis 127.0.0.1:6379> GET tutorialspoint
"redis"
In the above example SET and GET are the command, while tutorialspoint is the key.

Redis strings commands

Below given table shows some basic commands to manage strings in redis:
S.N.Command & Description
1SET key value
This command sets the value at the specified key
2GET key
Get the value of a key.
3GETRANGE key start end
Get a substring of the string stored at a key
4GETSET key value
Set the string value of a key and return its old value
5GETBIT key offset
Returns the bit value at offset in the string value stored at key
6MGET key1 [key2..]
Get the values of all the given keys
7SETBIT key offset value
Sets or clears the bit at offset in the string value stored at key
8SETEX key seconds value
Set the value with expiry of a key
9SETNX key value
Set the value of a key, only if the key does not exist
10SETRANGE key offset value
Overwrite part of a string at key starting at the specified offset
11STRLEN key
Get the length of the value stored in a key
12MSET key value [key value ...]
Set multiple keys to multiple values
13MSETNX key value [key value ...]
Set multiple keys to multiple values, only if none of the keys exist
14PSETEX key milliseconds value
Set the value and expiration in milliseconds of a key
15INCR key
Increment the integer value of a key by one
16INCRBY key increment
Increment the integer value of a key by the given amount
17INCRBYFLOAT key increment
Increment the float value of a key by the given amount
18DECR key
Decrement the integer value of a key by one
19DECRBY key decrement
Decrement the integer value of a key by the given number
20APPEND key value
Append a value to a key

Redis - Hashes

Redis Hashes are maps between string fields and string values, so they are the perfect data type to represent objects
In redis every hash can store up to more than 4 billion field-value pairs.

Example

redis 127.0.0.1:6379> HMSET tutorialspoint name "redis tutorial" description "redis basic commands for caching" likes 20 visitors 23000
OK
redis 127.0.0.1:6379> HGETALL tutorialspoint

1) "name"
2) "redis tutorial"
3) "description"
4) "redis basic commands for caching"
5) "likes"
6) "20"
7) "visitors"
8) "23000"

In the above example we have set redis tutorials detail (name, description, likes, visitors) in hash named tutorialspoint

Redis hash commands

Below given table shows some basic commands related to hash:
S.N.Command & Description
1HDEL key field2 [field2]
Delete one or more hash fields
2HEXISTS key field
Determine whether a hash field exists or not
3HGET key field
Get the value of a hash field stored at specified key
4HGETALL key
Get all the fields and values stored in a hash at specified key
5HINCRBY key field increment
Increment the integer value of a hash field by the given number
6HINCRBYFLOAT key field increment
Increment the float value of a hash field by the given amount
7HKEYS key
Get all the fields in a hash
8HLEN key
Get the number of fields in a hash
9HMGET key field1 [field2]
Get the values of all the given hash fields
10HMSET key field1 value1 [field2 value2 ]
Set multiple hash fields to multiple values
11HSET key field value
Set the string value of a hash field
12HSETNX key field value
Set the value of a hash field, only if the field does not exist
13HVALS key
Get all the values in a hash
14HSCAN key cursor [MATCH pattern] [COUNT count]
Incrementally iterate hash fields and associated values