更新时间:2025-08-12 gmt 08:00
redis重试机制-j9九游会登录
redis客户端支持添加自动重试机制,确保在执行redis操作失败后重试特定次数,这样能大幅度降低暂时性故障影响。例如:发生瞬时的网络抖动、磁盘抖动导致服务暂时不可用或者调用超时的情况下,提高redis操作的成功概率。
连接dcs单机、主备、读写分离、proxy集群实例添加retry配置,退避策略为指数退避(exponentialbackoff),重试次数上限为3次,并通过retry_on_error配置指定了busyloadingerror,connectionerror,timeouterror三种错误才进行重试,代码片段如下:
retry = retry(exponentialbackoff(), 3)
pool = blockingconnectionpool(host=redis_host, port=redis_port,
password=redis_password,
max_connections=50, timeout=3,
socket_timeout=2,
socket_connect_timeout=2,
retry=retry,
retry_on_error=[busyloadingerror,
connectionerror,
timeouterror],
health_check_interval=60,
decode_responses=true)
连接dcs集群实例使用了集群错误重试配置(cluster_error_retry_attempts),当遇到timeouterror、connectionerror或clusterdownerror时进行重试,默认重试上限为3,代码如下:
client = redis(host=redis_host, port=redis_port,
password=redis_password,
cluster_error_retry_attempts=3,
health_check_interval=60, max_connections=50,
decode_responses=true)
可以搭配retry一起使用,示例代码如下:
from redis.backoff import exponentialbackoff from redis.retry import retry from redis.cluster import rediscluster rc = rediscluster(host='localhost', port=6379, retry=retry(exponentialbackoff(), 6), cluster_error_retry_attempts=1)
但是其最大重试次数为 retries * (cluster_error_retry_attempts 1),以上述示例代码为例,最大重试次数为12次。
相关文档
意见反馈
文档内容是否对您有帮助?
提交成功!非常感谢您的反馈,我们会继续努力做到更好!
您可在查看反馈及问题处理状态。
系统繁忙,请稍后重试
如您有其它疑问,您也可以通过华为云社区问答频道来与我们联系探讨