如何调试/修复随机发生的Redis :: TimeoutError?

时间:2013-09-03 14:56:11

标签: ruby-on-rails ruby redis

我有一个使用redis的rails应用程序 - 但是 - 我不时会看到很多Redis::TimeoutError在这里和那里出现。在这种情况下没有模式。它既出现在Web应用程序中,也出现在后台作业中(使用sidekiq进行处理) - 不经常发生,但不时发生。

现在我不知道如何找出这个的根本原因,因此不知道如何解决它。

以下是我的设置背景:

redis实例在单独的物理服务器上运行,该服务器连接到私有本地1Gbit网络中的Web服务器和后台服务器。所有服务器都运行ubuntu 12.04。 redis版本是2.6.10。我使用像这样的初始化程序连接我的rails应用程序(3.2):

require 'redis'
require 'redis/objects'
REDIS = Redis.new(:url => APP_CONFIG['REDIS_URL'])
Redis.current = REDIS

这是redis-cli INFO

的输出
# Server
redis_version:2.6.10
redis_git_sha1:00000000
redis_git_dirty:0
redis_mode:standalone
os:Linux 3.2.0-38-generic x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:4.6.3
process_id:28475
run_id:d89bbb1b81d3169c4228cf23c0988ae437d496a1
tcp_port:6379
uptime_in_seconds:14913365
uptime_in_days:172
lru_clock:1507056

# Clients
connected_clients:233
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:19

# Memory
used_memory:801637360
used_memory_human:764.50M
used_memory_rss:594706432
used_memory_peak:4295394784
used_memory_peak_human:4.00G
used_memory_lua:31744
mem_fragmentation_ratio:0.74
mem_allocator:jemalloc-3.3.0

# Persistence
loading:0
rdb_changes_since_last_save:23166
rdb_bgsave_in_progress:0
rdb_last_save_time:1378219310
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:4
rdb_current_bgsave_time_sec:-1
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok

# Stats
total_connections_received:932395
total_commands_processed:3088408103
instantaneous_ops_per_sec:837
rejected_connections:0
expired_keys:31428
evicted_keys:3007
keyspace_hits:124093049
keyspace_misses:53060192
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:17651

# Replication
role:master
connected_slaves:1
slave0:192.168.0.2,6379,online

# CPU
used_cpu_sys:54000.21
used_cpu_user:73692.52
used_cpu_sys_children:36229.79
used_cpu_user_children:420655.84

# Keyspace
db0:keys=1498962,expires=1310

在我的redis配置中,我有以下设置:

\fidaemonize yes
pidfile /var/run/redis/redis-server.pid
timeout 0
loglevel notice
databases 1
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /var/lib/redis
slave-serve-stale-data yes
slave-read-only yes
slave-priority 100
maxclients 1000
maxmemory 4GB
maxmemory-policy volatile-lru
appendonly no
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60 

4 个答案:

答案 0 :(得分:7)

这可能来自许多问题:

  • 因为您使用SAVE命令(它在您的conf中设置)生成大量I / O并锤击服务器,特别是如果您在Amazon上使用EBS卷。
  • 因为你有一个Redis奴隶(和以前一样,在镜像之前做SAVE)。
  • 因为您使用的KEY *在很多索引上都很慢。

我写了一篇关于这些问题的文章,请参阅here

答案 1 :(得分:3)

  1. 在redis服务器上尝试“slowlog”命令,看看是否有一些“慢查询”。
  2. 发生“TimeoutError”时写一些日志,看看“慢速日志”中的“错误redis命令”。
  3. 调整客户端的超时设置。

答案 2 :(得分:1)

如果服务器正常运行,则客户端可能存在问题。每个redis客户端实例,而不是服务器,也有超时设置,默认设置非常短 - 类似于几毫秒。因此,如果服务器在该时间内没有响应,则客户端将引发Redis :: TimeoutError。

你可以尝试的第一件事是设置一个更长的超时值,看看情况是否会好转。

redis_url = 'redis://user:password@host:port/'
redis = Redis.connect(:url => redis_url, :timeout => 0.7)

即使设置了更长的超时,也无法保证不会发生超时,但这会是系统设计的问题。

答案 3 :(得分:0)

您是否正在编写自己的代码以连接到redis或只是让sidekiq处理它?我认为你应该设计你的连接代码,以便在连接丢失时重新连接。您可以解救Redis :: BaseConnectionError并重新连接。