Redis - Tech It Yourself


Monday, 12 July 2021


1. Introduction

Redis is an open source, advanced key-value store and an apt solution for building high performance, scalable web applications.

Redis has main features:

  • Redis holds its database entirely in the memory, using the disk only for persistence. It supports very high write and read speed but the data sets can't be larger than the memory. Moreover It is easier to represent  the complex data structures on memory than on disk.
  • All the commands in a transaction are serialized and executed sequentially. It can never happen that a request issued by another client is served in the middle of the execution of a Redis transaction. ... Either all of the commands or none are processed, so a Redis transaction is also atomic
  • Redis has a relatively rich set of data types such as list, set, sorted set, and hashes when compared to many key-value data stores.
  • Redis can replicate data to any number of slaves.
2. Installation & Configuration
2.1 Installation
$sudo apt-get update 
$sudo apt-get install redis-server
Redis desktop manager on Ubuntu:

2.2. Configuration
Get configuration
redis> config get config_name
redis> CONFIG GET * 
Set configuration
redis> config set config_name new_config_value
CONFIG SET loglevel "notice" 
redis> CONFIG GET loglevel 
2.3 Commands
redis> ping 
Run Commands on the Remote Server
$ redis-cli -h host -p port -a password
Get all statistics and information about the server
redis> INFO  

# Server 
os:Linux 3.5.0-48-generic x86_64 
2.4. Data Types
2.4.1 Keys
Operate with keys in Redis. 
There are some commands related to keys such as:
EXPIREAT command is used to set the expiry of key in Unix timestamp format. After the expiry time, the key will not be available in Redis.
redis> EXPIREAT learning 1293840000 
(integer) 1 
EXISTS learning 
(integer) 0
2.4.2 Strings
Redis string is a sequence of bytes with a known length not determined by any special terminating characters. Its length is up to 512 megabytes.
SET name "learning" 
SET and GET are Redis commands, name is the key and learning is the string value
There are some commands related to String such as:
GETRANGE command returns substring of the string value stored at the key, determined by the offsets start and end.
redis> GETRANGE KEY_NAME start end
redis> SET mykey "This is my test key" 
redis> GETRANGE mykey 0 3 
redis> GETRANGE mykey 0 -1 
"This is my test key"
2.4.3 Hashes
Hashes are maps between the string fields and the string values.
Hashes can store up to more than 4 billion field-value pairs.
redis> HMSET tutorialspoint name "redis tutorial" 
description "redis basic commands for caching" likes 20 visitors 23000 
There are some commands related to Hash such as:
HGET command is used to get the value associated with the field.
redis> HSET myhash field1 "foo" 
(integer) 1 
redis> HGET myhash field1 
redis> HEXISTS myhash field2 
2.4.4 Lists
Lists are lists of strings, sorted by insertion order. You can add elements in Redis lists in the head or the tail of the list.
Maximum length of a list is > 4 billion of elements.
There are some commands related to List such as:
LPUSH command inserts all the specified values at the head of the list stored at the key. If the key does not exist, it is created as an empty list before performing the push operation.
redis> LPUSH list1 "foo" 
(integer) 1 
redis> LPUSH list1 "bar" 
(integer) 2 
redis> LRANGE list1 0 -1 
1) "foo" 
2) "bar"
2.4.5 Sets
Sets are an unordered collection of unique strings. Unique means sets does not allow repetition of data in a key.
redis> SADD tutorials redis 
(integer) 1 
redis> SADD tutorials mongodb 
(integer) 1 
redis> SADD tutorials mysql 
(integer) 1 
redis> SADD tutorials mysql 
(integer) 0 
redis> SMEMBERS tutorials  
1) "mysql" 
2) "mongodb" 
3) "redis"
There are some commands related to Set such as:
SCARD command is used to return the number of elements stored in a set.
redis> SADD myset "hello" 
(integer) 1 
redis> SADD myset "foo" 
(integer) 1 
redis> SADD myset "hello" 
(integer) 0 
redis> SCARD myset 
(integer) 2
2.4.6 Sorted Sets
It is similar to Sets. 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.
redis> ZADD tutorials 1 redis 
(integer) 1 
redis> ZADD tutorials 2 mongodb 
(integer) 1 
redis> ZADD tutorials 3 mysql 
(integer) 1 
redis> ZADD tutorials 3 mysql 
(integer) 0 
redis> ZADD tutorials 4 mysql 
(integer) 0 
redis> ZRANGE tutorials 0 10 WITHSCORES  
1) "redis" 
2) "1" 
3) "mongodb" 
4) "2" 
5) "mysql" 
6) "4"
There are some function related to Sorted Sets such as:
ZINCRBY command increments the score of member in the sorted set.
redis> ZADD myset 1 "hello" 
(integer) 1 
redis> ZADD myset 1 "foo" 
(integer) 1 
redis> ZINCRBY myzset 2 "hello" 
(integer) 3 
redis> ZRANGE myzset 0 -1 WITHSCORES 
1) "foo" 
2) "2" 
3) "hello" 
4) "3"
2.5 HyperLogLog
HyperLogLog is an algorithm that uses randomization in order to provide an approximation of the number of unique elements in a set using just a constant, and small amount of memory (12 kbytes per key with a standard error of 0.81%).
redis> PFADD tutorials "redis"  
1) (integer) 1  
redis> PFADD tutorials "mongodb"  
1) (integer) 1  
redis> PFADD tutorials "mysql"  
1) (integer) 1  
redis> PFCOUNT tutorials  
(integer) 3 
2.6 Publish - Subscribe
Redis Pub/Sub is a messaging system where the senders (publishers) sends the messages to the receivers (subscribers). The link by which the messages are transferred is called channel. 
A client can subscribe any number of channels.
redis> SUBSCRIBE redisChat  
Reading messages... (press Ctrl-C to quit) 
1) "subscribe" 
2) "redisChat" 
3) (integer) 1 
redis> PUBLISH redisChat "Redis is a great caching technique"  
(integer) 1  
2.7 Transactions
Transactions allow the execution of a group of commands in a single step.
All commands in a transaction are sequentially executed as a single isolated operation. It is not possible that a request issued by another client is served in the middle of the execution of a Redis transaction.
Redis transaction is also atomic.
Redis transaction is initiated by command MULTI and then you need to pass a list of commands that should be executed in the transaction, after which the entire transaction is executed by EXEC command.
edis> MULTI 
redis> SET tutorial redis 
redis> GET tutorial 
redis> INCR visitors 
redis> EXEC  
1) OK 
2) "redis" 
3) (integer) 1 
2.8 Scripting
Scripting uses Lua interpreter. Using EVAL command.
EVAL script numkeys key [key ...] arg [arg ...]
redis> EVAL "return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}" 2 key1 
key2 first second  
1) "key1" 
2) "key2" 
3) "first" 
4) "second"
2.9 Pipelining
The client can send multiple requests to the server without waiting for the replies, and then reads the replies in a single step. This helps improving protocol performance.
This is executed from Terminal.
$ (printf "AUTH "abc"\r\nPING\r\nPING\r\nPING\r\n"; sleep 1) | nc localhost 6379 
3. Management
3.1 Backup and Restore
Using SAVE command to backup of the current Redis database.
It creates a dump.rdb file in Redis directory.
Restore Redis Data by moving backup file (dump.rdb) into Redis directory and start the server.
Get Redis directory by CONFIG command.> CONFIG get dir  
BGSAVE command will start the backup process and run this in the background.> BGSAVE  
Background saving started
3.2 Security
Set the password  so that remote connections need to authenticate before executing a command.> CONFIG set requirepass "abc" 
OK> CONFIG get requirepass 
1) "requirepass" 
Remember to save config file.
After setting the password, client can connect to server but running the command without authentication, will get NOAUTH Authentication required. Hence, the client needs to use AUTH command to authenticate.> AUTH "abc" 
3.3 Client Connection
Set the maximum number of clients to 1000
redis-server --maxclients 1000
List of clients connected:> CLIENT LIST 
Closes a client connection> CLIENT KILL 
3.4 Partitioning
Partitioning splits data into multiple Redis instances, so that every instance will only contain a subset of keys. This helps to use the strength of distributed system. But It is not easy to manage.
Types of Partitioning:
  • Range Partitioning: the keys from ID 0 to ID 10000 will go into instance R0, while the keys from ID 10001 to ID 20000 will go into instance R1, ...
  • Hash Partitioning: a hash function is used to map the key into a number and then the data is distributed to different Redis instances.

No comments:

Post a Comment

Thường mất vài phút để quảng cáo xuất hiện trên trang nhưng thỉnh thoảng, việc này có thể mất đến 1 giờ. Hãy xem hướng dẫn triển khai mã của chúng tôi để biết thêm chi tiết. Ðã xong