精进redis

Redis数据安全与性能优化

2018-09-18  本文已影响0人  孤尘F

1.Redis持久化选项

持久化方式:

在某一时刻将所有数据都写入硬盘
具体配置选项如下:
#60 秒后如果至少有 1000个 key 的值变化,则保存
save 60 1000    
# 默认情况下,如果 redis 最后一次的后台保存失败,redis 将停止接受写操作,
# 这样以一种强硬的方式让用户知道数据不能正确的持久化到磁盘,
# 否则就会没人注意到灾难的发生。
# 如果后台保存进程重新启动工作了,redis 也将自动的允许写操作。
# 然而你要是安装了靠谱的监控,你可能不希望 redis 这样做,那你就改成 no 好了。
stop-writes-on-bgsave-error no
# 是否在 dump .rdb 数据库的时候使用 LZF 压缩字符串
# 默认都设为 yes
# 如果你希望保存子进程节省点 cpu ,你就设置它为 no ,
# 不过这个数据集可能就会比较大
rdbcompression yes
# 设置 dump 的文件位置
dbfilename dump.rdb
# 工作目录
# 例如上面的 dbfilename 只指定了文件名,
# 但是它会写入到这个目录下。这个配置项一定是个目录,而不能是文件名。
dir ./
# 附 redis.conf 配置选项的中文解读
https://github.com/linli8/cnblogs/blob/master/redis%E5%89%AF%E6%9C%AC.conf
在执行写入命令时,将命令追加至硬盘
具体配置选项如下:
#是否开启AOF追加
appendonly no
#AOF追加的频率
#everysec没秒追加一次
#always 每条指令都追加,影响性能,影响磁盘寿命
#no 不主动追加,由操作系统决定合适追加至文件
appendfsync everysec
#在rewrite的时候是否对新的写操作进行fsync。no表示进行fsync,yes表示不进行
no-appendfsync-on-rewrite no
#当AOF文件大小大于上次重写(BGREWRITEAOF,后面会讲)之后的100%时,会对AOF文件执行BGREWRITEAOF操作
auto-aof-rewrite-percentage 100
#当AOF文件大小大于之后的64mb时,会对AOF文件执行BGREWRITEAOF操作
auto-aof-rewrite-min-size 64mb
# 工作目录
# 例如上面的 dbfilename 只指定了文件名,
# 但是它会写入到这个目录下。这个配置项一定是个目录,而不能是文件名。
dir ./

1.1 快照

快照会被写入dbfilename选项指定的文件中。如果在快照文件创建完毕之前,Redis、系统或者硬件这三者其中之一崩溃,那么Redis将会丢失最近一次快照之后的所有数据。
创建快照的几个办法:

快照文件的问题:

1.2 AOF文件

Redis每秒同步一次AOF文件和不使用任何持久化特性时相差无几,而使用每秒同步一次AOF文件,则可以保证Redis最多会丢失1s的数据。
BGREWRITEAOF命令
这个命令会通过移除AOF文件中的冗余命令来重写AOF文件,使AOF文件的体积变得尽可能的小。

2.复制

复制可以让其他服务器拥有一个不断更新的数据副本,从而使得拥有数据副本的服务器可以用于处理客户端发送的读请求。即不再基于单点的Redis服务器形式。可以在某一台服务崩溃时,其他服务器做补偿,从而保证数据的完整性。

2.1 Redis复制的启动过程

Redis复制主动服务器交互过程

当多个从服务器连接同一个主服务器时,如果顺序靠后的从服务器请求主服务器时,主服务正在执行BGSAVE命令或已经执行完毕,那么主服务器会线性对后续的从服务继续执行一遍整个流程。

对于Redis服务器来说使用复制+AOF文件的形式可以最大限度的保证数据的完整性。

3.处理系统故障

验证快照文件和AOF文件

#验证AOF文件的正确性,给定--fix参数时,程序将对AOF文件进行修复,它会扫描AOF文件,
#寻找不正确或不完整的命令,当发现第一个出错命令的时候,程序会删除出错的命令
#以及位于出错命令之后的所有命令,只保留那些位于出错命令之前的正确命令
fulibaodeMacBook-Pro:*** fureitakara$ redis-check-aof
Usage: redis-check-aof [--fix] <file.aof>
##检查快照文件,但是无法对快照文件进行修复
fulibaodeMacBook-Pro:*** fureitakara$ redis-check-dump
-bash: redis-check-dump: command not found

4.Redis事务

在用户使用WATCH命令对键进行监视之后,直到用户执行EXEC命令的这段时间里面,如果由其他客户端抢先对任何被监视的键进行了替换、更新或删除等操作,那么当用户尝试执行EXEC命令时,事务将失败,并返回UNWATCH/DISCARD等命令
UNWATCH命令可以在WATCH命令之后,MULTI命令执行之前对连接进行重置
DISCARD命令WATCH命令之后,EXEC命令执行之前对连接进行重置,清空所有入队命令

5.非事务型流水线

这种主要是由Redis具体对接语言的客户端来支持,通过将一组命令统一发送给Redis服务器而不是逐条发送,降低由于网路延迟带来的开销,从而提升系统的性能

6.Redis性能测试

使用以下命令来进行Redis性能测试

redis-benchmark
Usage: redis-benchmark [-h <host>] [-p <port>] [-c <clients>] [-n <requests>] [-k <boolean>]

 -h <hostname>      Server hostname (default 127.0.0.1)
 -p <port>          Server port (default 6379)
 -s <socket>        Server socket (overrides host and port)
 -a <password>      Password for Redis Auth
 -c <clients>       Number of parallel connections (default 50)
 -n <requests>      Total number of requests (default 100000)
 -d <size>          Data size of SET/GET value in bytes (default 3)
 --dbnum <db>       SELECT the specified db number (default 0)
 -k <boolean>       1=keep alive 0=reconnect (default 1)
 -r <keyspacelen>   Use random keys for SET/GET/INCR, random values for SADD
  Using this option the benchmark will expand the string __rand_int__
  inside an argument with a 12 digits number in the specified range
  from 0 to keyspacelen-1. The substitution changes every time a command
  is executed. Default tests use this to hit random keys in the
  specified range.
 -P <numreq>        Pipeline <numreq> requests. Default 1 (no pipeline).
 -e                 If server replies with errors, show them on stdout.
                    (no more than 1 error per second is displayed)
 -q                 Quiet. Just show query/sec values
 --csv              Output in CSV format
 -l                 Loop. Run the tests forever
 -t <tests>         Only run the comma separated list of tests. The test
                    names are the same as the ones produced as output.
 -I                 Idle mode. Just open N idle connections and wait.

Examples:

 Run the benchmark with the default configuration against 127.0.0.1:6379:
   $ redis-benchmark

 Use 20 parallel clients, for a total of 100k requests, against 192.168.1.1:
   $ redis-benchmark -h 192.168.1.1 -p 6379 -n 100000 -c 20

 Fill 127.0.0.1:6379 with about 1 million keys only using the SET test:
   $ redis-benchmark -t set -n 1000000 -r 100000000

 Benchmark 127.0.0.1:6379 for a few commands producing CSV output:
   $ redis-benchmark -t ping,set,get -n 100000 --csv

 Benchmark a specific command line:
   $ redis-benchmark -r 10000 -n 10000 eval 'return redis.call("ping")' 0

 Fill a list with 10000 random elements:
   $ redis-benchmark -r 10000 -n 10000 lpush mylist __rand_int__

 On user specified command lines __rand_int__ is replaced with a random integer
 with a range of values selected by the -r option.

7.总结

本篇文章主要总结了Redis对于持久化的一些配置,以及一些提升性能的方案。
对于集群方式下的Redis性能测试,有待于后续补充。

上一篇 下一篇

猜你喜欢

热点阅读