Notes from https://www.tutorialspoint.com/redis/redis_overview in case of all-in-one page => https://www.tutorialspoint.com/redis/redis_quick_guide.htm
Redis is an open source, BSD licensed, 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 written in C.
Redis has three main peculiarities that sets it apart: Redis holds its database entirely in the 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: can perform about 110000 SETs per second, about 81000 GETs per second.
- Supports rich data types
- Operations are atomic: ensures that if two clients concurrently access, Redis server will receive the updated value.
- Multi-utility tool
Installation:
brew install redis
Start redis server:
redis-server
Check if it is installed correctly:
redis-cli
if redis prompt looks like below and you can ping then it is ok!
redis 127.0.0.1:6379> ping PONG
HELP:
```
127.0.0.1:6379> help
redis-cli 6.0.6
To get help about Redis commands type:
"help @" to get a list of commands in
"help " for help on
"help " to get a list of possible help topics
"quit" to exit
To set redis-cli preferences:
":set hints" enable online hints
":set nohints" disable online hints
Set your preferences in ~/.redisclirc
```
SYNTAX:
Following is the basic syntax of Redis CONFIG command.
redis 127.0.0.1:6379> CONFIG GET CONFIG_SETTING_NAME
Following is the basic syntax of CONFIG SET command.
```
redis 127.0.0.1:6379> CONFIG SET CONFIG_SETTING_NAME NEW_CONFIG_VALUE
```
STRINGS: Redis string is a sequence of bytes. Strings in Redis are binary safe. You can store anything up to 512 megabytes in one string.
```
redis 127.0.0.1:6379> SET name "hello_world"
OK
redis 127.0.0.1:6379> GET name
"hello_world"
```
HASHES: A Redis hash is a collection of key value pairs. Redis Hashes are maps between string fields and string values.
```
127.0.0.1:6379> HMSET user:1 username user_1 password 1234 points 200
OK
127.0.0.1:6379> HGETALL user:1
1) "username"
2) "user_1"
3) "password"
4) "1234"
5) "points"
6) "200"
127.0.0.1:6379>
```
In the above example, hash data type is used to store the user's object
which contains basic information of the user. Here HMSET, HGETALL 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.
```
redis 127.0.0.1:6379> lpush test_list redis
(integer) 1
redis 127.0.0.1:6379> lpush test_list mongodb
(integer) 2
redis 127.0.0.1:6379> lpush test_list rabitmq
(integer) 3
redis 127.0.0.1:6379> lrange test_list 0 10
1) "rabitmq"
2) "mongodb"
3) "redis"
redis 127.0.0.1:6379> lpop test_list
"rabitmq"
```
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 the existence of members in O(1) time complexity.
```
redis 127.0.0.1:6379> sadd test_set redis
(integer) 1
redis 127.0.0.1:6379> sadd test_set mongodb
(integer) 1
redis 127.0.0.1:6379> sadd test_set rabitmq
(integer) 1
redis 127.0.0.1:6379> sadd test_set rabitmq
(integer) 0
redis 127.0.0.1:6379> smembers test_set
1) "rabitmq"
2) "mongodb"
3) "redis"
```
The max number of members in a set is 232 - 1.
(4294967295, more than 4 billion of members per set).
SORTED SETS:
```
redis 127.0.0.1:6379> zadd test_sorted_set redis
(integer) 1
redis 127.0.0.1:6379> zadd test_sorted_set mongodb
(integer) 1
redis 127.0.0.1:6379> zadd test_sorted_set rabitmq
(integer) 1
redis 127.0.0.1:6379> zadd test_sorted_set rabitmq
(integer) 0
redis 127.0.0.1:6379> ZRANGEBYSCORE test_sorted_set
1) "redis"
2) "mongodb"
3) "rabitmq"
```
RUN COMMANDS ON THE REMOTE SERVER:
First set a password:
$ redis-cli 127.0.0.1:6379> CONFIG GET requirepass 127.0.0.1:6379> CONFIG SET requirepass password 127.0.0.1:6379> exit
Then you need to restart redis-server and you'll be able
to authenticate your password:
$ redis-cli -h host -p port -a password
KEYS: Redis keys commands are used for managing keys in Redis.
SYNTAX:
```
redis 127.0.0.1:6379> COMMAND KEY_NAME
redis 127.0.0.1:6379> SET key_test redis
OK
redis 127.0.0.1:6379> DEL key_test
(integer) 1
```
If the key is deleted, then the output of the command will be (integer) 1,
otherwise it will be (integer) 0.
COMMANDS:
DEL key
DUMP
EXISTS
EXPIRE
EXPIREAT
PEXPIRE
PEXPIREAT
KEYS
MOVE
PERSIST
PTTL
TTL
RANDOMKEY
RENAME
RENAMENX
TYPE
Their descriptions can be seen by following the sytax below:
```
redis 127.0.0.1:6379> help TTL
TTL key
summary: Get the time to live for a key
since: 1.0.0
group: generic
```
TYPES: Redis - Strings Redis - Hashes Redis - Lists Redis - Sets Redis - Sorted Sets Redis - HyperLogLog Redis - Publish Subscribe Redis - Transactions Redis - Scripting Redis - Connections Redis - Server
Their keys can be seen by following the sytax below:
```
redis 127.0.0.1:6379> help @Set
SADD key member [member ...]
summary: Add one or more members to a set
since: 1.0.0
SCARD key
summary: Get the number of members in a set
since: 1.0.0
SDIFF key [key ...]
summary: Subtract multiple sets
since: 1.0.0
SDIFFSTORE destination key [key ...]
summary: Subtract multiple sets and store the resulting set in a key
since: 1.0.0
SINTER key [key ...]
summary: Intersect multiple sets
since: 1.0.0
SINTERSTORE destination key [key ...]
summary: Intersect multiple sets and store the resulting set in a key
since: 1.0.0
SISMEMBER key member
summary: Determine if a given value is a member of a set
since: 1.0.0
SMEMBERS key
summary: Get all the members in a set
since: 1.0.0
SMOVE source destination member
summary: Move a member from one set to another
since: 1.0.0
SPOP key [count]
summary: Remove and return one or multiple random members from a set
since: 1.0.0
SRANDMEMBER key [count]
summary: Get one or multiple random members from a set
since: 1.0.0
SREM key member [member ...]
summary: Remove one or more members from a set
since: 1.0.0
SSCAN key cursor [MATCH pattern] [COUNT count]
summary: Incrementally iterate Set elements
since: 2.8.0
SUNION key [key ...]
summary: Add multiple sets
since: 1.0.0
SUNIONSTORE destination key [key ...]
summary: Add multiple sets and store the resulting set in a key
since: 1.0.0
```
REDIS BENCHMARK:
Redis benchmark is the utility to check the performance of
Redis by running n commands simultaneously.
```
$redis-benchmark -h 127.0.0.1 -p 6379 -t set,sadd,zadd,lpush -n 100000 -q
SET: 139664.80 requests per second
LPUSH: 140845.08 requests per second
SADD: 141643.06 requests per second
```
PIPELINING:
The basic meaning of pipelining is, the client can send multiple requests to
the server without waiting for the replies at all,
and finally reads the replies in a single step.
```
$(echo -en "PING\r\n SET tutorial redis\r\nGET tutorial\r\nINCR
visitor\r\nINCR visitor\r\nINCR visitor\r\n"; sleep 10) | nc localhost 6379
+PONG
+OK
redis
:1
:2
:3
```
The benefit of this technique is a drastically improved protocol performance.
The speedup gained by pipelining ranges from a factor of five for connections
to localhost up to a factor of at least one hundred over slower internet connections.
PARTITIONING:
Partitioning is the process of splitting your data into multiple Redis instances,
so that every instance will only contain a subset of your keys.
Types of Partitioning:
There are two types of partitioning available in Redis.
Suppose we have four Redis instances, R0, R1, R2, R3 and
many keys representing users like user:1, user:2, ... and so forth.
Range Partitioning:
Range partitioning is accomplished by mapping ranges of objects
into specific Redis instances. Suppose in our example, the users
from ID 0 to ID 10000 will go into instance R0, while the users
from ID 10001 to ID 20000 will go into instance R1 and so forth.
Hash Partitioning:
In this type of partitioning, a hash function (eg. modulus function) is
used to convert the key into a number and then the data is stored in
different-different Redis instances.