ElastiCache Redis Migration

ElastiCache Redis 支援offline/online to import/export Redis Cluster,但每一種做法都有其限制、及適用的場景。

Jerry’s Notes
What’s next?
7 min readApr 3, 2022

--

ElastiCache Redis 離線/線上(offline/online) 遷移的差異?

ElastiCache Redis 遷移(Migration),主要分為兩種做法為【 離線/線上(offline/online) 】,但都有其使用上的限制及適合的場景,詳細說明及比較如下。

離線(offline)遷移(Migration)

透過 Redis 原生快照備份的功能,將內存的數據,備份到 RDB 檔中,再透過備份檔還原到 Redis 集群中。

優點:

■ 安全性較高,對於運行中的 Redis 集群影響小。

■ 支援匯入/匯出(import/export) ElastiCache Redis 集群。

缺點:

■ 需要客戶自已去補差異的資料。

■ 前端服務影響時間較長。

限制:

■ ElastiCache Redis (已啓用集群模式) 集群不支持多個數據庫。因此,還原到 ElastiCache Redis (已啓用集群模式) 時,如果 .rdb 文件引用多個數據庫,還原將會失敗。原生 Redis 限制。

■ Redis 5 備份出來的 RDB 檔,無法 Redis 4 以下的版本來使用。舊版的 Redis Engine 不支援新版的 Redis Engine 創建出來的 RDB 檔。原生 Redis 限制。

■ ElastiCache Redis (已啓用集群模式) 集群創建出來的備份檔,無法拿來創建 ElastiCache Redis (非集群模式)。

線上(online)遷移(Migration)

線上遷移專門用於將資料從 Amazon EC2 上的託管 Redis(”自建” Redis 集群),遷移至ElastiCache for Redis,而非在 ElastiCache for Redis 叢集之間遷移。

優點:

■ 線上即時搬移數據,原Source”自建” Redis 集群,仍持續提供服務。

■ 前端服務影響時間較短、且Source ”自建” Redis 集群,跟 Targe “ElastiCache Redis 集群上的數據一致。

缺點:

■ 只支援從”自建” Redis 集群,線上遷移(Online Migration)到 ElastiCache Redis 集群。

”不支援”從 ElastiCache Redis 集群線上遷移(Online Migration)至”自建” Redis 集群。

■ 不支援 Cluster mode enabled ElastiCache Redis 集群。

■ 原Source Redis 集群在搬移數據,負載會增加。

線上遷移(Online Migration)限制?

Source EC2 Redis — ”自建” Redis 集群

Engine version 2.8.21+
AUTH is disable
Protected-mode directive = No

127.0.0.1:6379> config get protected-mode
protected-mode: yes -> NO
! 如果啟用了,則只能夠通過lookback ip(127.0.0.1)訪問Redis cache,如果從外網訪問,則會返回相應的錯誤資訊

 CPU and memory loading 不能太高
 logical databases 的數量要一致 source=target

Target ElastiCache Redis — ElastiCache Redis 集群

Engine version 5.0.5+
encryption at-rest: Disable 必需關閉
encryption in-transit: Disable必需關閉
Multi-AZ: Enable
Auto-Failover: Enable
Reserved memory: recommended 25% 確保有足夠的記憶體容量,可儲存來自您 Redis on EC2 執行個體的資料。
單節點 (不支援。)
Cluster mode : enable (不支援)
Cluster mode : disable -> 要滿足以上需求。

Q: ElastiCache Redis 如何遷移數據到自建環境?

A: 使用離線(offline)、匯入數據到RDB備份檔中。

1. 選中集群redis-spotlight 在Actions中選Backup並輸入備份名
2. 點擊 create backup
3. 從左側菜單進入Backups 選中您創建的備份,點擊Copy。這裡您可以重新命名您希望copy到S3 的備份名稱,並選擇您希望copy備份到的S3桶。選好後點Copy。您的備份會進入exporting狀態。
4. 進入S3存儲桶,您可以看到6個rdb文件分別對應您的6個節點 (這裡需要和您強調,我測試發現對於多shard分片的集群,每一個shard會備份成一個RDB文件。我的測試環境和您一樣是6個shard,每個shard一主一從共12個節點。最終在S3看到是6個RDB文件,每一個對應一個shard)
5. 後續您可以download 這些備份文件到您的EC2或者您本地並使用這些備份文件來創建您的自建集群。

Q: ElastiCache Redis如何進行跨賬戶遷移?

如何將一個賬號下的ElastiCache Redis數據,同步到另外一個賬號?

A: 目前AWS現在尚未有直接將 AWS 帳戶下的 ElastiCache Redis 遷移或是同步至另一個 AWS 帳戶下 ElastiCache Redis 集群的功能。

替代作法: 您可以在原 AWS 帳戶下的 ElastiCache Redis 集群,來為您要搬移的 ElastiCache Redis 集群進行手動備份的動作,然以將該備份導出至Amazon S3上,然後將該備份檔(.rdb),給予讀取權限給目標端的 AWS 帳戶,最後在目的地的 AWS 帳戶下,使用該備份檔來建立新的集群。

Q: 為什麼我使用 sync/psync 命令來同步數據,為什麼收到 “sync unknown command” 這樣的錯誤訊息?

A: 因為 Amazon ElastiCache 是 AWS 全託管的服務,所以 ElastiCache 限制了對某些需要高級特權的特定於緩存引擎的命令。故 sync/psync 是無法提供給客戶使用的權限。

最主要的原因是因為這些命令,當特定使用情況下,如下高內存使用率、高 CPU 負載、誤操作,容易造成節點異常或是失效,所以這些命令是被限制的。

[+] 受限 Redis 命令 :
https://docs.aws.amazon.com/zh_cn/AmazonElastiCache/latest/red-ug/RestrictedCommands.html

為了提供託管服務體驗,ElastiCache 限制了對某些需要高級特權的特定於緩存引擎的命令的訪問。對於運行 Redis 的緩存集群,以下命令不可用:
* save
* slaveof
* shutdown
* sync
….

另外,目前 Amazon ElastiCache Redis 也不支援,在不同 Redis 集群同步的功能,您必需自行開發程式,去讀取來源端 ElastiCache Redis 集群,然後寫入您自建的目標端 Redis 集群。

替代方案:

您可以考慮安排前端業務維護時間,然後透過手动备份,將您運行中的 Redis 集群進行備份,然後使用這個備份檔來創創建新的 Redis 集群,再去調整前端應用去使用新的 Redis 集群。以下相關文檔提供您參考。

--

--

Jerry’s Notes
What’s next?

An cloud support engineer focus on troubleshooting with customer reported issue ,and cloud solution architecture.