[database] How do I move a redis database from one server to another?

I currently have a live redis server running on a cloud instance and I want to migrate this redis server to a new cloud instance and use that instance as my new redis server. If it were MySQL, I would export the DB from the old server and import it into the new server. How should I do this with redis?

P.S.: I'm not looking to set-up replication. I want to completely migrate the redis server to a new instance.

This question is related to database redis data-migration database-migration

The answer is


you can also use rdd

it can dump & restore a running redis server and allow filter/match/rename dumps keys


If you have the connectivity between servers it is better to set up replication (which is trivial, unlike with SQL) with the new instance as a slave node - then you can switch the new node to master with a single command and do the move with zero downtime.


The simple way I found to export / Backup Redis data (create dump file ) is to start up a server via command line with slaveof flag and create live replica as follow (assuming the source Redis is 1.2.3.4 on port 6379):

/usr/bin/redis-server --port 6399 --dbfilename backup_of_master.rdb --slaveof 1.2.3.4 6379

First, create a dump on server A.

A$ redis-cli
127.0.0.1:6379> CONFIG GET dir
1) "dir"
2) "/var/lib/redis/"
127.0.0.1:6379> SAVE
OK

This ensures dump.rdb is completely up-to-date, and shows us where it is stored (/var/lib/redis/dump.rdb in this case). dump.rdb is also periodically written to disk automatically.

Next, copy it to server B:

A$ scp /var/lib/redis/dump.rdb myuser@B:/tmp/dump.rdb

Stop the Redis server on B, copy dump.rdb (ensuring permissions are the same as before), then start.

B$ sudo service redis-server stop
B$ sudo cp /tmp/dump.rdb /var/lib/redis/dump.rdb
B$ sudo chown redis: /var/lib/redis/dump.rdb
B$ sudo service redis-server start

The version of Redis on B must be greater or equal than that of A, or you may hit compatibility issues.


It is also possible to migrate data using the SLAVEOF command:

SLAVEOF old_instance_name old_instance_port

Check that you have receive the keys with KEYS *. You could test the new instance by any other way too, and when you are done just turn replication of:

SLAVEOF NO ONE

redis-dump finally worked for me. Its documentation provides an example how to dump a Redis database and insert the data into another one.


To check where the dump.rdb has to be placed when importing redis data,

start client

$redis-cli

and

then

redis 127.0.0.1:6379> CONFIG GET *
 1) "dir"
 2) "/Users/Admin"

Here /Users/Admin is the location of dump.rdb that is read from server and therefore this is the file that has to be replaced.


Nowadays you can also use MIGRATE, available since 2.6.

I had to use this since I only wanted to move the data in one database and not all of them. The two Redis instances live on two different machines.

If you can't connect directly to Redis-2 from Redis-1, use ssh port binding:

 ssh [email protected] -L 1234:127.0.0.1:6379

A small script to loop all the keys using KEYS and MIGRATE each key. This is Perl, but hopefully you get the idea:

 foreach ( $redis_from->keys('*') ) {

    $redis_from->migrate(
        $destination{host},    # localhost in my example
        $destination{port},    # 1234
        $_,                    # The key
        $destination{db},
        $destination{timeout} 
    );
 }

See http://redis.io/commands/migrate for more info.


I also want to do the same thing: migrate a db from a standalone redis instance to a another redis instances(redis sentinel).

Because the data is not critical(session data), i will give https://github.com/yaauie/redis-copy a try.


I just published a command line interface utility to npm and github that allows you to copy keys that match a given pattern (even *) from one Redis database to another.

You can find the utility here:

https://www.npmjs.com/package/redis-utils-cli


Key elements of a zero-downtime migration is:

In short:

  1. setup a target redis (empty) as slave of a source redis (with your data)
  2. wait for replication finish
  3. permit writes to a target redis (which is currently slave)
  4. switch your apps to a target redis
  5. wait for finish datastream from master to slave
  6. turn a target redis from master to slave

Additionally redis have options which allows to disable a source redis to accept writes right after detaching a target:

  • min-slaves-to-write
  • min-slaves-max-lag

This topic covered by

Very good explanation from RedisLabs team https://redislabs.com/blog/real-time-synchronization-tool-for-redis-migration (use web.archive.org)

And even their interactive tool for migrate: https://github.com/RedisLabs/redis-migrate


Examples related to database

Implement specialization in ER diagram phpMyAdmin - Error > Incorrect format parameter? Authentication plugin 'caching_sha2_password' cannot be loaded Room - Schema export directory is not provided to the annotation processor so we cannot export the schema SQL Query Where Date = Today Minus 7 Days MySQL Error: : 'Access denied for user 'root'@'localhost' SQL Server date format yyyymmdd How to create a foreign key in phpmyadmin WooCommerce: Finding the products in database TypeError: tuple indices must be integers, not str

Examples related to redis

System has not been booted with systemd as init system (PID 1). Can't operate Could not connect to Redis at 127.0.0.1:6379: Connection refused with homebrew How to connect to remote Redis server? WRONGTYPE Operation against a key holding the wrong kind of value php How to store and retrieve a dictionary with redis Start redis-server with config file Linux - Install redis-cli only how to check redis instance version? MISCONF Redis is configured to save RDB snapshots How to get all keys with their values in redis

Examples related to data-migration

What is the exact location of MySQL database tables in XAMPP folder? How to move Jenkins from one PC to another SQL Server String or binary data would be truncated How do I move a redis database from one server to another? How to group by week in MySQL? Exporting data In SQL Server as INSERT INTO

Examples related to database-migration

Rollback one specific migration in Laravel Access denied for user 'homestead'@'localhost' (using password: YES) How do I enable EF migrations for multiple contexts to separate databases? EF Migrations: Rollback last applied migration? Reset Entity-Framework Migrations How do I move a redis database from one server to another? How do I copy SQL Azure database to my local development server? How to export SQL Server database to MySQL?