redis性能优化--已整合

1. 禁用centos的swap

We recommend that you use a swappiness of 0 when your data always fits into the RAM and 1 when you are not sure.

/etc/sysctl.conf

vm.swappiness=0

 

2. 当redis启用backup时,redis的maxmemory不要超过总内存的50%;

可以考虑在/etc/sysctl.conf设置vm.overcommit_memory=1

 

 3. 禁用一些涉及安全的命令

创建一个配置文件renamed-commands.conf,内容如下:
rename-command FLUSHDB e0cc96ad2eab73c2c347011806a76b73
rename-command FLUSHALL a31907b21c437f46808ea49322c91d23a
rename-command CONFIG ""
rename-command KEYS ""
rename-command DEBUG ""
rename-command SAVE ""

编辑redis.conf配置文件,添加下面一行:

include /path/to/chapter7/rename-commands.conf

重启redis服务

解释:

• FLUSHDB/FLUSHALL: These commands are very critical. Since they delete all of your data in Redis, you should disable/rename them.
• CONFIG: Ideally, you would disable/rename the command CONFIG in production, because it gives access to all the options set in your redis.conf file to the client.
• KEYS: This command will block Redis while it is executing, and since this command runs in linear time over all existing keys, it is recommended that you disable/rename it.
• DEBUG: This command can force a crash in the redis-server (DEBUG SEGFAULT), and you should disable/rename it.
• SAVE: This command should be disabled in production since it will block all other clients during the process of saving the data in a file. Also, we recommend that you use BGSAVE instead.

 

4. redis persistence的两种方式:

Redis Database (RDB) and Append-only File (AOF).

RDB与AOF的比较:RDB的加载速度比AOF的快;但AOF的数据完整性比RDB高;

使用场景:

1. If your application does not need persistence, disable RDB and AOF
2. If your application has tolerance to data loss, use RDB
3. If your application requires fully durable persistence, use both RDB and AOF

RDB是在某个时间点将数据写入一个临时文件,持久化结束后,用这个临时文件替换上次持久化的文件,达到数据恢复。 
优点:使用单独子进程来进行持久化,主进程不会进行任何IO操作,保证了redis的高性能 
缺点:RDB是间隔一段时间进行持久化,如果持久化之间redis发生故障,会发生数据丢失。所以这种方式更适合数据要求不严谨的时候

Append-only file,将“操作 + 数据”以格式化指令的方式追加到操作日志文件的尾部,在append操作返回后(已经写入到文件或者即将写入),才进行实际的数据变更,“日志文件”保存了历史所有的操作过程;当server需要数据恢复时,可以直接replay此日志文件,即可还原所有的操作过程。AOF相对可靠,它和mysql中bin.log、apache.log、zookeeper中txn-log简直异曲同工。AOF文件内容是字符串,非常容易阅读和解析。 
优点:可以保持更高的数据完整性,如果设置追加file的时间是1s,如果redis发生故障,最多会丢失1s的数据;且如果日志写入不完整支持redis-check-aof来进行日志修复;AOF文件没被rewrite之前(文件过大时会对命令进行合并重写),可以删除其中的某些命令(比如误操作的flushall)。 
缺点:AOF文件比RDB文件大,且恢复速度慢。

 

执行RDB的命令:BGSAVE(background save,不会阻塞redis;不要使用save,因为会阻塞redis);默认会生成一个dump.rdb的文件 

redis.conf配置文件,设置RDB:

save 900 1
save 300 10
save 60 10000

解释:

1. Save a .rdb file on disk every 900 seconds (15 minutes) if at least one write operation happens.
2. Save a .rdb file on disk every 300 seconds (5 minutes) if at least 10 write operations happen.
3. Save a .rdb file on disk every 60 seconds (1 minute) if at least 10,000 write operations happen.

redis.conf中与RDB相关的配置参数:

1. stop-writes-on-bgsave-error: The possible values for this are yes or no. This option makes Redis stop accepting writes if the last background save has failed. Redis starts accepting writes again after a background
save succeeds. The default value is yes
2. rdbcompression: The possible values for this are yes or no. When this option is set to yes, Redis uses LZF compression for the .rdb files. The default value is yes
3. rdbchecksum: The possible values for this are yes or no. When it is set to yes, Redis saves a checksum at the end of the .rdb file and performs a checksum before loading the .rdb file. Redis does not start if the RDB checksum does
not match with the one in the file. The default value is yes
4. dbfilename: This option sets the .rdb filename. The default value is dump.rdb
5. save: This option configures the snapshot frequency, based on the number of seconds and changes. It can be specified multiple times. The default values are save 3600 1, save 300 100, and save 60 10000
6. dir: This specifies the directory location of the AOF and RDB files

 

redis.conf中关于AOF的配置参数

1. appendonly: This will enable or disable AOF. The options available are yes and no. By default, AOF is disabled
2. appendfilename: This specifies the AOF filename. This field is for a filename only, not a file path. It is empty by default

3. appendfsync: Redis uses a background thread to perform fsync() in the main process (fsync() is a system call that tells the OS to flush data to disk). Redis allows you to configure the fsync policy in three possible ways:
no: Do not execute fsync(); let the OS decide when to flush the data. This is the fastest option
always: Execute fsync() after every write. This is the slowest option, but also the safest
everysec: Execute fsync() once every second. This still provides good write performance. This is the default value

4. no-appendfsync-on-rewrite: The possible values are yes and no. If the appendfsync policy is set to everysec or always and a background save or AOF log rewrite is taking place, Redis may block due to a lot of disk I/O (the fsync() syscall will be long). You should only enable this option if you have latency problems. The default value is no
5. auto-aof-rewrite-percentage: The valid values range from 0 to 100. Redis is able to automatically rewrite the log file by implicitly executing the command BGREWRITEAOF when the AOF size grows by the specified percentage. The default value is 100
6. auto-aof-rewrite-min-size: This is the minimum size for AOF to be rewritten. This prevents AOF rewrites until the specified minimum size is reached, even if the specified auto-aof-rewrite-percentage value is exceeded. The default value is 67,108,864 bytes
7. aof-load-truncated: The possible values are yes and no. In the event of a crash, the AOF may get truncated, and this option specifies whether Redis should load the truncated AOF on startup or exit with an error. When the value is yes, Redis will load the truncated file and emit an error message. When it is no, Redis will exit with an error and not load the truncated file
8. dir: This specifies the directory location of the AOF and RDB files

 

5. redis replication

将redis从改为主

将原来的主节点设为debug模式

$ redis-cli -p 5555 DEBUG SEGFAULT

将原来的6666从节点,设为主节点
$ redis-cli -p 6666 SLAVEOF NO ONE

将7777从节点的主指向6666新主节点
$ redis-cli -p 7777 SLAVEOF 127.0.0.1 6666

 

在Redis Sentinel环境下,jedis该如何配置

https://blog.csdn.net/varyall/article/details/79740728

posted on 2018-06-06 20:00  yyyyuuuu386  阅读(199)  评论(0编辑  收藏  举报

导航