What's the best strategy to sync Redis data to MySQL?

前端 未结 3 723
我在风中等你
我在风中等你 2021-01-31 04:40
  1. The use case is to use Redis to be local cache of MySQL
  2. The data format in MySQL is: a single primary key and several other fields. There will not be queries cros
相关标签:
3条回答
  • 2021-01-31 05:01

    When you update values in Redis,you can put the values in other 'Queue', such as List in Redis.Then consuming the values in Queue and update Mysql.

    If the Redis data isn't too much,just using a Scheduler to batch flush all data to Mysql.

    0 讨论(0)
  • 2021-01-31 05:08

    You don't need to hack anything ;)

    I am not entirely sure why you need the data on mysql. If I knew, maybe there would be a more suitable answer. In any case, as a generic answer you can use redis keyspace notifications

    You could subscribe to the commands HSET, HMSET, HDEL and DEL on your keys, so you would get a notification everytime a key is deleted or a hash value is set or removed.

    Note if you miss any notification you would have an inconsistency. So once in a while you could just use the SCAN command to go through all your keys and check on mysql if they need to be updated.

    Another strategy could be maintaining two separate structures. One would be the hash with the values, and the other would be a ZSET of all the values sorted by timestamp of update. The best way to keep both structures up to date would be to write two or three lua scripts (insert/update and delete) that would operate on the hash and the zset atomically.

    Then you can just periodically query the ZSET for the elements with a timestamp higher than your last sync operation, get all the keys that were updated (it would include deleted keys, unless you want to keep a second ZSET exclusively for those) and then just retrieve all the elements by key and sync to mysql.

    Hope it will work for you!

    0 讨论(0)
  • 2021-01-31 05:21

    you can implement redis replication protocol.
    but there is a github project for your demands.

    the stable version is 2.5.0

    <dependency>
        <groupId>com.moilioncircle</groupId>
        <artifactId>redis-replicator</artifactId>
        <version>2.5.0</version>
    </dependency>
    
    0 讨论(0)
提交回复
热议问题