Migrating from MySQL to Redis

Debates on using relational DBs vs KV Stores apart, In this post, I’d like to share how can you migrate all your data from a MySQL table to Redis - a super fast key-value store; leaving the decision of whether migration is good/bad for your environment up to YOU.

In this tutorial, I have mapped my mysql attributes to a Redis hash map data type with my primary key as a hash key using HMSET, which is one of the commands Redis provides to create redis hashes.

MySQL Table Schema and Data:

Now, this is my data ‘transformation’ script, if you will; that reads the rows from MySQL table and generates full HMSET command for every row using MySQL’s CONCAT:

And the final step would be to pipe the output of this transform script to redis-cli by running the following command

On successful completion, all your data should in Redis and you can check by either running INFO or HGETALL (eg. HGETALL iid:21a134) from redis-cli

image

Now, what do you do with it? Redis-cli is kinda like mysql cli prompt, you can’t do much. You have to use language specific clients (similar to mysql clients) from your code to communicate with Redis. Redis clients are available for almost all well known programming languages and are listed HERE. And of course, first thing you’ll observe with your Redis-powered environment is very fast read/write speeds relative to MySQL DBs.