Redis-py Read LOWEST_LATENCY or NEAREST



Redis py版本:4.2.0

我们如何启用redis py从LOWEST_LATENCY节点读取?我们使用的是AWS全局数据存储,所以我们想让redis-py从地理分布的最近节点中读取最近节点?

我想你做不到。

根据亚马逊的这篇文章,Python库集成了最初在该项目中实现的集群支持,因此用户不需要第三方库来支持集群。然而,在编写时,不支持最低延迟模式。支持的模式有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();

这显然被称为"最近",这可能意味着它最初与地纬度接近有关(但这只是猜测(:

/**
* 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(),定义如下。Comment有关于延迟测量的重要信息:

/**
* 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(),它返回按延迟排序的节点。但这种排序发生在库中。该库似乎实现了延迟排序,这在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;
}

很抱歉,如果你已经知道其中的一些,但当我看了一眼时,我想我会把它作为答案发布。

最新更新