1.
about Redis
Redis is an open source, memory-based Key-value database written using the ANSI C language.
It supports storing more value types, including string (string), list (linked list), set (set), Zset (sorted set-ordered set), and hash (hash type).
Redis supports master-slave synchronization, where data can be synchronized from the primary server to any number of slave servers, and because of the full implementation of the publish/subscribe mechanism, you can subscribe to a channel and receive a complete message release record from the primary server when the tree is synchronized anywhere from the database.
The following advantages are compared to Memcached,rdeis:
1. Redis native supports more data types.
2. Redis has a persistence mechanism that can periodically persist the in-memory data to the hard disk.
3. Redis supports Master-slave mode of data backup.
4. Performance. The Redis authors say that the performance is average to a single core, and Redis is better in the case of small pieces of data.
Why do you say that, the reason is that Redis is a single-threaded operation. Because it is single-threaded, the overall performance will certainly be low compared to memcached multithreading. Because it is single-threaded, IO is serialized, network IO, and memory Io, so performance is affected when the single data is too large to be followed up by waiting for all of the IO of a command to complete for subsequent commands.
2.
installation
Redis2.1 Redis is easy to install and can be installed directly with Yum or Apt-get
12 |
# yum install epel-release (centos 7可以直接安装epel源) # yum install redis |
2.2 Start/Stop Redis
1234 |
# redis-server /etc/redis.conf # systemctl start redis # systemctl stop redis |
3.
Use
Redis3.1 REDIS-CLI Command line operation KV
Connect to Redis
12 |
# redis-cli -p port # redis-cli |
Ping
1 |
127.0.0.1:6379> ping <br>PONG |
Set the key value
12 |
127.0.0.1:6379> set testkey "hello" OK |
Query key
12 |
127.0.0.1:6379> get testkey "hello" |
Delete key
12 |
127.0.0.1:6379> del testkey (integer) 1 |
Set Validity period
12 |
127.0.0.1:6379> setex test 10 111 OK |
Use Expire key s to set the expiration time in milliseconds with Pexpire
12 |
127.0.0.1:6379> EXPIRE test11 300 (integer) 1 |
Use TTL key to view expiration time in milliseconds with Pttl
12 |
127.0.0.1:6379> TTL test11 (integer) 288 |
Cancel the expiration time with persist key
12 |
127.0.0.1:6379> PERSIST test11 (integer) 1 |
3.2 Advanced features 3.2.1 Self-increment, self-reduction, INCR, DECR, Incrby, SORT
12345678 |
127.0.0.1:6379> set counter 100 OK 127.0.0.1:6379> incr counter (integer) 101 127.0.0.1:6379> incr counter (integer) 102 127.0.0.1:6379> decr counter (integer) 101 |
3.2.2 Transactions
123456789101112 |
127.0.0.1:6379> MULTI OK 127.0.0.1:6379> set test11 111111 QUEUED 127.0.0.1:6379> set test12 121212 QUEUED 127.0.0.1:6379> incr counter QUEUED 127.0.0.1:6379> EXEC 1) OK 2) OK 3) (integer) 102 |
3.2.3 Hyperloglogs
Redis added the Hyperloglog algorithm in version 2.8.9.
3.2.4 Publish/Subscribe feature
A Redis Publish Subscription (PUB/SUB) is a message communication pattern: the Sender (pub) sends a message and the Subscriber (sub) receives the message. Redis clients can subscribe to any number of channels.
Subscribe to a channel on a client redischat
12345 |
127.0.0.1:6379> SUBSCRIBE redisChat Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "redisChat" 3) (integer) 1 |
On another client, sending a message to the channel Redischat, the Subscriber can receive the message
Publishing side:
12 |
127.0.0.1:6379> PUBLISH redisChat "redis haha" (integer) 1 |
Subscribers:
12345678 |
127.0.0.1:6379> SUBSCRIBE redisChat Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "redisChat" 3) (integer) 1 1) "message" 2) "redisChat" 3) "redis haha" |
3.
3
View Redis Status
Info output is a lot of information, you can specify the part of the output
1 |
127.0.0.1:6379> info stats |
1 |
127.0.0.1:6379> info memory |
Used_memory: The total amount of memory allocated by the Redis allocator, in bytes (byte).
Used_memory_rss: Returns the total amount of memory allocated by Redis (commonly known as the resident set size) from the operating system's perspective. This value is consistent with the output of commands such as top and PS.
rss > used, and the difference between the two values, indicates the existence (internal or external) memory fragmentation.
The rate of memory fragmentation can be seen through the Mem_fragmentation_ratio value.
Used > rss, it means that part of Redis's memory is swapped out to swap space, in which case the operation can have a noticeable delay.
Used_memory_peak: Peak, set maximum memory greater than peak
3.
4 other Commands
View the number of records
View all Keys
List all client connections
1 |
127.0.0.1:6379> CLIENT LIST |
Close the Ip:port client
1 |
127.0.0.1:6379> CLIENT KILL 127.0.0.1:11902 |
Clear all keys for all databases
1 |
127.0.0.1:6379> FLUSHALL |
Empties all keys in the current database
1 |
127.0.0.1:6379> FLUSHDB |
Returns the last time the data was successfully saved to disk, expressed in the UNIX timestamp format
1 |
127.0.0.1:6379> LASTSAVE |
Returns the current server time, expressed in UNIX timestamp format
Connect to a different database (default database is 0)
12 |
127.0.0.1:6379> SELECT 1 OK |
Moves the key of the current database to the specified database
12 |
127.0.0.1:6379> MOVE test2 1 (integer) 1 |
4.
settings File4.1/etc/redis.conf
12345678910111213 |
daemonize no 是否以后台daemon方式运行
timeout 0 请求超时时间
maxclients 10000 最大连接数
maxmemory <bytes> 最大内存
maxmemory-policy volatile-lru 达到最大内存时的LRU驱逐策略
maxmemory-samples 3 随机抽取n个key执行LRU
hash
-max-ziplist-entries 512 Map内部不超过多少个成员时会采用线性紧凑格式存储
hash
-max-ziplist-value 64 Map内成员值长度不超过多少字节会采用线性紧凑格式存储
类似的还有,list-max-ziplist-entries 512,list-max-ziplist-value 64等等 slowlog-log-slower-than 10000 slow log计入时间,microseconds(1000000)
slowlog-max-len 128 slow log计入条数
|
4.2 Viewing the maximum number of connections
123 |
127.0.0.1:6379> config get maxclients 1) "maxclients" 2) "10000" |
Adjusting parameters during Operation
1 |
127.0.0.1:6379> config set maxclients 10001 |
4.3
View
Slow Log
123456 |
127.0.0.1:6379> SLOWLOG get 127.0.0.1:6379> SLOWLOG get 10 1) 1) (integer) 0 2) (integer) 1448413479 3) (integer) 124211 4) 1) "FLUSHALL" |
Confirm Slow log Bar number setting
1 |
127.0.0.1:6379> SLOWLOG len |
Empty slow Log
1 |
127.0.0.1:6379> SLOWLOG reset |
5.
Data Persistence
5.1
Snapshot (
Snapshot
)5.1.1 Setting a snapshot in the settings file
1234 |
save <seconds> <changes> 开启快照,并设定保存快照到硬盘的频率 rdbcompression yes /no 保存快照的时候,是否压缩 dbfilename dump.rdb 指定快照的文件名(Append Only File也保存在此) dir /var/lib/redis/ 指定快照存放的场所 |
5.1.2 Creating a snapshot manually
To execute the Save or bgsave command at the command line
12 |
127.0.0.1:6379> SAVE OK |
5.2
Log Backups (
Append only File
)
Similar to the MySQL Binlog, the operation is recorded in log. When snapshots do not reach the required precision, they are used in conjunction with snapshots and are not recommended for use alone. The default interval is 1 seconds and can be modified.
5.2.1 setting aof in the settings file
1234 |
appendonly yes 开启Append Only File appendfilename "appendonly.aof" 指定日志文件名 appendfsync always /everysec/no 指定写日志的频率 no-appendfsync-on-rewrite no 当有bgsave等其他进程执行fsync()时,AOF和appendfsync none动作一样 |
5.3
Restore
To recover Redis data simply move the Redis backup file (dump.rdb,appendonly.aof) to the Redis directory and start the server.
To get your Redis directory, use the command as shown below:
123 |
127.0.0.1:6379> config get dir 1) "dir" 2) "/var/lib/redis" |
6. PostScript
This paper briefly introduces the installation and use of Redis, then introduces the master-slave synchronization, load dispersion.
Redis Series (i): 10-minute redis (turn)