遷移至 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 -Redis 在 Azure vm 上執行的快取、AWS EC2 等等。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 downtime. 現有快取的不中斷作業。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. 另一個 Redis 快取可以讀取此檔案(稱為 RDB)。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 ',而且會位於 redis 設定檔中指定的路徑。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 export 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 做為會話存放區,而應用程式會話有效期限為七天。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 實例複製到另一個實例。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.