0

我有一个 python 应用程序,它定期将一些数据写入 AWS 弹性缓存集群。

这是一个模拟我的应用程序功能的示例脚本。它还复制了我一直面临的错误。

from datetime import datetime
import redis
import time
import sys
import random

class Config:
    default_host = "localhost"
    master_host = "xxx.xx.0001.xxxx.cache.amazonaws.com"
    replica_host = "xxx.xx.0001.xxxx.cache.amazonaws.com"
    redis_db = 8
    socket_conn_timeout = 10
    request_delay_sec = 0.1

def get_redis_client():
    return redis.Redis(
        host=Config.master_host,
        db=Config.redis_db,
        socket_connect_timeout=Config.socket_conn_timeout,
    )

def get_random_key_value():
    val = time.time()
    key = "test_key_" + str(random.randint(0, 100))
    return key, val

r = get_redis_client()

r.flushdb()

flag = False

while True:
    try:
        if flag:
            print("beat:", time.time())
        r.set(*get_random_key_value())
        time.sleep(Config.request_delay_sec)
    except redis.RedisError as re:
        print(datetime.now(), "Error:", type(re), re)
        flag = True
        # sys.exit()
    except KeyboardInterrupt:
        print("Stopping loop execution")
        sys.exit()

这是我的应用程序的环境详细信息

  • Python(v 3.7.0)
  • Redis-py(v 3.5.3)
  • AWS 弹性缓存(禁用集群模式,1 个主节点,1 个只读副本)

当我垂直扩展我的 AWS 弹性缓存集群并且上面的脚本正在运行时,我在集群扩展过程中收到以下错误几秒钟,然后它就消失了。

<class 'redis.exceptions.ReadOnlyError'> You can't write against a read only replica.

AWS 文档还指出,在垂直扩展过程中,由于数据同步可能会出现一些不一致(https://docs.aws.amazon.com/AmazonElastiCache/latest/red-ug/redis-cluster-vertical-scaling.html

有没有人遇到过类似的问题,或者可以解释为什么在放大过程中会出现这个错误?如何修复?

编辑:我用 golang 脚本尝试了同样的事情,它工作得很好。

4

0 回答 0