客户端。如何分离读/写操作?

标签 redis haproxy stackexchange.redis

我们使用 StackExchange.Redis 作为 redis 客户端。

是否可以告诉客户端使用 127.0.0.1:1001 严格用于写入,127.0.0.2 严格用于读取?

enter image description here

最佳答案

如果你的 redis 服务器 127.0.0.1:1002 是 slaveof 127.0.0.1:1001,那么你可以像那样执行你的“写入”命令

redisClient.StringSet("key", "value", flags: CommandFlags.DemandMaster);

另外,我注意到,redisClient 本质上会执行需要在 master 上“写入”的命令。对于“读取”命令,您可以编写

redisClient.StringGet("key", flags: CommandFlags.DemandSlave);

但是如果你的奴隶不可用,你会得到一个异常(exception)。另一种选择

CommandFlags.PreferSlave

来自文档:

This operation should be performed on the slave if it is available, but will be performed on a master if no slaves are available. Suitable for read operations only.

关于客户端。如何分离读/写操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44143463/

相关文章:

redis - celery 以 redis 作为代理,结果后端使用了太多的 redis 连接

c# - RedisClient.Get<T> C# 与 ServiceStack.Redis 的性能

redis - 解除对 redis 奴隶的奴役

ASP.net 应用程序连接 REDIS SocketFailure on Ping

node.js - 如何将 Node.js Express 应用程序部署在 HAProxy 后面?

load-balancing - 如何创建 float IP 并使用它来配置 HAProxy

ssl - 检查 haproxy 在运行时使用哪个证书的方法?

redis - 为什么 StackExchange.Redis 在 transaction.ExecuteAsync() 中频繁抛出 "Unexpected response to EXEC: MultiBulk: 0"异常?

redis - 使用 StackExchange.Redis 中的 REDIS Sets 命令

c# - Redis 弹出列表项 按项目数