当前位置: 代码迷 >> 综合 >> 测试人员应该知道的Redis知识(十) Redis配置(下)
  详细解决方案

测试人员应该知道的Redis知识(十) Redis配置(下)

热度:28   发布时间:2024-02-27 01:55:30.0

一、概述

 

平时我们工作中除了基本的Redis命令外,我们对于Redis的一些基础配置也是需要有一定了解的,上期中给大家介绍了Units、Includes、Network等方面的相关的内容,今天会带大家针对Redis的配置文件剩余部分进行了解。

 

Reids配置文件是位于安装目录下的redis.conf,如果没有找到的话,可以从源码文件夹中copy,一般我们不会对原始文件进行修改,而是copy一份。

cp redis.conf myredis.conf

让Redis以制定配置文件启动的话,我们可以执行以下语句

./redis-server /path/to/redis.conf

 

二、SECURITY

# Warning: since Redis is pretty fast an outside user can try up to
# 1 million passwords per second against a modern box. This means that you
# should use very strong passwords, otherwise they will be very easy to break.
# Note that because the password is really a shared secret between the client
# and the server, and should not be memorized by any human, the password
# can be easily a long string from /dev/urandom or whatever, so by using a
# long and unguessable password no brute force attack will be possible.

我们可以看到,由于Redis速度很快,每秒可以尝试一百万个密码进行攻击。这意味着你应该使用很强的密码,否则很容易被破解。当然一般情况下,我们不会真的把安全策略做在Redis这一层。

 

在默认配置文件中,安全这块的配置也都是被注释掉的。

如果我们需要添加代码,我们可以连接上Redis后执行以下命令:???????

127.0.0.1:6379> CONFIG GET requirepass
1) "requirepass"
2) ""
127.0.0.1:6379> CONFIG SET requirepass "123456"
OK
127.0.0.1:6379>

之后我们再连上Redis,如果需要执行命令的话,需要先进行授权???????

 ~ redis-cli
127.0.0.1:6379> ping
(error) NOAUTH Authentication required.
127.0.0.1:6379> AUTH 123456
OK
127.0.0.1:6379> ping
PONG
127.0.0.1:6379>

 

三、MEMORY MANAGEMENT???????

# Set a memory usage limit to the specified amount of bytes.
# When the memory limit is reached Redis will try to remove keys
# according to the eviction policy selected (see maxmemory-policy).
#
# If Redis can't remove keys according to the policy, or if the policy is
# set to 'noeviction', Redis will start to reply with errors to commands
# that would use more memory, like SET, LPUSH, and so on, and will continue
# to reply to read-only commands like GET.
#
# This option is usually useful when using Redis as an LRU or LFU cache, or to
# set a hard memory limit for an instance (using the 'noeviction' policy).
#
# WARNING: If you have replicas attached to an instance with maxmemory on,
# the size of the output buffers needed to feed the replicas are subtracted
# from the used memory count, so that network problems / resyncs will
# not trigger a loop where keys are evicted, and in turn the output
# buffer of replicas is full with DELs of keys evicted triggering the deletion
# of more keys, and so forth until the database is completely emptied.
#
# In short... if you have replicas attached it is suggested that you set a lower
# limit for maxmemory so that there is some free RAM on the system for replica
# output buffers (but this is not needed if the policy is 'noeviction').
#
maxmemory <bytes>

设置Redis可以使用的最大内存数,单位是bytes。一旦达到内存使用上限,redis会试图移除内部数据,移除规则可以通过maxmemory-policy来指定。如果redis无法移除内存中的数据,那么当我们执行SET、LPUSH等写操作时,将会返回错误信息,但是对于GET这类的读操作则不会有影响。???????

# MAXMEMORY POLICY: how Redis will select what to remove when maxmemory
# is reached. You can select one from the following behaviors:
#
# volatile-lru -> Evict using approximated LRU, only keys with an expire set.
# allkeys-lru -> Evict any key using approximated LRU.
# volatile-lfu -> Evict using approximated LFU, only keys with an expire set.
# allkeys-lfu -> Evict any key using approximated LFU.
# volatile-random -> Remove a random key having an expire set.
# allkeys-random -> Remove a random key, any key.
# volatile-ttl -> Remove the key with the nearest expire time (minor TTL)
# noeviction -> Don't evict anything, just return an error on write operations.
#
# LRU means Least Recently Used
# LFU means Least Frequently Used
#
# Both LRU, LFU and volatile-ttl are implemented using approximated
# randomized algorithms.
#
# Note: with any of the above policies, Redis will return an error on write
#       operations, when there are no suitable keys for eviction.
#
#       At the date of writing these commands are: set setnx setex append
#       incr decr rpush lpush rpushx lpushx linsert lset rpoplpush sadd
#       sinter sinterstore sunion sunionstore sdiff sdiffstore zadd zincrby
#       zunionstore zinterstore hset hsetnx hmset hincrby incrby decrby
#       getset mset msetnx exec sort
#
# The default is:
#
maxmemory-policy noeviction

当内存达到上限时,Redis释放内存的策略主要有以下几种:

    volatile-lru:在设置了过期时间的key中使用LRU算法随机进行移除
            allkeys-lru:使用LRU算法移除key
            volatile-lfu:在设置了过期时间的key中使用LFU算法随机进行移除  
            allkeys-lfu:使用LFU算法移除key
            volatile-random:在设置了过期时间的key中随机进行移除
            allkeys-random:移除随机的key
            volatile-ttl:移除那些TTL值最小的key,即那些最近要过期的key
            noeviction:不进行移除。针对写操作,只是返回错误信息

由于默认策略是noeviction,但我们生产环境中一般不会选择此项配置,各位小伙伴也可以看下自己公司Redis此项配置。

 

四、CLIENTS???????

# Set the max number of connected clients at the same time. By default
# this limit is set to 10000 clients, however if the Redis server is not
# able to configure the process file limit to allow for the specified limit
# the max number of allowed clients is set to the current file limit
# minus 32 (as Redis reserves a few file descriptors for internal uses).
#
# Once the limit is reached Redis will close all the new connections sending
# an error 'max number of clients reached'.
#
maxclients 10000

设置Redis最大的客户端进行连接数。默认情况下为10000个客户端。当没有更多的可供的连接数时,redis会拒绝新的连接请求,并且向这些连接请求方发出 “max number of clients reached” 错误。

 

五、REPLICATION

主从相关配置,我们在之后的章节中进行介绍。

 

六、APPEND ONLY MODE

aof相关配置,我们在之后的章节中进行介绍。

 

 

七、总结

 

今天给大家介绍了一下redis的常用配置,如果大家还有别的什么问题,可以在评论中留言。