Redis-py 读取 LOWEST_LATENCY 或 NEAREST

Redis-py Read LOWEST_LATENCY or NEAREST

Redis-py版本: 4.2.0

我们如何让redis-py从LOWEST_LATENCY节点读取数据?我们正在使用 AWS 全球数据存储,因此我们希望启用 redis-py 以从地理分布的最近节点的最近节点读取?

我认为你不能。

Python 库,根据 this Amazon article, integrated cluster support originally implemented in this project,因此用户不需要第 3 方库来支持集群。但是,在撰写本文时,不支持最低延迟模式。支持的模式有 PRIMARIESREPLICASALL_NODESRANDOM。请注意,其中一些包含在 Java 库中,例如 REPLICAS,我认为它是 Lettuce 中的 ANY_REPLICA

LOWEST_LATENCY 模式在 Java 库中实现,它似乎不是 AWS 支持或提供的参数,据我所知,它不存在于 Python 库中.

/**
 * Setting to read from the node with the lowest latency during topology discovery. Note that latency measurements are
 * momentary snapshots that can change in rapid succession. Requires dynamic refresh sources to obtain topologies and
 * latencies from all nodes in the cluster.
 * @since 6.1.7
 */
public static final ReadFrom LOWEST_LATENCY = new ReadFromImpl.ReadFromLowestCommandLatency();

这显然被称为 NEAREST,这可能意味着它最初以某种方式与地理空间接近度相关联(但这只是一个猜测):

/**
 * Setting to read from the node with the lowest latency during topology discovery. Note that latency measurements are
 * momentary snapshots that can change in rapid succession. Requires dynamic refresh sources to obtain topologies and
 * latencies from all nodes in the cluster.
 * @deprecated since 6.1.7 as we're renaming this setting to {@link #LOWEST_LATENCY} for more clarity what this setting
 *             actually represents.
 */
@Deprecated
public static final ReadFrom NEAREST = LOWEST_LATENCY;

查看 ReadFromLowestCommandLatency(),这是定义。评论包含有关延迟测量的重要信息:

/**
 * Read from the node with the lowest latency during topology discovery. Note that latency measurements are momentary
 * snapshots that can change in rapid succession. Requires dynamic refresh sources to obtain topologies and latencies from
 * all nodes in the cluster.
 */
static final class ReadFromLowestCommandLatency

所有这些 Readxxx 方法都以某种方式使用 getNodes(),其中 returns 个节点按延迟排序。但这种排序发生在图书馆。该库似乎实现了延迟排序,这在 Python 实现中没有完成。

// Returns the list of nodes that are applicable for the read operation. The list is ordered by latency.
List<RedisNodeDescription> getNodes();

我没有执行完整的代码分析,但是例如 TopologyComparators.java 中的这个方法似乎证实了这一点:

/**
 * Sort partitions by latency.
 * @param clusterNodes
 * @return List containing {@link RedisClusterNode}s ordered by latency
 */
public static List<RedisClusterNode> sortByLatency(Iterable<RedisClusterNode> clusterNodes) {
    List<RedisClusterNode> ordered = LettuceLists.newList(clusterNodes);
    ordered.sort(LatencyComparator.INSTANCE);
    return ordered;
}

抱歉,如果您已经了解了其中的一些内容,但当我看了一下时,我想我会 post 它作为答案。