Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

  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 cross table of db
  3. Redis key is primary key in MySQL, and value is hash containing other fields in MySQL
  4. When power off, less than one minute data lose is acceptable.

My solution is:

  1. Redis writes AOF file, some process will monitor this file and sync the updated datas to MySQL
  2. Hack Redis to write AOF in several files, just like MySQL binlog
  3. The data interface will only read and write through Redis

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

like image 387
pengdu Avatar asked Apr 15 '14 10:04

pengdu


People also ask

Is Redis cache faster than MySQL?

In terms of the efficiency of updating databases, Redis is superior to MySQL while SQLite is slowest. However, in terms of the efficiency of querying from databases, SQLite seems to be about ten times faster than Redis and MySQL.

Is Redis better than MySQL?

Overall, you don't really compare Redis and MySQL : if you want reliable relational storage, you use MySQL (or another DBMS) if you can afford to lose a few seconds of data in case of crash and you only need to fast store and retrieve key-value pairs, then Redis might be interesting.

Can Redis be used with MySQL?

Redis is an open-source and in-memory data structure store that can be used for caching, real-time analytics, searching, and machine learning. Integrate Redis with PHP and MySQL will improve your application performance because Redis stores data in RAM. You can use it with databases like MySQL or MariaDB.


2 Answers

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!

like image 108
Javier Ramirez Avatar answered Oct 03 '22 16:10

Javier Ramirez


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

the stable version is 3.6.2

<dependency>     <groupId>com.moilioncircle</groupId>     <artifactId>redis-replicator</artifactId>     <version>3.6.2</version> </dependency> 
like image 36
Baoyi Chen Avatar answered Oct 03 '22 18:10

Baoyi Chen