Redis - Tech It Yourself

Hot

Monday, 12 July 2021

Redis

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:
https://redisdesktop.com/download

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

# Server 
redis_version:2.8.13 
redis_git_sha1:00000000 
redis_git_dirty:0 
redis_build_id:c2238b38b1edb0e2 
redis_mode:standalone 
os:Linux 3.5.0-48-generic x86_64 
arch_bits:64 
2.4. Data Types
2.4.1 Keys
Operate with keys in Redis. 
redis 127.0.0.1:6379> COMMAND KEY_NAME
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 127.0.0.1:6379> Expireat KEY_NAME TIME_IN_UNIX_TIMESTAMP 
redis 127.0.0.1:6379> 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 127.0.0.1:6379> GETRANGE KEY_NAME start end
redis 127.0.0.1:6379> SET mykey "This is my test key" 
OK 
redis 127.0.0.1:6379> GETRANGE mykey 0 3 
"This" 
redis 127.0.0.1:6379> 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 127.0.0.1:6379> 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 127.0.0.1:6379> HSET myhash field1 "foo" 
(integer) 1 
redis 127.0.0.1:6379> HGET myhash field1 
"foo" 
redis 127.0.0.1:6379> HEXISTS myhash field2 
(nil)
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 127.0.0.1:6379> LPUSH list1 "foo" 
(integer) 1 
redis 127.0.0.1:6379> LPUSH list1 "bar" 
(integer) 2 
redis 127.0.0.1:6379> 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 127.0.0.1:6379> SADD tutorials redis 
(integer) 1 
redis 127.0.0.1:6379> SADD tutorials mongodb 
(integer) 1 
redis 127.0.0.1:6379> SADD tutorials mysql 
(integer) 1 
redis 127.0.0.1:6379> SADD tutorials mysql 
(integer) 0 
redis 127.0.0.1:6379> 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 127.0.0.1:6379> SADD myset "hello" 
(integer) 1 
redis 127.0.0.1:6379> SADD myset "foo" 
(integer) 1 
redis 127.0.0.1:6379> SADD myset "hello" 
(integer) 0 
redis 127.0.0.1:6379> 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 127.0.0.1:6379> ZADD tutorials 1 redis 
(integer) 1 
redis 127.0.0.1:6379> ZADD tutorials 2 mongodb 
(integer) 1 
redis 127.0.0.1:6379> ZADD tutorials 3 mysql 
(integer) 1 
redis 127.0.0.1:6379> ZADD tutorials 3 mysql 
(integer) 0 
redis 127.0.0.1:6379> ZADD tutorials 4 mysql 
(integer) 0 
redis 127.0.0.1:6379> 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 127.0.0.1:6379> ZINCRBY KEY INCREMENT MEMBER
redis 127.0.0.1:6379> ZADD myset 1 "hello" 
(integer) 1 
redis 127.0.0.1:6379> ZADD myset 1 "foo" 
(integer) 1 
redis 127.0.0.1:6379> ZINCRBY myzset 2 "hello" 
(integer) 3 
redis 127.0.0.1:6379> 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 127.0.0.1:6379> PFADD tutorials "redis"  
1) (integer) 1  
redis 127.0.0.1:6379> PFADD tutorials "mongodb"  
1) (integer) 1  
redis 127.0.0.1:6379> PFADD tutorials "mysql"  
1) (integer) 1  
redis 127.0.0.1:6379> 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 127.0.0.1:6379> SUBSCRIBE redisChat  
Reading messages... (press Ctrl-C to quit) 
1) "subscribe" 
2) "redisChat" 
3) (integer) 1 
redis 127.0.0.1:6379> 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 127.0.0.1:6379> MULTI 
OK 
redis 127.0.0.1:6379> SET tutorial redis 
QUEUED 
redis 127.0.0.1:6379> GET tutorial 
QUEUED 
redis 127.0.0.1:6379> INCR visitors 
QUEUED 
redis 127.0.0.1:6379> 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 127.0.0.1:6379> 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.
127.0.0.1:6379> CONFIG get dir  
BGSAVE command will start the backup process and run this in the background.
127.0.0.1:6379> BGSAVE  
Background saving started
3.2 Security
Set the password  so that remote connections need to authenticate before executing a command. 
127.0.0.1:6379> CONFIG set requirepass "abc" 
OK 
127.0.0.1:6379> CONFIG get requirepass 
1) "requirepass" 
2) "abc" 
127.0.0.1:6379> CONFIG REWRITE
OK
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.
127.0.0.1:6379> AUTH "abc" 
OK 
3.3 Client Connection
Set the maximum number of clients to 1000
redis-server --maxclients 1000
List of clients connected: 
127.0.0.1:6379> CLIENT LIST 
Closes a client connection
127.0.0.1:6379> CLIENT KILL 127.0.0.1:52668 
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