配置优化建议配置redis服务以守护进程启动 Redis默认不是以守护进程的方式运行,可以通过将配置项daemonize修改为yes,这样启动redis-server后会自动在后台运行。 安全配置 将bind配置为127.0.0.1可以避免redis受外部攻击。另外使用requirepass配置项,可以设置访问redis服务器数据时先要输入密码。 一个小遗憾是redis只支持在配置文件中使用明文保存访问密码,这里提供一个动态生成配置文件的思路增加安全性: 首先将redis.conf备份成redis.conf.tml,在里面的requirepass配置好密码密文。 在启动redis服务前使用其它程序读取redis.conf.tml的requirepass配置项,把密文解密,替换requirepass值生成redis配置文件redis.conf.,启动redis服务后把redis.conf删除,这样就达到保密效果。 设置最大内存及内存淘汰策略 为避免redis占用内存无限膨胀,导致把系统内存耗尽,建议将maxmemory设置为1024mb。(实际用ps命令查看,会发现redis-server最多会使用比maxmemory多一些的内存) 同时配置内存淘汰策略maxmemory-policy为allkeys-lru,让redis在内存满时在所有的key中使用LRU算法对数据进行淘汰。 日志文件配置 Redis提供了logfile配置项,可以指定日志输出位置。但默认情况下redis会把所有日志输出到同一个文件,天常日久,这个日志文件会越积越大。 建议修改redis的源码,把里面的redisLog函数改为按天输出日志。 持久化配置 由于我们只需要使用redis做数据库缓存,所以不需要持久化。也不需要担心redis重启出现“缓存雪崩”的现象,因为我们业务服务器有很多台,不会同时重启。 单台业务服务器tps有限,缓存清零对数据库也不会产生太大压力。 关闭持久化方法把原来的save配置屏蔽,增加save "" 慢查询配置 设置slowlog-log-slower-than 5000,把所有响应时间大于5ms的请求记录起来,方便出故障时定位问题。 高风险命令配置 有一些redis命令会消耗redis服务器比较多资源,导致查询缓存效率下降。为了防止新手误操作,我们可以把这些命令改名,配置如下: rename-command MONITOR "DANGEROUS_CMD_MONITOR" rename-command FLUSHALL "DANGEROUS_CMD_FLUSHALL" rename-command FLUSHDB "DANGEROUS_CMD_FLUSHDB" rename-command CONFIG "DANGEROUS_CMD_CONFIG" rename-command KEYS "DANGEROUS_CMD_KEYS"
优化前后性能测试比较
优化前:
[url=][/url]
[huangcihui:/home/huangcihui] redis-benchmark -h 127.0.0.1 -p 6379 -c 100 -n 100000 -k 1 -e -q -r 10000 -d 512PING_INLINE: 56148.23 requests per secondPING_BULK: 55617.35 requests per secondSET: 57570.52 requests per secondGET: 56085.25 requests per secondINCR: 55309.73 requests per secondLPUSH: 54764.51 requests per secondRPUSH: 57570.52 requests per secondLPOP: 54644.81 requests per secondRPOP: 54884.74 requests per secondSADD: 50327.12 requests per secondHSET: 58445.36 requests per secondSPOP: 53191.49 requests per secondLPUSH (needed to benchmark LRANGE): 54945.05 requests per secondLRANGE_100 (first 100 elements): 11693.17 requests per secondLRANGE_300 (first 300 elements): 3824.09 requests per secondLRANGE_500 (first 450 elements): 2342.19 requests per secondLRANGE_600 (first 600 elements): 1671.12 requests per secondMSET (10 keys): 40192.93 requests per second [huangcihui:/home/huangcihui] redis-benchmark -h 127.0.0.1 -p 6379 -c 100 -n 100000 -k 1 -e -q -r 10000 -d 512PING_INLINE: 55340.34 requests per secondPING_BULK: 54854.64 requests per secondSET: 53937.43 requests per secondGET: 54347.82 requests per secondINCR: 52910.05 requests per secondLPUSH: 54674.69 requests per secondRPUSH: 51894.13 requests per secondLPOP: 53676.86 requests per secondRPOP: 53022.27 requests per secondSADD: 53676.86 requests per secondHSET: 55401.66 requests per secondSPOP: 56085.25 requests per secondLPUSH (needed to benchmark LRANGE): 54347.82 requests per secondLRANGE_100 (first 100 elements): 11160.71 requests per secondLRANGE_300 (first 300 elements): 3383.98 requests per secondLRANGE_500 (first 450 elements): 2246.33 requests per secondLRANGE_600 (first 600 elements): 1592.66 requests per secondMSET (10 keys): 37622.27 requests per second [huangcihui:/home/huangcihui] redis-benchmark -h 127.0.0.1 -p 6379 -c 100 -n 100000 -k 1 -e -q -r 10000 -d 512PING_INLINE: 54734.54 requests per secondPING_BULK: 54024.85 requests per secondSET: 54854.64 requests per secondGET: 52798.31 requests per secondINCR: 55463.12 requests per secondLPUSH: 55432.37 requests per secondRPUSH: 55834.73 requests per secondLPOP: 54495.91 requests per secondRPOP: 53705.69 requests per secondSADD: 52521.01 requests per secondHSET: 54229.93 requests per secondSPOP: 54585.15 requests per secondLPUSH (needed to benchmark LRANGE): 55648.30 requests per secondLRANGE_100 (first 100 elements): 11225.86 requests per secondLRANGE_300 (first 300 elements): 3598.29 requests per secondLRANGE_500 (first 450 elements): 2222.77 requests per secondLRANGE_600 (first 600 elements): 1620.25 requests per secondMSET (10 keys): 38684.72 requests per second [url=][/url]
优化后:
[url=][/url]
[huangcihui:/home/huangcihui] redis-benchmark -h 127.0.0.1 -p 6379 -c 100 -n 100000 -k 1 -e -q -r 10000 -d 512 -a pass123PING_INLINE: 51361.07 requests per secondPING_BULK: 46838.41 requests per secondSET: 49043.65 requests per secondGET: 50150.45 requests per secondINCR: 51786.64 requests per secondLPUSH: 55493.89 requests per secondRPUSH: 50150.45 requests per secondLPOP: 56915.20 requests per secondRPOP: 55928.41 requests per secondSADD: 56369.79 requests per secondHSET: 58651.02 requests per secondSPOP: 57703.40 requests per secondLPUSH (needed to benchmark LRANGE): 56593.10 requests per secondLRANGE_100 (first 100 elements): 11723.33 requests per secondLRANGE_300 (first 300 elements): 3954.76 requests per secondLRANGE_500 (first 450 elements): 2504.95 requests per secondLRANGE_600 (first 600 elements): 1733.61 requests per secondMSET (10 keys): 44444.45 requests per second [huangcihui:/home/huangcihui] redis-benchmark -h 127.0.0.1 -p 6379 -c 100 -n 100000 -k 1 -e -q -r 10000 -d 512 -a pass123PING_INLINE: 57339.45 requests per secondPING_BULK: 56561.09 requests per secondSET: 56116.72 requests per secondGET: 56625.14 requests per secondINCR: 57142.86 requests per secondLPUSH: 59880.24 requests per secondRPUSH: 51387.46 requests per secondLPOP: 51599.59 requests per secondRPOP: 51334.70 requests per secondSADD: 55865.92 requests per secondHSET: 57937.43 requests per secondSPOP: 58719.91 requests per secondLPUSH (needed to benchmark LRANGE): 56625.14 requests per secondLRANGE_100 (first 100 elements): 11845.53 requests per secondLRANGE_300 (first 300 elements): 3999.20 requests per secondLRANGE_500 (first 450 elements): 2414.70 requests per secondLRANGE_600 (first 600 elements): 1702.16 requests per secondMSET (10 keys): 39494.47 requests per second [huangcihui:/home/huangcihui] redis-benchmark -h 127.0.0.1 -p 6379 -c 100 -n 100000 -k 1 -e -q -r 10000 -d 512 -a pass123PING_INLINE: 51786.64 requests per secondPING_BULK: 38417.21 requests per secondSET: 55524.71 requests per secondGET: 39047.25 requests per secondINCR: 44822.95 requests per secondLPUSH: 53276.50 requests per secondRPUSH: 58582.31 requests per secondLPOP: 57208.24 requests per secondRPOP: 55066.08 requests per secondSADD: 52910.05 requests per secondHSET: 55187.64 requests per secondSPOP: 57405.28 requests per secondLPUSH (needed to benchmark LRANGE): 57570.52 requests per secondLRANGE_100 (first 100 elements): 10960.11 requests per secondLRANGE_300 (first 300 elements): 3794.20 requests per secondLRANGE_500 (first 450 elements): 2355.44 requests per secondLRANGE_600 (first 600 elements): 1705.41 requests per secondMSET (10 keys): 44130.62 requests per second [url=][/url]
经优化后运行效率会有小提升 使用约定集群 Redis提供三种集群模式,分别是主从,哨兵,分片三种。但因为我们只打算做为单机缓存,所以不需要配置。 合理使用数据库和键名前缀区分业务 Redis提供了多数据库配置,最多支持256个数据库。我们可以规定不同业务模块使用不同的数据库,这样可以避免数据库主键名称冲突。 但即使同一业务模块,也经常容易出现主键名称相同的情况,所以键名需要制定一些规范:统一使用”前缀:” + 具体值。 例如set uname:13560453764 huangcihui 前缀可以使用excel管理起来,这样基本可以解决键名冲突问题。 失效时间 最好对所有键设置失效时间,失效时间最好是某个范围内的随机数,这样可以避免缓存同时失效的情况。
压测 部署redis前最好先执行压测命令,看一下性能是否有异常。如果有异常需要考虑是否调整操作系统参数。 redis-benchmark -h 127.0.0.1 -p 6379 -c 100 -n 100000 -k 1 -e -a pass123 -q 注意需要使用-k参数,设置长连接,不然测试结果性能会差很多。(我的机器上测试相差5倍)
|