redis数据同步到mysql_将Redis数据同步到MySQL的最佳策略是什么?
发布日期:2021-06-24 13:28:03 浏览次数:2 分类:技术文章

本文共 1909 字,大约阅读时间需要 6 分钟。

bd96500e110b49cbb3cd949968f18be7.png

The use case is to use Redis to be local cache of MySQL

The data format in MySQL is: a single primary key and several other fields. There will not be queries cross table of db

Redis key is primary key in MySQL, and value is hash containing other fields in MySQL

When power off, less than one minute data lose is acceptable.

My solution is:

Redis writes AOF file, some process will monitor this file and sync the updated datas to MySQL

Hack Redis to write AOF in several files, just like MySQL binlog

The data interface will only read and write through Redis

Is this solution OK?

And what's the best strategy to do this job?

解决方案

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!

转载地址:https://blog.csdn.net/weixin_33178459/article/details/113197779 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:mysql5.7安装版卸载_MySQL5.7解压版安装与卸载教程
下一篇:mysql 主从配置 linux_Linux下mysql主从配置

发表评论

最新留言

不错!
[***.144.177.141]2024年04月22日 10时12分14秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章