将Redis数据同步到MySQL的最佳策略是什么? [英] What's the best strategy to sync Redis data to MySQL?
问题描述
- 用例是使用Redis作为MySQL的本地缓存
- MySQL中的数据格式是:单个主键和其他几个字段.不会有数据库的查询交叉表
- Redis键是MySQL中的主键,值是包含MySQL中其他字段的哈希值
- 断电时,丢失少于一分钟的数据是可以接受的.
我的解决方法是:
- Redis写入AOF文件,某些进程将监视该文件并将更新的数据同步到MySQL
- Hack Redis可以在多个文件中写入AOF,就像MySQL binlog
- 数据接口只能通过Redis进行读写
此解决方案可以吗?
而做这项工作的最佳策略是什么?
Is this solution OK?
And what's the best strategy to do this job?
推荐答案
您不需要破解任何东西;)
You don't need to hack anything ;)
我不完全确定为什么您需要mysql上的数据.如果我知道,也许会有一个更合适的答案.无论如何,作为通用答案,您可以使用 redis键空间通知
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
您可以在键上订阅命令HSET,HMSET,HDEL和DEL,这样,每次删除键或设置或删除哈希值时,您都会收到通知.
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.
请注意,如果您错过任何通知,将会出现不一致的情况.因此,有时您可以只使用SCAN命令浏览所有密钥,并检查mysql是否需要更新.
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.
另一种策略可能是维护两个单独的结构.一个是带有值的哈希,另一个是所有值的ZSET,这些值按更新时间戳排序.使两种结构保持最新状态的最佳方法是编写两个或三个lua脚本(插入/更新和删除),这些脚本可对散列和zset进行原子操作.
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.
然后,您可以定期在ZSET中查询时间戳比上次同步操作高的元素,获取所有已更新的键(它将包括已删除的键,除非您想专门为它们保留第二个ZSET)然后只需按键检索所有元素并同步到mysql.
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.
希望它会为您服务!
这篇关于将Redis数据同步到MySQL的最佳策略是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!