Redis关闭与客户端的连接,同时在后台保存更改的密钥

时间:2014-05-08 22:55:07

标签: redis

我在EC2上有一台Redis服务器,连接了2台app服务器。所有小/中等。交通不高;在300秒内只改变了10个键。我开始注意到应用程序服务器连接错误到Redis机器:

 Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool

起初我认为这是我的池配置或我用来与Redis接口的java客户端的问题,但是当我注意到两个应用服务器总是在同一时间生成这些异常时我很快就揭穿了这个理论他们总是聚集在一起。然后我查看了redis.log并注意到以下输出,同时出现错误:

[13939] 08 May 22:31:05.051 * 10 changes in 300 seconds. Saving... [13939] 08 May 22:31:05.342 * Background saving started by pid 13945 [13939] 08 May 22:31:09.357 - DB 0: 606477 keys (0 volatile) in 1048576 slots HT. [13939] 08 May 22:31:09.357 - 3 clients connected (0 slaves), 764180208 bytes in use [13939] 08 May 22:31:14.542 - DB 0: 606477 keys (0 volatile) in 1048576 slots HT. [13939] 08 May 22:31:25.947 - 3 clients connected (0 slaves), 764180208 bytes in use [13939] 08 May 22:31:25.947 - Error writing to client: Connection reset by peer [13939] 08 May 22:31:25.947 - Error writing to client: Connection reset by peer [13939] 08 May 22:31:25.947 - Error writing to client: Connection reset by peer [13939] 08 May 22:31:25.947 - Accepted 10.123.29.90:56301 [13939] 08 May 22:31:25.947 - Accepted 10.42.105.60:35315 [13939] 08 May 22:31:25.947 - Error writing to client: Connection reset by peer [13939] 08 May 22:31:25.947 - Accepted 10.123.29.90:56302 [13939] 08 May 22:31:25.947 - Error writing to client: Connection reset by peer [13939] 08 May 22:31:25.947 - Accepted 10.42.105.60:35317 [13939] 08 May 22:31:25.947 - Error writing to client: Connection reset by peer [13939] 08 May 22:31:25.947 - Accepted 10.123.29.90:56306 [13939] 08 May 22:31:25.948 - Error writing to client: Connection reset by peer [13939] 08 May 22:31:25.948 - Accepted 10.42.105.60:35318 [13939] 08 May 22:31:25.948 - Error writing to client: Connection reset by peer [13939] 08 May 22:31:25.948 - Accepted 10.123.29.90:56308 [13939] 08 May 22:31:25.948 - Error writing to client: Connection reset by peer [13939] 08 May 22:31:25.948 - Accepted 10.42.105.60:35319 [13939] 08 May 22:31:25.948 - Error writing to client: Connection reset by peer [13939] 08 May 22:31:25.948 - Accepted 10.42.105.60:35320 [13939] 08 May 22:31:25.948 - Error writing to client: Connection reset by peer [13939] 08 May 22:31:25.948 - Accepted 10.123.29.90:56310 [13939] 08 May 22:31:25.948 - Error writing to client: Connection reset by peer [13939] 08 May 22:31:25.948 - Accepted 10.42.105.60:35322 [13939] 08 May 22:31:27.652 - Accepted 10.42.105.60:35327 [13939] 08 May 22:31:27.872 - Accepted 10.42.105.60:35329 [13945] 08 May 22:31:27.926 * DB saved on disk

只有当Redis在后台保存新数据时才会出现错误。我正在使用Redis 2.6。任何帮助表示赞赏。

编辑:使用spring-data

在下面的Redis连接池配置
<bean id="redisPoolConfig" class="redis.clients.jedis.JedisPoolConfig" lazy-init="false"
      p:maxTotal="500"
      p:maxIdle="20"
      p:testOnBorrow="true"
      p:testOnCreate="true"
      p:testOnReturn="true"
      p:maxWaitMillis="30000"
    />
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
      p:hostName="${REDIS_HOST}"
      p:port="${REDIS_PORT}"
      p:usePool="true"
      p:poolConfig-ref="redisPoolConfig"
    />

1 个答案:

答案 0 :(得分:0)

我想我找到了答案。升级到2.8.9并重新启动后,日志提醒我操作系统将打开文件描述符的数量限制为1024,这可以通过正在运行的Redis实例轻松超过。所以可能发生的事情是后台保存过程正在打开新的文件描述符,这会阻止新连接被接受,因为每个新的客户端连接都会打开一个文件。通过ulimit -n将限制增加到10000后,一切似乎都正常运行。