問題描述
- 用例是使用Redis作為MySQL的本地緩存
- MySQL 中的數據格式是:一個主鍵和幾個其他字段.不會有db的跨表查詢
- Redis key 是 MySQL 中的主鍵,value 是包含 MySQL 中其他字段的 hash
- 斷電時,數據丟失少于一分鐘是可以接受的.
我的解決方案是:
- 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 的最佳策略是什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!