在企业项目中,怎样保持redis双写一致性?有什么最实用的办法?

在企业项目中,怎样保持redis双写一致性?有什么最实用的办法?

开启事务,然后先写mysql,再写redis,这样一旦写redis失败,也可以回滚mysql中的数据

1.Redis主从复制
Redis主从复制是将一个Redis实例(即主节点)的数据自动同步到其他Redis实例(即从节点)的过程。在这种情况下,所有的写操作都发生在主节点上,并且所有的从节点都会从主节点接收并复制所有的写操作。这样可以确保在主节点和从节点之间数据的一致性。

2.Redis Sentinel
Redis Sentinel是一种用于在Redis实例之间提供高可用性和容错能力的系统。它通过监视Redis实例的状态并根据需要自动故障转移来确保服务的可用性。在使用Redis Sentinel时,所有写操作都应该发送到主节点,并且客户端应该从哨兵获取主节点的地址。

3.Redis Cluster
Redis Cluster是一种用于在多个Redis实例之间分配数据并提供高可用性和可扩展性的系统。它可以水平扩展以处理更大的负载,并且可以自动将数据分布到集群的多个节点上。在使用Redis Cluster时,所有的写操作都应该发送到正确的节点,并且客户端应该从集群中的某个节点获取信息。

  • 你可以看下这个问题的回答https://ask.csdn.net/questions/771143
  • 这篇博客也不错, 你可以看下数据库事务和redis最终一致性的解决方法
  • 除此之外, 这篇博客: 关于redis的访问控制中的 redis默认是开启保护模式的,这里模拟一下保护模式的效果。 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 模拟条件:

    #不绑定任何ip----1
    127.0.0.1:6370> config get bind
    1) "bind"
    2) ""
    #不设置密码
    127.0.0.1:6370> config get requirepass
    1) "requirepass"
    2) ""
    #启动保护模式
    127.0.0.1:6370> config get protected-mode
    1) "protected-mode"
    2) "yes"
    

    指定ip连接:

    [root@node03 ~]# redis-cli -h 192.168.25.66 -p 6370
    192.168.25.66:6370> keys *
    (error) DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 
    1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 
    2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 
    3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 
    4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.

    报错,并提供了四种从外网访问服务的解决办法。四种方法选择任意一种即可。

    • 通过命令CONFIG SET protected-mode no关闭保护模式
    • 修改配置文件protected-mode no重启服务
    • 重启服务,在启动命令中指定--protected-mode no,关闭保护模式
    • bind一个IP地址或者设置一个密码。