迁移到 Azure Cache for RedisMigrate to Azure Cache for Redis

本文介绍将在本地运行或在其他云服务中运行的现有 Redis 缓存迁移到 Azure Cache for Redis 的多种方法。This article describes a number of approaches to migrate an existing Redis cache running on-premises or in another cloud service to Azure Cache for Redis.

迁移方案Migration scenarios

开源 Redis 可以在许多计算环境中运行。Open-source Redis can run in many compute environments. 常见示例包括:Common examples include:

  • 本地 - 在专用数据中心中运行的 Redis 缓存。On-premises - Redis caches running in private datacenters.
  • 基于云的 VM - 在 Azure VM、AWS EC2 等上运行的 Redis 缓存。Cloud-based VMs - Redis caches running on Azure VMs, AWS EC2, and so on.
  • 托管服务 - 托管 Redis 服务,例如 AWS ElastiCache。Hosting services - Managed Redis services such as AWS ElastiCache.
  • 不同区域 - 位于另一个 Azure 区域的 Redis 缓存。Different regions - Redis caches located in another Azure region.

如果拥有此类缓存,则可以以最小中断或停机时间将其移至 Azure Cache for Redis。If you have such a cache, you may be able to move it to Azure Cache for Redis with minimal interruption or downtime.

迁移选项Migration options

可以通过多种方式从一个缓存切换到另一个缓存。There are different ways that you can switch from one cache to another. 根据缓存所在的位置以及应用程序与缓存的交互方式,其中一种方法将比其他方法更有用。Depending on where your cache is and how your application interacts with it, one method will be more useful than the others. 下面详细介绍一些常用的迁移策略。Some of the frequently used migration strategies are detailed below.

选项Option 优点Advantages 缺点Disadvantages
创建新缓存Create a new cache 最易于实现。Simplest to implement. 需要将数据重新填充到新的缓存中,这可能不适用于许多应用程序。Need to repopulate data to the new cache, which may not work with many applications.
通过 RDB 文件导出和导入数据Export and import data via RDB file 通常与任何 Redis 缓存兼容。Compatible with any Redis cache generally. 如果某些数据在生成 RDB 文件后写入现有缓存,这些数据可能会丢失。Some data could be lost, if they're written to the existing cache after the RDB file is generated.
将数据双重写入两个缓存Dual-write data to two caches 无数据丢失或停机时间。No data loss or downtown. 现有缓存不间断操作。Uninterrupted operations of the existing cache. 更轻松地测试新缓存。Easier testing of the new cache. 需要两个缓存以延长时间。Needs two caches for an extended period of time.
以编程方式迁移数据Migrate data programmatically 对数据的移动方式具有完全控制。Full control over how data are moved. 需要自定义代码。Requires custom code.

创建新的 Azure Cache for RedisCreate a new Azure Cache for Redis

从技术上讲,这种方法不是迁移。This approach technically isn't a migration. 如果无需担心丢失数据,则迁移 Azure Cache for Redis 的最简单方法是创建缓存实例并将应用程序连接到该实例。If losing data isn't a concern, the easiest way to move to Azure Cache for Redis is to create cache instance and connect your application to it. 例如,如果将 Redis 用作数据库记录的后备缓存,则可以轻松地从头开始重新生成缓存。For example, if you use Redis as a look-aside cache of database records, you can easily rebuild the cache from scratch.

实现此选项的一般步骤如下:General steps to implement this option are:

  1. 创建新的 Azure Cache for Redis 实例。Create a new Azure Cache for Redis instance.

  2. 更新应用程序以使用新实例。Update your application to use the new instance.

  3. 删除旧的 Redis 实例。Delete the old Redis instance.

将数据导出到 RDB 文件并将该文件导入 Azure Cache for RedisExport data to an RDB file and import it into Azure Cache for Redis

开源 Redis 定义了一种标准机制,用于获取缓存的内存中数据集的快照并将其保存到文件中。Open-source Redis defines a standard mechanism for taking a snapshot of a cache's in-memory dataset and saving it to a file. 此文件名为 RDB,可由另一个 Redis 缓存读取。This file, called RDB, can be read by another Redis cache. Azure Cache for Redis 高级层支持通过 RDB 文件将数据导入缓存实例。Azure Cache for Redis premium tier supports importing data into a cache instance via RDB files. 可以使用 RDB 文件将数据从现有缓存传输到 Azure Cache for Redis。You can use an RDB file to transfer data from an existing cache to Azure Cache for Redis.

重要

RDB 文件格式可以在 Redis 版本之间更改,并且可能无法保持向后兼容。RDB file format can change between Redis versions and may not maintain backward-compatibility. 要从中导出的缓存的 Redis 版本应等于或低于 Azure Cache for Redis 提供的版本。The Redis version of the cache you're exporting from should be equal or less than the version provided by Azure Cache for Redis.

实现此选项的一般步骤如下:General steps to implement this option are:

  1. 在高级层中创建与现有缓存大小相同(或大于现有缓存)的新 Azure Cache for Redis 实例。Create a new Azure Cache for Redis instance in the premium tier that is the same size as (or bigger than) the existing cache.

  2. 保存现有 Redis 缓存的快照。Save a snapshot of the existing Redis cache. 可以配置 Redis 以定期保存快照,或者使用 SAVEBGSAVE 命令手动运行该过程。You can configure Redis to save snapshots periodically, or run the process manually using the SAVE or BGSAVE commands. RDB 文件默认命名为“dump.rdb”,并将位于 redis.conf 配置文件中指定的路径下。The RDB file is named “dump.rdb” by default and will be located at the path specified in the redis.conf configuration file.

    备注

    如果要在 Azure Cache for Redis 中迁移数据,请参阅这些有关如何导出 RDB 文件的说明,或改为使用 PowerShell 导出 cmdletIf you’re migrating data within Azure Cache for Redis, see these instructions on how to export an RDB file or use the PowerShell Export cmdlet instead.

  3. 将 RDB 文件复制到新缓存所在区域中的 Azure 存储帐户。Copy the RDB file to an Azure storage account in the region where your new cache is located. 可以将 AzCopy 用于此任务。You can use AzCopy for this task.

  4. 使用这些导入说明PowerShell 导入 cmdlet 将 RDB 文件导入新缓存。Import the RDB file into the new cache using these import instructions or the PowerShell Import cmdlet.

  5. 更新应用程序以使用新缓存实例。Update your application to use the new cache instance.

在迁移期间同时写入两个 Redis 缓存Write to two Redis caches simultaneously during migration period

可以使用应用程序将数据写入现有缓存和要设置的新缓存,而不是直接在缓存之间移动数据。Rather than moving data directly between caches, you may use your application to write data to both an existing cache and a new one you're setting up. 应用程序一开始仍将从现有缓存中读取数据。The application will still read data from the existing cache initially. 当新缓存拥有必要的数据后,就可以将应用程序切换到该缓存,然后停用旧缓存。When the new cache has the necessary data, you switch the application to that cache and retire the old one. 举例来说,假设使用 Redis 作为会话存储,且应用程序会话的有效期为 7 天。Let's say, for example, you use Redis as a session store and the application sessions are valid for seven days. 写入两个缓存一周后,即可确定新缓存包含所有未过期会话信息。After writing to the two caches for a week, you'll be certain the new cache contains all non-expired session information. 此后,可放心地依靠它,而不必担心数据丢失。You can safely rely on it from that point onward without concern over data loss.

实现此选项的一般步骤如下:General steps to implement this option are:

  1. 在高级层中创建与现有缓存大小相同(或大于现有缓存)的新 Azure Cache for Redis 实例。Create a new Azure Cache for Redis instance in the premium tier that is the same size as (or bigger than) the existing cache.

  2. 修改应用程序代码以写入新实例和原始实例。Modify application code to write to both the new and the original instances.

  3. 继续从原始实例读取数据,直到新实例填充足够的数据为止。Continue reading data from the original instance until the new instance is sufficiently populated with data.

  4. 更新应用程序代码以仅从新实例读取和写入。Update the application code to reading and writing from the new instance only.

  5. 删除原始实例。Delete the original instance.

以编程方式迁移Migrate programmatically

可以通过编程方式从现有缓存中读取数据并将其写入 Azure Cache for Redis,从而创建自定义迁移过程。You can create a custom migration process by programmatically reading data from an existing cache and writing them into Azure Cache for Redis. 开源工具可用于将数据从一个 Azure Cache for Redis 实例复制到另一个 Azure Cache for Redis 实例。This open-source tool can be used to copy data from one Azure Cache for Redis instance to another. 此工具对于在不同 Azure 缓存区域中的缓存实例之间移动数据很有用。This tool is useful for moving data between cache instances in different Azure Cache regions. 还提供编译版本A compiled version is available as well. 你可能还会发现,源代码可为编写自己的迁移工具提供有用指导。You may also find the source code to be a useful guide for writing your own migration tool.

备注

Microsoft 尚未正式支持此工具。This tool isn't officially supported by Microsoft.

实现此选项的一般步骤如下:General steps to implement this option are:

  1. 在现有缓存​​所在区域中创建 VM。Create a VM in the region where the existing cache is located. 如果数据集很大,请选择功能相对强大的 VM 以减少复制时间。If your dataset is large, choose a relatively powerful VM to reduce copying time.

  2. 创建新的 Azure Cache for Redis 实例。Create a new Azure Cache for Redis instance.

  3. 刷新新缓存中的数据以确保其为空。Flush data from the new cache to ensure that it's empty. 由于复制工具本身不会覆盖目标缓存中的任何现有密钥,因此需要执行此步骤。This step is required because the copy tool itself doesn't overwrite any existing key in the target cache.

    重要

    确保不要从源缓存中刷新。Make sure to NOT flush from the source cache.

  4. 使用应用程序(例如上述开源工具)自动将数据从源缓存复制到目标缓存。Use an application such as the open-source tool above to automate the copying of data from the source cache to the target. 请记住,复制过程可能需要一段时间才能完成,具体取决于数据集的大小。Remember that the copy process could take a while to complete depending on the size of your dataset.

后续步骤Next steps

了解有关 Azure Cache for Redis 功能的详细信息。Learn more about Azure Cache for Redis features.