Redis configuration to explain and combat

Foreword

Redis memory kv is an open source database, but also used as a buffer, the message queue. It supports multiple data types, such as strings, lists, dictionaries, collections, ordered set.

 

Demo environment

$ uname -a
Darwin 18.6.0 Darwin Kernel Version 18.6.0: Thu Apr 25 23:16:27 PDT 2019; root:xnu-4903.261.4~2/RELEASE_X86_64 x86_64
$ redis-server -v
Redis server v=5.0.5 sha=00000000:0 malloc=libc bits=64 build=31cd6e21ec924b46

 

installation

brew install redis

 

Configuration

Redis used the default configuration file path is /usr/local/etc/redis.conf, Redis will affect the behavior by modifying the configuration file.

Redis default is to run in the foreground, when the terminal exits Redis will quit, so the first task is to modify the configuration file so that Redis running in the background:

daemonize yes

Redis and then start the following command:

redis-server /usr/local/etc/redis.conf 

After the start Redis can see redis-server process listening 127.0.0.1:6379, can be modified by the following fields:

bind 127.0.0.1 ::1
port 6379

Redis server connection redis-server via Redis client redis-cli:

Redis- $ CLI 
 127.0 . 0.1 : 6379 > 
 127.0 . 0.1 : 6379 > SET # Hello World string operations 
the OK 
127.0 . 0.1 : 6379 > # * list all Keys Key
 . 1 ) " X1 " 
2 ) " X2 " 
. 3 ) " the Hello " 
127.0 . 0.1 : 6379 > HSET myhash 1 the WWW # dictionary (hash table) operation 
(Integer) 1 
127.0 . 0.1: 6379 > HSET myhash 2 yyy 
(Integer) 1 
127.0 . 0.1 : 6379 > 
 127.0 . 0.1 : 6379 > 
 127.0 . 0.1 : 6379 > hgetall myhash # Gets a dictionary of myhash All Key-value
 1 ) " 1 " 
2 ) " the WWW " 
. 3 ) " 2 " 
. 4 ) " yyy " 
127.0 . 0.1 : 6379 > LPUSH mylist1 # list operations, elements may be repeated 
(Integer) 1 
127.0 . 0.1 : 6379 > LPUSH mylist 2 
(Integer) 2 
127.0 . 0.1 : 6379 > LPUSH mylist . 3 
(Integer) . 3 
127.0 . 0.1 : 6379 > LPUSH mylist 1 
(Integer) . 4 
127.0 . 0.1 : 6379 > Lrange mylist 0 - . 1 
. 1 ) " . 1 " 
2 ) " . 3 " 
. 3) " 2 " 
. 4 ) " . 1 " 
127.0 . 0.1 : 6379 > Sadd myset . 1 # collection operation is repeated element remains only one 
(Integer) . 1 
127.0 . 0.1 : 6379 > Sadd myset 2 
(Integer) . 1 
127.0 . 0.1 : 6379 > myset Sadd . 3 
(Integer) . 1 
127.0 . 0.1 : 6379 > myset Sadd . 1 
(Integer) 0 
127.0 . 0.1 : 6379> Smembers myset
 . 1 ) " . 1 " 
2 ) " 2 " 
. 3 ) " . 3 " 
127.0 . 0.1 : 6379 > Zadd mysortset . 1 Redis # ordered set of operations, in front of the score value, as a basis for sorting 
(Integer) . 1 
127.0 . 0.1 : 6379 > Zadd mysortset . 3 MySQL 
(Integer) . 1 
127.0 . 0.1 : 6379 > Zadd mysortset 2 the memcached 
(Integer) . 1 
127.0 .0.1:6379> zrange mysortset 0 -1 withscores
1) "redis"
2) "1"
3) "memcached"
4) "2"
5) "mysql"
6) "3"

The above operation demonstrates Redis strings, lists, dictionaries, collections, ordered collection of five data types. redis-cli using the default configuration connection redis-server, but in a production environment IP and Port:

u$ redis-cli -h
redis-cli 5.0.5

Usage: redis-cli [OPTIONS] [cmd [arg [arg ...]]]
  -h <hostname>      Server hostname (default: 127.0.0.1).
  -p <port>          Server port (default: 6379).

Redis-server authentication configuration

requirepass foobared

After restarting redis-server, try again redis-cli connection operations:

$ redis-cli 
127.0.0.1:6379> 
127.0.0.1:6379> keys *
(error) NOAUTH Authentication required.
127.0.0.1:6379> 

After this time you need to enter a password to operate then:

127.0.0.1:6379> auth foobared
OK
127.0.0.1:6379> keys *
1) "myset"
2) "mysortset"
3) "x1"
4) "mylist"
5) "myhash"
6) "x2"
7) "hello"
127.0.0.1:6379> 

 

Endurance of

Redis data persistence strategies RDB snapshots and AOF (append only file) way.

RDB is enabled by default Redis persistence strategy, relevant configured to:

Save 900  . 1 # 900s have more than one key change trigger RDB persistent 
Save 300  10 # 300S more than 10 key change trigger RDB persistent 
Save 60  10000 10000 # 60s more than the key change RDB trigger persistent 
rdbcompression yes # compressed data stored in the rdb file 
rdbchecksum yes # open checksum confirm rdb data capabilities 
dbfilename dump.rdb #rdb file name 
dir / usr / local / var / db / Redis / #rdb file directory location

Data file is loaded dump.rdb Redis started, if the file is corrupted or incomplete dump.rdb (possibly power off the machine, the last portion of the brush RDB data persistence to dump.rdb file), by redis-check-rdb detection dump.rdb file availability:

$ redis-check-rdb dump.rdb 
[offset 0] Checking RDB file dump.rdb
[offset 26] AUX FIELD redis-ver = '5.0.5'
[offset 40] AUX FIELD redis-bits = '64'
[offset 52] AUX FIELD ctime = '1561972490'
[offset 67] AUX FIELD used-mem = '1006704'
[offset 83] AUX FIELD aof-preamble = '0'
[offset 85] Selecting DB ID 0
[offset 266] Checksum OK
[offset 266] \o/ RDB looks OK! \o/
[info] 7 keys read
[info] 0 expires
[info] 0 already expired

When dump.rdb file is damaged and can not be recovered, dump.rdb need to delete the file, otherwise it will automatically exit after redis-server start:

$ echo " i am test hahhahha" > dump.rdb 
$ redis-server /usr/local/etc/redis.conf 
23194:C 01 Jul 2019 17:44:34.757 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
23194:C 01 Jul 2019 17:44:34.757 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=23194, just started
23194:C 01 Jul 2019 17:44:34.757 # Configuration loaded
$ ps -ef|grep redis
  501 23264 46375   0  5:45下午 ttys002    0:00.00 grep redis

When you delete a file dump.rdb, redis-server can be a normal start, but before the data is gone:

$ rm dump.rdb 
$ redis-server /usr/local/etc/redis.conf 
23498:C 01 Jul 2019 17:48:26.574 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
23498:C 01 Jul 2019 17:48:26.574 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=23498, just started
23498:C 01 Jul 2019 17:48:26.574 # Configuration loaded
$ redis-cli 
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> 

In addition dump.rdb files are binary data, with vim directly view the human eye can not understand:

Data Format:

Beginning with a fixed 5 REDIS characters, represented as RDB file.

db_version expressed as the redis version.

EOF indicates the end of the text content.

8 is a checksum byte unsigned integer checksum. Verify data integrity.

databases section:

The first part indicates that the redis which is what the DB (0-15).

The second part is db_number db specific needs, such as 0. Then the first and second portions together is select 0.

Key_value_pairs third portion represents the specific key information.

A complete RDB file contents example:

 

RDB persistence key points:

After 1.redis use linux fork child process, the process has a parent process memory snapshot of the way quick access to the memory snapshot (copy on write).

2.redis RDB persistence off disk operations occur independent sub-process, master process does not affect the other requests. (Corresponding to bgsave command, if a save command or the main treatment process RDB)

 

RDB strategy advantages:

1. suitable for large-scale data recovery, data recovery fast.

2. If the business of data integrity and consistency do not ask, RDB is a good choice.

RDB policy shortcomings:

Data integrity and consistency is not high, it will be lost under unusual circumstances last RDB data before backup.

 

AOF strategy

AOF emergence and RDB is complementary to it in the form of log records the contents of each write operation, the write command to perform a previous data recovery is complete after.

Yes appendOnly 
appendFileName " appendonly.aof " 
# # appendfsync Always write operation occurs every time the disk brush 
appendfsync everysec # per second timing of the brush disc 
# appendfsync no # brush disc itself relies on the system

Empty Redis then restart redis-server:

Redis- $ cli 
 127.0 . 0.1 : 6379 > 
 127.0 . 0.1 : 6379 > 
 127.0 . 0.1 : 6379 > * Keys 
(empty List or the SET) 
127.0 . 0.1 : 6379 > 
 127.0 . 0.1 : 6379 > 
 127.0 . 0.1 : 6379 > the SET the Hello world 
the OK 

$ CAT appendonly.aof 
 * 2 # represents two parameters 
$ 6 # this parameter has six characters
# Is the SELECT parameter SELECT 
$ . 1 # This parameter has a character 
0 is a parameter # 0 
* 3 # 3 parameters represents 
$ 3 # 3 bytes of the parameters 
set # parameters SET 
$ . 5 This argument # 5 bytes of 
hello # parameters Hello 
$ . 5 # 5 bytes of the parameter 
world parameters world #

So appendonly.aof reduced to save the command:

select 0
set hello world

Which select 0 indicates the default choice Redis No. 0 DB.

 

AOF strategy advantages:

Higher data integrity and consistency, anomalies will only lose the data 1s.

AOF policy shortcomings:

AOF redundant files, file is too large, Redis boot loader slowly.

 

After executing loader Redis service starts, giving priority to determine whether to open the AOF persistence will load AOF file if otherwise loaded RDB file.

 

Contrast Memcached

1.Redis support data persistence, Memcached does not support, power data will disappear.

2.Redis than Memcached supports more data types, Memcached all values ​​are strings.

So preference Redis.

 

to sum up

This article demonstrates Redis five data types of operations, in addition introduced Redis RDB and AOF persistence strategy, and finally compared Redis and Memcached.

 

reference

https://redis.io/

http://redisbook.com/preview/rdb/rdb_struct.html

https://juejin.im/post/5bd96bcaf265da396b72f855

 

 

 

Guess you like

Origin www.cnblogs.com/makelu/p/11116418.html