17

I have been using Lettuce as a Redis client to talk to AWS Elasticache. The specific configuration that I am currently using is the Static Master/Slave with predefined node addresses. Recently, the primary node took a tumble kicking off a failover process and eventually causing all application write requests to fail with the following error:

redis.RedisCommandExecutionException: READONLY You can't write against a read only slave.

Since then, I have been doing some research and realized that Standalone Master/Slave is probably the configuration that fits the purpose of talking to Elasticache (in non-clustered mode) as according to the AWS docs, the client should always only talk to the primary endpoint - which gets updated to point to the new master in an event of a failover.

This has left me wondering, why does the author make the recommendation of using the Static Master/Slave with predefined node addresses method when using AWS Elasticache?

Any thoughts?

Configuration: 1 Master and 2 Slave nodes

4

1 回答 1

34

您的问题有两个答案,因为 AWS ElastiCache 可以以不同的方式使用:

  1. 仅使用主节点
  2. 使用主副本和副本

解释

AWS ElastiCache(非集群)带有自己的故障转移机制,当发生故障转移时不会通知您的应用程序。这取决于您的使用情况是好是坏:

大师专用

如果您想依赖故障转移并且您不想使用您的副本进行额外的读取,那么仅使用 master 是可行的方法。对于仅供主使用,您将客户端指向主要端点。如果 ElastiCache 发生故障转移,则会重置客户端连接。AWS 在幕后更新主终端节点,一旦客户端成功重新连接,您将再次与(新)主节点通信。

为什么在这种情况下不能使用副本?

唯一的拓扑源是 AWS ElastiCache 节点本身。lettuce 没有连接到 AWS 的 API(这永远不会发生)。Redis 在该部分中公开了连接的副本,INFO REPLICATION但是: ElastiCache Redis 节点报告无法访问的副本 IP 地址,因此无法通过拓扑发现连接到这些节点。

使用主副本和副本

尽管无法从 ElastiCache 服务器推导出副本端点,但仍然可以提供静态端点。Lettuce 连接到所有节点并在启动时确定节点角色。这允许再次根据节点角色进行路由。如果发生故障转移(如您的情况),Lettuce 不会收到有关故障转移的通知并坚持初始拓扑。

故障转移通知

故障转移通知是缺失的部分。虽然 Redis Sentinel 提供指示升级/角色更改的通知,但没有“仅”主/副本的机制。你可以说:好的,让我们断开连接作为触发拓扑更新的信号。这在某些情况下可能有效,但在更多情况下(应用程序和 Redis 节点之间的网络分区、连接超时)它会在不需要的情况下触发更新。定期拓扑升级也只是尝试发现变化。

第三个答案

我对 AWS ElastiCache 实施不满意。它仅适用于 Master 使用,但只要您想使用副本,您就依赖于故障转移的专有实现。如果没有 AWS 故障转移(即在您自己的数据中心/Redis 设置中),一些运维人员会通知您 Redis 已关闭。他们要么重新启动 Redis 节点,要么重新启动应用程序以恢复操作。这些信号丢失了。

与此同时,AWS 提供了 Redis Cluster,它可能是更好的 HA/故障转移设置,但 Redis Cluster 对应用程序有严格的限制。也可以轮询 AWS 的 ElastiCache API 以从 API 端发现拓扑,然后启动拓扑更新(重新连接)。

Lettuce 用于静态拓扑的 Master/Replica API 至少提供了一种使用副本的方法。其他一切都源于这种经验。欢迎任何形式的贡献(经验、建议、文档、代码)。

更新:根据antirez/redis#5335对齐副本措辞

于 2016-12-09T09:39:41.110 回答