问题描述
我对如何通过Node.js连接到AWS的ElastiCache Redis感到困惑.我已经成功地通过node_redis NPM成功连接到主主机(001),但是我无法使用ioredis的集群功能,因为显然ElastiCache并未实现CLUSTER命令.
I'm confused as to how to connect to AWS's ElastiCache Redis via Node.js. I've successfully managed to connect to the primary host (001) via the node_redis NPM, but I'm unable to use the clustering ability of ioredis because apparently ElastiCache doesn't implement the CLUSTER commands.
我认为必须有另一种方法,但是适用于Node的AWS开发工具包仅具有用于管理ElastiCache的命令,而不能用于实际连接到它.
I figured that there must be another way, but the AWS SDK for Node only has commands for managing ElastiCache, not for actually connecting to it.
在不使用CLUSTER的情况下,我担心如果主节点发生故障,我的应用程序将无法进行故障转移,因为我无法回退到其他群集.当主服务器切换时,我也从Redis客户端Error: READONLY You can't write against a read only slave.
收到错误消息,我不确定如何妥善处理.
Without using CLUSTER, I'm concerned that my app won't be able to fail over if the master node fails, since I can't fall back to the other clusters. I also get errors from my Redis client, Error: READONLY You can't write against a read only slave.
when the master switches, which I'm not sure how to handle gracefully.
我是否想得太多?我发现有关将ElastiCache Redis群集与Node.js一起使用的信息很少.
Am I overthinking this? I am finding very little information about using ElastiCache Redis clusters with Node.js.
推荐答案
我对此考虑过多.
Amazon ElastiCache for Redis将通过获取新的服务资源来修复该节点,然后将重定向该节点的现有DNS名称以指向新的服务资源.因此,Redis节点的DNS名称保持不变,但是Redis节点的IP地址会随着时间变化.如果您的复制组具有一个或多个只读副本,并且启用了多可用区,则在主节点发生故障的情况下,ElastiCache将自动检测到故障,选择一个副本并将其升级为新的主副本.它还将传播DNS,以便您可以继续使用主要端点,并且在升级之后,它将指向新提升的主要节点.有关更多详细信息,请参见本常见问题解答的多可用区"部分.如果在禁用多可用区的情况下选择"Redis复制"选项,则在主节点发生故障的情况下,您将可以选择启动故障转移到只读副本节点的选项.故障转移目标可以在同一区域或另一个区域中.要故障回复到原始区域,请将原始区域中的只读副本提升为主要副本.您可以选择构建应用程序,以强制Redis客户端库重新连接到修复的Redis服务器节点.这可能会有所帮助,因为某些Redis库在遇到通信错误或超时时将无限期停止使用服务器.
Amazon ElastiCache for Redis will repair the node by acquiring new service resources, and will then redirect the node's existing DNS name to point to the new service resources. Thus, the DNS name for a Redis node remains constant, but the IP address of a Redis node can change over time. If you have a replication group with one or more read replicas and Multi-AZ is enabled, then in case of primary node failure ElastiCache will automatically detect the failure, select a replica and promote it to become the new primary. It will also propagate the DNS so that you can continue to use the primary endpoint and after the promotion it will point to the newly promoted primary. For more details see the Multi-AZ section of this FAQ. When Redis replication option is selected with Multi-AZ disabled, in case of primary node failure you will be given the option to initiate a failover to a read replica node. The failover target can be in the same zone or another zone. To failback to the original zone, promote the read replica in the original zone to be the primary. You may choose to architect your application to force the Redis client library to reconnect to the repaired Redis server node. This can help as some Redis libraries will stop using a server indefinitely when they encounter communication errors or timeouts.
解决方案是仅连接到主主节点,而不在客户端使用任何群集.当主服务器发生故障时,将升级从属服务器并更新DNS,以便从属服务器将成为主要节点,而主机无需在客户端进行更改.
The solution is to connect to the primary master node only, without using any clustering on the client side. When the master fails, the slave is promoted and the DNS is updated so that the slave will become the primary node, without the host needing to change on the client's side.
为防止发生故障转移时的临时连接错误,可以在ioredis中添加一些配置:
To prevent temporary connectivity errors when the failover happens, you can add some configuration to ioredis:
var client = new Redis(port, host, {
retryStrategy: function (times) {
log.warn('Lost Redis connection, reattempting');
return Math.min(times * 2, 2000);
},
reconnectOnError: function (err) {
if (err.message.slice(0, targetError.length) === 'READONLY') {
// When a slave is promoted, we might get temporary errors saying
// READONLY You can't write against a read only slave. Attempt to
// reconnect if this happens.
log.warn('ElastiCache returned a READONLY error, reconnecting');
return 2; // `1` means reconnect, `2` means reconnect and resend
// the failed command
}
}
});
这篇关于通过Node.js连接到ElastiCache集群的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!