1. # Redis 配置文件


  2. # 当配置中需要配置内存大小时,可以使用 1k, 5GB, 4M 等类似的格式,其转换方式如下(不区分大小写)

  3. #

  4. # 1k =>

  5. 1000 bytes

  6. # 1kb => 1024 bytes

  7. # 1m => 1000000 bytes

  8. # 1mb =>

  9. 1024*1024 bytes

  10. # 1g => 1000000000 bytes

  11. # 1gb => 1024*1024*1024

  12. bytes

  13. #

  14. # 内存配置大小写是一样的.比如 1gb 1Gb 1GB 1gB


  15. # daemonize no 默认情况下,redis不是在后台运行的,如果需要在后台运行,把该项的值更改为yes

  16. daemonize

  17. yes


  18. # 当redis在后台运行的时候,Redis默认会把pid文件放在/var/run/redis.pid,你可以配置到其他地址。

  19. #

  20. 当运行多个redis服务时,需要指定不同的pid文件和端口

  21. pidfile /var/run/redis.pid


  22. # 指定redis运行的端口,默认是6379

  23. port 6379


  24. # 指定redis只接收来自于该IP地址的请求,如果不进行设置,那么将处理所有请求,

  25. # 在生产环境中最好设置该项

  26. # bind

  27. 127.0.0.1


  28. # Specify the path for the unix socket that will be used to listen for

  29. #

  30. incoming connections. There is no default, so Redis will not listen

  31. # on a

  32. unix socket when not specified.

  33. #

  34. # unixsocket /tmp/redis.sock

  35. #

  36. unixsocketperm 755


  37. # 设置客户端连接时的超时时间,单位为秒。当客户端在这段时间内没有发出任何指令,那么关闭该连接

  38. # 0是关闭此设置

  39. timeout

  40. 0


  41. # 指定日志记录级别

  42. # Redis总共支持四个级别:debug、verbose、notice、warning,默认为verbose

  43. #

  44. debug  记录很多信息,用于开发和测试

  45. # varbose 有用的信息,不像debug会记录那么多

  46. #

  47. notice 普通的verbose,常用于生产环境

  48. # warning 只有非常重要或者严重的信息会记录到日志

  49. loglevel

  50. debug


  51. # 配置log文件地址

  52. # 默认值为stdout,标准输出,若后台模式会输出到/dev/null

  53. #logfile

  54. stdout

  55. logfile /var/log/redis/redis.log


  56. # To enable logging to the system logger, just set 'syslog-enabled' to

  57. yes,

  58. # and optionally update the other syslog parameters to suit your

  59. needs.

  60. # syslog-enabled no


  61. # Specify the syslog identity.

  62. # syslog-ident redis


  63. # Specify the syslog facility.  Must be USER or between LOCAL0-LOCAL7.

  64. #

  65. syslog-facility local0


  66. # 可用数据库数

  67. # 默认值为16,默认数据库为0,数据库范围在0-(database-1)之间

  68. databases 16


  69. ################################ 快照  

  70. #################################

  71. #

  72. # 保存数据到磁盘,格式如下:

  73. #

  74. #   save

  75. <seconds> <changes>

  76. #

  77. #   

  78. 指出在多长时间内,有多少次更新操作,就将数据同步到数据文件rdb。

  79. #   相当于条件触发抓取快照,这个可以多个条件配合

  80. #   

  81. #   

  82. 比如默认配置文件中的设置,就设置了三个条件

  83. #

  84. #   save 900 1  900秒内至少有1个key被改变

  85. #   save 300

  86. 10  300秒内至少有300个key被改变

  87. #   save 60 10000  60秒内至少有10000个key被改变


  88. save 900 1

  89. save 300 10

  90. save 60 10000


  91. # 存储至本地数据库时(持久化到rdb文件)是否压缩数据,默认为yes

  92. rdbcompression yes


  93. # 本地持久化数据库文件名,默认值为dump.rdb

  94. dbfilename dump.rdb


  95. # 工作目录

  96. #

  97. # 数据库镜像备份的文件放置的路径。

  98. #

  99. 这里的路径跟文件名要分开配置是因为redis在进行备份时,先会将当前数据库的状态写入到一个临时文件中,等备份完成时,

  100. #

  101. 再把该该临时文件替换为上面所指定的文件,而这里的临时文件和上面所配置的备份文件都会放在这个指定的路径当中。

  102. #

  103. #

  104. AOF文件也会存放在这个目录下面

  105. #

  106. # 注意这里必须制定一个目录而不是文件

  107. dir ./


  108. ################################# 复制

  109. #################################


  110. # 主从复制. 设置该数据库为其他数据库的从数据库.

  111. #

  112. 设置当本机为slav服务时,设置master服务的IP地址及端口,在Redis启动时,它会自动从master进行数据同步

  113. #

  114. # slaveof

  115. <masterip> <masterport>


  116. # 当master服务设置了密码保护时(用requirepass制定的密码)

  117. # slav服务连接master的密码

  118. #

  119. #

  120. masterauth <master-password>



  121. # 当从库同主机失去连接或者复制正在进行,从机库有两种运行方式:

  122. #

  123. # 1)

  124. 如果slave-serve-stale-data设置为yes(默认设置),从库会继续相应客户端的请求

  125. #

  126. # 2)

  127. 如果slave-serve-stale-data是指为no,出去INFO和SLAVOF命令之外的任何请求都会返回一个

  128. #    错误"SYNC with

  129. master in progress"

  130. #

  131. slave-serve-stale-data yes


  132. # 从库会按照一个时间间隔向主库发送PINGs.可以通过repl-ping-slave-period设置这个时间间隔,默认是10秒

  133. #

  134. #

  135. repl-ping-slave-period 10


  136. # repl-timeout 设置主库批量数据传输时间或者ping回复时间间隔,默认值是60秒

  137. #

  138. 一定要确保repl-timeout大于repl-ping-slave-period

  139. # repl-timeout 60


  140. ################################## 安全

  141. ###################################


  142. # 设置客户端连接后进行任何其他指定前需要使用的密码。

  143. #

  144. 警告:因为redis速度相当快,所以在一台比较好的服务器下,一个外部的用户可以在一秒钟进行150K次的密码尝试,这意味着你需要指定非常非常强大的密码来防止暴力破解

  145. #

  146. #

  147. requirepass foobared


  148. # 命令重命名.

  149. #

  150. # 在一个共享环境下可以重命名相对危险的命令。比如把CONFIG重名为一个不容易猜测的字符。

  151. #

  152. #

  153. 举例:

  154. #

  155. # rename-command CONFIG

  156. b840fc02d524045429941cc15f59e41cb7be6c52

  157. #

  158. #

  159. 如果想删除一个命令,直接把它重命名为一个空字符""即可,如下:

  160. #

  161. # rename-command CONFIG ""


  162. ################################### 约束

  163. ####################################


  164. # 设置同一时间最大客户端连接数,默认无限制,Redis可以同时打开的客户端连接数为Redis进程可以打开的最大文件描述符数,

  165. # 如果设置

  166. maxclients 0,表示不作限制。

  167. # 当客户端连接数到达限制时,Redis会关闭新的连接并向客户端返回max number of clients

  168. reached错误信息

  169. #

  170. # maxclients 128


  171. # 指定Redis最大内存限制,Redis在启动时会把数据加载到内存中,达到最大内存后,Redis会先尝试清除已到期或即将到期的Key

  172. #

  173. Redis同时也会移除空的list对象

  174. #

  175. #

  176. 当此方法处理后,仍然到达最大内存设置,将无法再进行写入操作,但仍然可以进行读取操作

  177. #

  178. #

  179. 注意:Redis新的vm机制,会把Key存放内存,Value会存放在swap区

  180. #

  181. #

  182. maxmemory的设置比较适合于把redis当作于类似memcached的缓存来使用,而不适合当做一个真实的DB。

  183. #

  184. 当把Redis当做一个真实的数据库使用的时候,内存使用将是一个很大的开销

  185. # maxmemory <bytes>


  186. # 当内存达到最大值的时候Redis会选择删除哪些数据?有五种方式可供选择

  187. #

  188. # volatile-lru ->

  189. 利用LRU算法移除设置过过期时间的key (LRU:最近使用 Least Recently Used )

  190. # allkeys-lru ->

  191. 利用LRU算法移除任何key

  192. # volatile-random -> 移除设置过过期时间的随机key

  193. #

  194. allkeys->random -> remove a random key, any key

  195. # volatile-ttl ->

  196. 移除即将过期的key(minor TTL)

  197. # noeviction -> 不移除任何可以,只是返回一个写错误

  198. #

  199. #

  200. 注意:对于上面的策略,如果没有合适的key可以移除,当写的时候Redis会返回一个错误

  201. #

  202. #       写命令包括: set setnx

  203. setex append

  204. #       incr decr rpush lpush rpushx lpushx linsert lset

  205. rpoplpush sadd

  206. #       sinter sinterstore sunion sunionstore sdiff sdiffstore

  207. zadd zincrby

  208. #       zunionstore zinterstore hset hsetnx hmset hincrby incrby

  209. decrby

  210. #       getset mset msetnx exec sort

  211. #

  212. # 默认是:

  213. #

  214. #

  215. maxmemory-policy volatile-lru


  216. # LRU 和 minimal TTL 算法都不是精准的算法,但是相对精确的算法(为了节省内存),随意你可以选择样本大小进行检测。

  217. #

  218. Redis默认的灰选择3个样本进行检测,你可以通过maxmemory-samples进行设置

  219. #

  220. # maxmemory-samples

  221. 3


  222. ############################## AOF ###############################



  223. #

  224. 默认情况下,redis会在后台异步的把数据库镜像备份到磁盘,但是该备份是非常耗时的,而且备份也不能很频繁,如果发生诸如拉闸限电、拔插头等状况,那么将造成比较大范围的数据丢失。

  225. #

  226. 所以redis提供了另外一种更加高效的数据库备份及灾难恢复方式。

  227. # 开启append

  228. only模式之后,redis会把所接收到的每一次写操作请求都追加到appendonly.aof文件中,当redis重新启动时,会从该文件恢复出之前的状态。

  229. #

  230. 但是这样会造成appendonly.aof文件过大,所以redis还支持了BGREWRITEAOF指令,对appendonly.aof 进行重新整理。

  231. #

  232. 你可以同时开启asynchronous dumps 和 AOF


  233. appendonly no


  234. # AOF文件名称 (默认: "appendonly.aof")

  235. # appendfilename appendonly.aof


  236. # Redis支持三种同步AOF文件的策略:

  237. #

  238. # no: 不进行同步,系统去操作 . Faster.

  239. # always:

  240. always表示每次有写操作都进行同步. Slow, Safest.

  241. # everysec: 表示对写操作进行累积,每秒同步一次.

  242. Compromise.

  243. #

  244. # 默认是"everysec",按照速度和安全折中这是最好的。

  245. #

  246. 如果想让Redis能更高效的运行,你也可以设置为"no",让操作系统决定什么时候去执行

  247. #

  248. 或者相反想让数据更安全你也可以设置为"always"

  249. #

  250. # 如果不确定就用 "everysec".


  251. # appendfsync always

  252. appendfsync everysec

  253. # appendfsync no


  254. # AOF策略设置为always或者everysec时,后台处理进程(后台保存或者AOF日志重写)会执行大量的I/O操作

  255. #

  256. 在某些Linux配置中会阻止过长的fsync()请求。注意现在没有任何修复,即使fsync在另外一个线程进行处理

  257. #

  258. #

  259. 为了减缓这个问题,可以设置下面这个参数no-appendfsync-on-rewrite

  260. #

  261. # This means that while

  262. another child is saving the durability of Redis is

  263. # the same as "appendfsync

  264. none", that in pratical terms means that it is

  265. # possible to lost up to 30

  266. seconds of log in the worst scenario (with the

  267. # default Linux

  268. settings).

  269. #

  270. # If you have latency problems turn this to "yes". Otherwise

  271. leave it as

  272. # "no" that is the safest pick from the point of view of

  273. durability.

  274. no-appendfsync-on-rewrite no


  275. # Automatic rewrite of the append only file.

  276. # AOF 自动重写

  277. #

  278. 当AOF文件增长到一定大小的时候Redis能够调用 BGREWRITEAOF 对日志文件进行重写

  279. #

  280. #

  281. 它是这样工作的:Redis会记住上次进行些日志后文件的大小(如果从开机以来还没进行过重写,那日子大小在开机的时候确定)

  282. #

  283. #

  284. 基础大小会同现在的大小进行比较。如果现在的大小比基础大小大制定的百分比,重写功能将启动

  285. #

  286. 同时需要指定一个最小大小用于AOF重写,这个用于阻止即使文件很小但是增长幅度很大也去重写AOF文件的情况

  287. # 设置 percentage

  288. 为0就关闭这个特性


  289. auto-aof-rewrite-percentage 100

  290. auto-aof-rewrite-min-size 64mb


  291. ################################## SLOW LOG

  292. ###################################


  293. # Redis Slow Log 记录超过特定执行时间的命令。执行时间不包括I/O计算比如连接客户端,返回结果等,只是命令执行时间

  294. #

  295. #

  296. 可以通过两个参数设置slow log:一个是告诉Redis执行超过多少时间被记录的参数slowlog-log-slower-than(微妙),

  297. #

  298. 另一个是slow log 的长度。当一个新命令被记录的时候最早的命令将被从队列中移除


  299. # 下面的时间以微妙微单位,因此1000000代表一分钟。

  300. #

  301. 注意制定一个负数将关闭慢日志,而设置为0将强制每个命令都会记录

  302. slowlog-log-slower-than 10000


  303. # 对日志长度没有限制,只是要注意它会消耗内存

  304. # 可以通过 SLOWLOG RESET

  305. 回收被慢日志消耗的内存

  306. slowlog-max-len 1024


  307. ################################ VM ###############################


  308. ### WARNING! Virtual Memory is deprecated in Redis 2.4

  309. ### The use of

  310. Virtual Memory is strongly discouraged.


  311. # Virtual Memory allows Redis to work with datasets bigger than the

  312. actual

  313. # amount of RAM needed to hold the whole dataset in memory.

  314. # In

  315. order to do so very used keys are taken in memory while the other keys

  316. # are

  317. swapped into a swap file, similarly to what operating systems do

  318. # with

  319. memory pages.

  320. #

  321. # To enable VM just set 'vm-enabled' to yes, and set the

  322. following three

  323. # VM parameters accordingly to your needs.


  324. vm-enabled no

  325. # vm-enabled yes


  326. # This is the path of the Redis swap file. As you can guess, swap

  327. files

  328. # can't be shared by different Redis instances, so make sure to use a

  329. swap

  330. # file for every redis process you are running. Redis will complain if

  331. the

  332. # swap file is already in use.

  333. #

  334. # The best kind of storage for the

  335. Redis swap file (that's accessed at random)

  336. # is a Solid State Disk

  337. (SSD).

  338. #

  339. # *** WARNING *** if you are using a shared hosting the default

  340. of putting

  341. # the swap file under /tmp is not secure. Create a dir with access

  342. granted

  343. # only to Redis user and configure Redis to create the swap file

  344. there.

  345. vm-swap-file /tmp/redis.swap


  346. # vm-max-memory configures the VM to use at max the specified amount

  347. of

  348. # RAM. Everything that deos not fit will be swapped on disk *if* possible,

  349. that

  350. # is, if there is still enough contiguous space in the swap

  351. file.

  352. #

  353. # With vm-max-memory 0 the system will swap everything it can. Not

  354. a good

  355. # default, just specify the max amount of RAM you can in bytes, but

  356. it's

  357. # better to leave some margin. For instance specify an amount of

  358. RAM

  359. # that's more or less between 60 and 80% of your free

  360. RAM.

  361. vm-max-memory 0


  362. # Redis swap files is split into pages. An object can be saved using

  363. multiple

  364. # contiguous pages, but pages can't be shared between different

  365. objects.

  366. # So if your page is too big, small objects swapped out on disk will

  367. waste

  368. # a lot of space. If you page is too small, there is less space in the

  369. swap

  370. # file (assuming you configured the same number of total swap file

  371. pages).

  372. #

  373. # If you use a lot of small objects, use a page size of 64 or 32

  374. bytes.

  375. # If you use a lot of big objects, use a bigger page size.

  376. # If

  377. unsure, use the default :)

  378. vm-page-size 32


  379. # Number of total memory pages in the swap file.

  380. # Given that the page

  381. table (a bitmap of free/used pages) is taken in memory,

  382. # every 8 pages on

  383. disk will consume 1 byte of RAM.

  384. #

  385. # The total swap size is vm-page-size *

  386. vm-pages

  387. #

  388. # With the default of 32-bytes memory pages and 134217728 pages

  389. Redis will

  390. # use a 4 GB swap file, that will use 16 MB of RAM for the page

  391. table.

  392. #

  393. # It's better to use the smallest acceptable value for your

  394. application,

  395. # but the default is large in order to work in most

  396. conditions.

  397. vm-pages 134217728


  398. # Max number of VM I/O threads running at the same time.

  399. # This threads

  400. are used to read/write data from/to swap file, since they

  401. # also encode and

  402. decode objects from disk to memory or the reverse, a bigger

  403. # number of

  404. threads can help with big objects even if they can't help with

  405. # I/O itself

  406. as the physical device may not be able to couple with many

  407. # reads/writes

  408. operations at the same time.

  409. #

  410. # The special value of 0 turn off threaded

  411. I/O and enables the blocking

  412. # Virtual Memory

  413. implementation.

  414. vm-max-threads 4


  415. ############################### ADVANCED CONFIG

  416. ###############################


  417. # 当hash中包含超过指定元素个数并且最大的元素没有超过临界时,

  418. #

  419. hash将以一种特殊的编码方式(大大减少内存使用)来存储,这里可以设置这两个临界值

  420. # Redis

  421. Hash对应Value内部实际就是一个HashMap,实际这里会有2种不同实现,

  422. #

  423. 这个Hash的成员比较少时Redis为了节省内存会采用类似一维数组的方式来紧凑存储,而不会采用真正的HashMap结构,对应的value

  424. redisObject的encoding为zipmap,

  425. #

  426. 当成员数量增大时会自动转成真正的HashMap,此时encoding为ht。

  427. hash-max-zipmap-entries

  428. 512

  429. hash-max-zipmap-value 64


  430. # list数据类型多少节点以下会采用去指针的紧凑存储格式。

  431. #

  432. list数据类型节点值大小小于多少字节会采用紧凑存储格式。

  433. list-max-ziplist-entries

  434. 512

  435. list-max-ziplist-value 64


  436. # set数据类型内部数据如果全部是数值型,且包含多少节点以下会采用紧凑格式存储。

  437. set-max-intset-entries

  438. 512


  439. # zsort数据类型多少节点以下会采用去指针的紧凑存储格式。

  440. #

  441. zsort数据类型节点值大小小于多少字节会采用紧凑存储格式。

  442. zset-max-ziplist-entries

  443. 128

  444. zset-max-ziplist-value 64


  445. # Redis将在每100毫秒时使用1毫秒的CPU时间来对redis的hash表进行重新hash,可以降低内存的使用

  446. #

  447. #

  448. 当你的使用场景中,有非常严格的实时性需要,不能够接受Redis时不时的对请求有2毫秒的延迟的话,把这项配置为no。

  449. #

  450. #

  451. 如果没有这么严格的实时性要求,可以设置为yes,以便能够尽可能快的释放内存

  452. activerehashing yes


  453. ################################## INCLUDES

  454. ###################################


  455. # 指定包含其它的配置文件,可以在同一主机上多个Redis实例之间使用同一份配置文件,而同时各个实例又拥有自己的特定配置文件

  456. #

  457. include /path/to/local.conf

  458. # include /path/to/other.conf