SoFunction
Updated on 2025-03-04

Detailed explanation of various methods of Redis data migration

introduction

In modern distributed systems, Redis is a high-performance key-value storage database and is widely used in cache, message queues, session storage and other scenarios. As the business develops, the data migration requirements for Redis instances are becoming more and more common. Whether it is replacing hardware, upgrading Redis version, or performing data sharding, data migration is an inevitable link. This article will introduce in detail the various methods of Redis data migration and help you easily complete migration tasks through command-line tools.

1. Background and requirements of Redis data migration

1.1 Why do data migration be needed?

The demand for Redis data migration usually originates from the following scenarios:

  • Hardware upgrade: Replace a higher performance server.
  • Redis version upgrade: Upgrade to a new version of Redis to support more features.
  • Data sharding: Distribute data to multiple Redis instances for horizontal scaling.
  • Disaster recovery backup: Migrate data to alternate instances to improve system availability.
  • Business adjustments: Migrate data from one environment to another (such as from a test environment to a production environment).

1.2 The Challenge of Data Migration

  • Data consistency: During the migration process, how to ensure the consistency of data is a key issue.
  • Migration efficiency: For large-scale data, the efficiency of migration directly affects the availability of the business.
  • Operational complexity: Different migration methods have different operation steps. Choosing the appropriate migration method can reduce the operation complexity.

2. Various methods of Redis data migration

Redis provides a variety of methods for data migration, each with its applicable scenarios and advantages and disadvantages. Below we will introduce these methods in detail and demonstrate how to operate through command line tools.

2.1 Use the MIGRATE command to migrate a single key

MIGRATEThe command is an atomic operation provided by Redis, which is used to migrate a single key from the source Redis instance to the target Redis instance. Its advantage is that it can directly transmit data through the network without the need for intermediate files.

2.1.1 Command format

MIGRATE target_host target_port key target_db timeout [COPY] [REPLACE]
  • target_host: The host name or IP address of the target Redis instance.
  • target_port: The port of the target Redis instance.
  • key: The key name to be migrated.
  • target_db: The database number of the target Redis instance.
  • timeout: Timeout time (milliseconds) for migration operation.
  • COPY: Optional parameter, retaining the keys in the source instance.
  • REPLACE: Optional parameter, if the key already exists in the target instance, replace it.

2.1.2 Example

Turn keymykeyMigrate from the current instance to the target instance192.168.1.2:6379

MIGRATE 192.168.1.2 6379 mykey 0 5000

2.1.3 Applicable scenarios

  • Migrate a small number of keys.
  • Scenarios that require atomic operation.

2.2 Use the DUMP and RESTORE commands to migrate the keys

DUMPandRESTOREThe command can serialize the value of the key and restore it to the target Redis instance. This approach is suitable for scenarios where a small number of keys need to be migrated.

2.2.1 Command format

  • Use on source Redis instanceDUMPCommand serialization key:
DUMP key
  • Use on target Redis instanceRESTORECommand recovery key:
RESTORE key ttl serialized_value [REPLACE]

2.2.2 Example

  • Serialize keys on source instancemykey
DUMP mykey
  • Recover key on the target instancemykey
RESTORE mykey 0 "\x00\x03foo\x06\x00\x8f\x7f\x9b\x7f\x9b\x7f"

2.2.3 Applicable scenarios

  • Migrate a small number of keys.
  • Scenarios where migration process needs to be manually controlled.

2.3 Migrate full data using BGSAVE and RDB files

BGSAVEThe command can save Redis's data to an RDB file, and then copy the RDB file to the target Redis instance and load it. This method is suitable for full data migration.

2.3.1 Operation steps

  • Generate RDB files on source Redis instance:
BGSAVE
  • The RDB file that will be generated (usually) Copy to the data directory of the target Redis instance.
  • Restart the target Redis instance and it will automatically load the RDB file.

2.3.2 Applicable scenarios

  • Full data migration.
  • Short downtimes can be accepted during the migration process.

2.4 Use the SLAVEOF command to synchronize data

SLAVEOFThe command can set the target Redis instance as the slave node of the source Redis instance, and then cancel the slave node relationship after the data synchronization is completed. This method is suitable for scenarios where data is synchronized in real time.

2.4.1 Operation steps

  • Execute on the target Redis instance:
SLAVEOF source_host source_port
  • Wait for data synchronization to complete.
  • Cancel the slave node relationship:
SLAVEOF NO ONE

2.4.2 Applicable scenarios

  • Scenarios where data is required in real time.
  • Data consistency needs to be ensured during the migration process.

2.5 Batch import data using the --pipe option of redis-cli

redis-cliof--pipeOptions can import data in batches and are suitable for large-scale data migration.

2.5.1 Operation steps

  • Generate data files on source Redis instance:
redis-cli --rdb 
  • useredis-cliImport the data file to the target Redis instance:
cat  | redis-cli -h target_host -p target_port --pipe

2.5.2 Applicable scenarios

  • Large-scale data migration.
  • Scenarios where data is imported efficiently.

2.6 Batch migration of multiple keys using SCAN and MIGRATE

If you need to migrate multiple keys, you can combineSCANandMIGRATECommands implement batch migration.

2.6.1 Operation steps

  1. useSCANThe command traverses all keys in the source Redis instance.
  2. Use for each keyMIGRATEThe command is migrated to the target Redis instance.

2.6.2 Example

redis-cli -h source_host -p source_port --scan --pattern "*" | while read key; do
  redis-cli -h source_host -p source_port MIGRATE target_host target_port $key 0 5000
done

2.6.3 Applicable scenarios

  • Migrate multiple keys.
  • Scenarios that require flexible control of the migration process.

3. How to choose the appropriate data migration method?

In practical applications, the following factors need to be considered when choosing a suitable data migration method:

  1. Data volume: If the data volume is small, you can use itMIGRATEorDUMP/RESTORE;If the data volume is large, you can use itBGSAVEorSLAVEOF
  2. Migration efficiency: For large-scale data migration,BGSAVEandSLAVEOFUsually more efficient.
  3. Data consistency: If data consistency is required,SLAVEOFIt is a good choice.
  4. Operational complexityMIGRATEandDUMP/RESTORESimple operation, suitable for quickly migrating a small amount of data.

4. Summary

Redis data migration is a common operation, but different migration methods are required for different scenarios. This article introduces six commonly used Redis data migration methods in detail, including:

  1. useMIGRATECommand to migrate a single key.
  2. useDUMPandRESTORECommand migration key.
  3. useBGSAVEand RDB files to migrate full data.
  4. useSLAVEOFCommand to synchronize data.
  5. useredis-cliof--pipeOption to import data in batches.
  6. useSCANandMIGRATEBatch migration of multiple keys.

Through the introduction of this article, I believe you have mastered the various methods of Redis data migration and can choose the most suitable migration plan according to actual needs.

The above is a detailed explanation of the various methods of Redis data migration. For more information about Redis data migration, please pay attention to my other related articles!