<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Elasticache on Tarragon</title><link>https://tarrragon.github.io/blog/tags/elasticache/</link><description>Recent content in Elasticache on Tarragon</description><generator>Hugo -- gohugo.io</generator><language>zh-TW</language><copyright>Tarragon (CC BY 4.0)</copyright><lastBuildDate>Mon, 22 Jun 2026 00:00:00 +0000</lastBuildDate><atom:link href="https://tarrragon.github.io/blog/tags/elasticache/index.xml" rel="self" type="application/rss+xml"/><item><title>ElastiCache → 自管 Redis / Valkey：脫離 managed 的遷移路徑</title><link>https://tarrragon.github.io/blog/backend/02-cache-redis/vendors/aws-elasticache/migrate-to-self-managed/</link><pubDate>Mon, 22 Jun 2026 00:00:00 +0000</pubDate><guid>https://tarrragon.github.io/blog/backend/02-cache-redis/vendors/aws-elasticache/migrate-to-self-managed/</guid><description>&lt;blockquote>
&lt;p>本文是跨 vendor migration playbook、cross-link 到 &lt;a href="https://tarrragon.github.io/blog/backend/02-cache-redis/vendors/aws-elasticache/" data-link-title="AWS ElastiCache" data-link-desc="AWS managed Redis / Valkey / Memcached">AWS ElastiCache&lt;/a>（source）跟 &lt;a href="https://tarrragon.github.io/blog/backend/02-cache-redis/vendors/redis/" data-link-title="Redis" data-link-desc="OSS in-memory data structure store、cache 主流">Redis&lt;/a> / &lt;a href="https://tarrragon.github.io/blog/backend/02-cache-redis/vendors/valkey/" data-link-title="Valkey" data-link-desc="Redis fork、Linux Foundation 託管、BSD 授權">Valkey&lt;/a>（target）。跑 6 維 diff dimension audit 後判定為 &lt;strong>Type C operational redesign hybrid&lt;/strong>：engine 層相容（Low）但 operational model 差異大（IAM auth → password/ACL、CloudWatch → 自管監控、auto failover → Sentinel/自建 HA）。&lt;/p>&lt;/blockquote>
&lt;h2 id="為什麼從-managed-遷出">為什麼從 managed 遷出&lt;/h2>
&lt;p>ElastiCache 遷出的 driver 通常不是 engine 層問題 — 它跑的就是 Redis 或 Valkey。常見遷出原因：&lt;/p>
&lt;ul>
&lt;li>&lt;strong>成本&lt;/strong>：managed premium 在大規模（數百 GB、多叢集）下比自管 + 運維人力更貴，尤其跨帳戶大量叢集時&lt;/li>
&lt;li>&lt;strong>跨雲或混合雲&lt;/strong>：業務需要在 GCP、Azure 或 on-prem 同時運行 cache 層，ElastiCache 只在 AWS&lt;/li>
&lt;li>&lt;strong>功能限制&lt;/strong>：ElastiCache 不支援所有 Redis module（RediSearch、RedisJSON 等），或 Valkey 8.x 新功能 ElastiCache 尚未上線&lt;/li>
&lt;li>&lt;strong>控制權&lt;/strong>：自管可以自訂 redis.conf、自選 kernel 參數、自決 upgrade 時機&lt;/li>
&lt;/ul>
&lt;p>資料搬遷用 RDB export + import 就完成，真正的工程量在 operational model 重建 — ElastiCache 幫你管的 HA、monitoring、backup、security，遷出後全要自建。&lt;/p>
&lt;h2 id="6-維-diff-dimension-audit">6 維 diff dimension audit&lt;/h2>
&lt;table>
 &lt;thead>
 &lt;tr>
 &lt;th>維度&lt;/th>
 &lt;th>評估&lt;/th>
 &lt;th>等級&lt;/th>
 &lt;/tr>
 &lt;/thead>
 &lt;tbody>
 &lt;tr>
 &lt;td>Schema / API&lt;/td>
 &lt;td>同 Redis/Valkey engine、RESP 相容&lt;/td>
 &lt;td>Low&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>Operational model&lt;/td>
 &lt;td>IAM auth → ACL/password、CloudWatch → 自管監控、auto failover → Sentinel 或手動&lt;/td>
 &lt;td>High&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>Abstraction / paradigm&lt;/td>
 &lt;td>相同（key-value cache）&lt;/td>
 &lt;td>Low&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>Number of components&lt;/td>
 &lt;td>ElastiCache 1 → Redis/Valkey + Sentinel/HA + 監控 + backup 多元件&lt;/td>
 &lt;td>Medium&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>Application change&lt;/td>
 &lt;td>endpoint 換、認證方式換、少量 client config 修改&lt;/td>
 &lt;td>Low-Medium&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>Data topology&lt;/td>
 &lt;td>RDB 相容、cluster mode 對應 Redis Cluster&lt;/td>
 &lt;td>Low&lt;/td>
 &lt;/tr>
 &lt;/tbody>
&lt;/table>
&lt;p>Operational model 是 High — 這是 Type C 的判定依據。遷移重心在重建 ElastiCache 幫你做的那些事。&lt;/p>
&lt;h2 id="階段一盤點-elasticache-依賴">階段一：盤點 ElastiCache 依賴&lt;/h2>
&lt;p>在動手之前，先列出 ElastiCache 幫你管的所有東西，每一項都要在自管環境重建或決定不要。&lt;/p>
&lt;h3 id="認證與網路">認證與網路&lt;/h3>
&lt;ul>
&lt;li>&lt;strong>IAM auth&lt;/strong>：ElastiCache 支援 IAM auth token（短效 token），自管 Redis 改用 &lt;code>requirepass&lt;/code> 或 Redis 6+ ACL&lt;/li>
&lt;li>&lt;strong>VPC / Security Group&lt;/strong>：自管 Redis 仍需 VPC 隔離，但 security group 規則要自己維護&lt;/li>
&lt;li>&lt;strong>TLS&lt;/strong>：ElastiCache 原生 in-transit encryption，自管要自己配 redis TLS 憑證&lt;/li>
&lt;/ul>
&lt;h3 id="高可用">高可用&lt;/h3>
&lt;ul>
&lt;li>&lt;strong>Auto failover&lt;/strong>：ElastiCache 自動偵測 primary failure 並 promote replica。自管用 &lt;a href="https://tarrragon.github.io/blog/backend/02-cache-redis/vendors/redis/sentinel-ha-failover/" data-link-title="Redis Sentinel 與 failover 時序：從 master 死掉到 client 重連的每一段" data-link-desc="Redis Sentinel 的 failover 不是一個瞬間動作，是 down 偵測 → quorum 確認 → 選主 → 提升 → 配置廣播 → client 重連的一條時序鏈，每一段都有自己的延遲與失敗模式。本文展開 Sentinel 的判定模型與這條時序、5 個讓 failover 卡住或丟資料的 production 踩坑，以及 Sentinel 撐不住該往 Cluster 或 managed 走的邊界">Sentinel HA failover&lt;/a> 或 Redis Cluster 內建 failover&lt;/li>
&lt;li>&lt;strong>Cross-AZ replication&lt;/strong>：ElastiCache 自動跨 AZ。自管要自己在不同 AZ 部署 replica&lt;/li>
&lt;/ul>
&lt;h3 id="監控與備份">監控與備份&lt;/h3>
&lt;ul>
&lt;li>&lt;strong>CloudWatch metrics&lt;/strong>：ElastiCache 自動發 &lt;code>CurrConnections&lt;/code>、&lt;code>CacheHitRate&lt;/code>、&lt;code>ReplicationLag&lt;/code> 等。自管用 &lt;code>INFO&lt;/code> 指令 + Prometheus redis_exporter&lt;/li>
&lt;li>&lt;strong>Snapshot&lt;/strong>：ElastiCache 自動 daily snapshot + 手動 snapshot。自管用 &lt;code>BGSAVE&lt;/code> + cron + 外部 storage&lt;/li>
&lt;/ul>
&lt;h3 id="跨-region-replication">跨 region replication&lt;/h3>
&lt;ul>
&lt;li>&lt;strong>Global Datastore&lt;/strong>：ElastiCache 支援跨 region active-passive replication。自管 Redis 沒有原生跨 region replication — 若目前使用 Global Datastore，遷出前需要決定是用 application-level replication、第三方工具（Redis Enterprise Active-Active）還是放棄跨 region cache 同步&lt;/li>
&lt;/ul>
&lt;h3 id="升級與維護">升級與維護&lt;/h3>
&lt;ul>
&lt;li>&lt;strong>Engine 升級&lt;/strong>：ElastiCache 在維護窗口自動或手動升級。自管要自己做 rolling upgrade&lt;/li>
&lt;li>&lt;strong>Patch&lt;/strong>：安全 patch 由 AWS 負責。自管要自己追蹤 CVE&lt;/li>
&lt;/ul>
&lt;h2 id="階段二建立自管環境">階段二：建立自管環境&lt;/h2>
&lt;h3 id="部署架構">部署架構&lt;/h3>
&lt;p>最小 production 架構：1 primary + 1 replica + 3 Sentinel（或 Redis Cluster 3 primary + 3 replica）。&lt;/p></description><content:encoded><![CDATA[<blockquote>
<p>本文是跨 vendor migration playbook、cross-link 到 <a href="/blog/backend/02-cache-redis/vendors/aws-elasticache/" data-link-title="AWS ElastiCache" data-link-desc="AWS managed Redis / Valkey / Memcached">AWS ElastiCache</a>（source）跟 <a href="/blog/backend/02-cache-redis/vendors/redis/" data-link-title="Redis" data-link-desc="OSS in-memory data structure store、cache 主流">Redis</a> / <a href="/blog/backend/02-cache-redis/vendors/valkey/" data-link-title="Valkey" data-link-desc="Redis fork、Linux Foundation 託管、BSD 授權">Valkey</a>（target）。跑 6 維 diff dimension audit 後判定為 <strong>Type C operational redesign hybrid</strong>：engine 層相容（Low）但 operational model 差異大（IAM auth → password/ACL、CloudWatch → 自管監控、auto failover → Sentinel/自建 HA）。</p></blockquote>
<h2 id="為什麼從-managed-遷出">為什麼從 managed 遷出</h2>
<p>ElastiCache 遷出的 driver 通常不是 engine 層問題 — 它跑的就是 Redis 或 Valkey。常見遷出原因：</p>
<ul>
<li><strong>成本</strong>：managed premium 在大規模（數百 GB、多叢集）下比自管 + 運維人力更貴，尤其跨帳戶大量叢集時</li>
<li><strong>跨雲或混合雲</strong>：業務需要在 GCP、Azure 或 on-prem 同時運行 cache 層，ElastiCache 只在 AWS</li>
<li><strong>功能限制</strong>：ElastiCache 不支援所有 Redis module（RediSearch、RedisJSON 等），或 Valkey 8.x 新功能 ElastiCache 尚未上線</li>
<li><strong>控制權</strong>：自管可以自訂 redis.conf、自選 kernel 參數、自決 upgrade 時機</li>
</ul>
<p>資料搬遷用 RDB export + import 就完成，真正的工程量在 operational model 重建 — ElastiCache 幫你管的 HA、monitoring、backup、security，遷出後全要自建。</p>
<h2 id="6-維-diff-dimension-audit">6 維 diff dimension audit</h2>
<table>
  <thead>
      <tr>
          <th>維度</th>
          <th>評估</th>
          <th>等級</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>Schema / API</td>
          <td>同 Redis/Valkey engine、RESP 相容</td>
          <td>Low</td>
      </tr>
      <tr>
          <td>Operational model</td>
          <td>IAM auth → ACL/password、CloudWatch → 自管監控、auto failover → Sentinel 或手動</td>
          <td>High</td>
      </tr>
      <tr>
          <td>Abstraction / paradigm</td>
          <td>相同（key-value cache）</td>
          <td>Low</td>
      </tr>
      <tr>
          <td>Number of components</td>
          <td>ElastiCache 1 → Redis/Valkey + Sentinel/HA + 監控 + backup 多元件</td>
          <td>Medium</td>
      </tr>
      <tr>
          <td>Application change</td>
          <td>endpoint 換、認證方式換、少量 client config 修改</td>
          <td>Low-Medium</td>
      </tr>
      <tr>
          <td>Data topology</td>
          <td>RDB 相容、cluster mode 對應 Redis Cluster</td>
          <td>Low</td>
      </tr>
  </tbody>
</table>
<p>Operational model 是 High — 這是 Type C 的判定依據。遷移重心在重建 ElastiCache 幫你做的那些事。</p>
<h2 id="階段一盤點-elasticache-依賴">階段一：盤點 ElastiCache 依賴</h2>
<p>在動手之前，先列出 ElastiCache 幫你管的所有東西，每一項都要在自管環境重建或決定不要。</p>
<h3 id="認證與網路">認證與網路</h3>
<ul>
<li><strong>IAM auth</strong>：ElastiCache 支援 IAM auth token（短效 token），自管 Redis 改用 <code>requirepass</code> 或 Redis 6+ ACL</li>
<li><strong>VPC / Security Group</strong>：自管 Redis 仍需 VPC 隔離，但 security group 規則要自己維護</li>
<li><strong>TLS</strong>：ElastiCache 原生 in-transit encryption，自管要自己配 redis TLS 憑證</li>
</ul>
<h3 id="高可用">高可用</h3>
<ul>
<li><strong>Auto failover</strong>：ElastiCache 自動偵測 primary failure 並 promote replica。自管用 <a href="/blog/backend/02-cache-redis/vendors/redis/sentinel-ha-failover/" data-link-title="Redis Sentinel 與 failover 時序：從 master 死掉到 client 重連的每一段" data-link-desc="Redis Sentinel 的 failover 不是一個瞬間動作，是 down 偵測 → quorum 確認 → 選主 → 提升 → 配置廣播 → client 重連的一條時序鏈，每一段都有自己的延遲與失敗模式。本文展開 Sentinel 的判定模型與這條時序、5 個讓 failover 卡住或丟資料的 production 踩坑，以及 Sentinel 撐不住該往 Cluster 或 managed 走的邊界">Sentinel HA failover</a> 或 Redis Cluster 內建 failover</li>
<li><strong>Cross-AZ replication</strong>：ElastiCache 自動跨 AZ。自管要自己在不同 AZ 部署 replica</li>
</ul>
<h3 id="監控與備份">監控與備份</h3>
<ul>
<li><strong>CloudWatch metrics</strong>：ElastiCache 自動發 <code>CurrConnections</code>、<code>CacheHitRate</code>、<code>ReplicationLag</code> 等。自管用 <code>INFO</code> 指令 + Prometheus redis_exporter</li>
<li><strong>Snapshot</strong>：ElastiCache 自動 daily snapshot + 手動 snapshot。自管用 <code>BGSAVE</code> + cron + 外部 storage</li>
</ul>
<h3 id="跨-region-replication">跨 region replication</h3>
<ul>
<li><strong>Global Datastore</strong>：ElastiCache 支援跨 region active-passive replication。自管 Redis 沒有原生跨 region replication — 若目前使用 Global Datastore，遷出前需要決定是用 application-level replication、第三方工具（Redis Enterprise Active-Active）還是放棄跨 region cache 同步</li>
</ul>
<h3 id="升級與維護">升級與維護</h3>
<ul>
<li><strong>Engine 升級</strong>：ElastiCache 在維護窗口自動或手動升級。自管要自己做 rolling upgrade</li>
<li><strong>Patch</strong>：安全 patch 由 AWS 負責。自管要自己追蹤 CVE</li>
</ul>
<h2 id="階段二建立自管環境">階段二：建立自管環境</h2>
<h3 id="部署架構">部署架構</h3>
<p>最小 production 架構：1 primary + 1 replica + 3 Sentinel（或 Redis Cluster 3 primary + 3 replica）。</p>





<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="ln">1</span><span class="cl"><span class="c1"># Docker Compose 驗證用（production 用 VM 或 K8s）</span>
</span></span><span class="line"><span class="ln">2</span><span class="cl"><span class="c1"># Primary</span>
</span></span><span class="line"><span class="ln">3</span><span class="cl">docker run -d --name redis-primary -p 6379:6379 redis:7 <span class="se">\
</span></span></span><span class="line"><span class="ln">4</span><span class="cl"><span class="se"></span>  redis-server --requirepass <span class="s2">&#34;</span><span class="nv">$REDIS_PASSWORD</span><span class="s2">&#34;</span> --appendonly yes
</span></span><span class="line"><span class="ln">5</span><span class="cl">
</span></span><span class="line"><span class="ln">6</span><span class="cl"><span class="c1"># Replica</span>
</span></span><span class="line"><span class="ln">7</span><span class="cl">docker run -d --name redis-replica -p 6380:6379 redis:7 <span class="se">\
</span></span></span><span class="line"><span class="ln">8</span><span class="cl"><span class="se"></span>  redis-server --replicaof redis-primary <span class="m">6379</span> <span class="se">\
</span></span></span><span class="line"><span class="ln">9</span><span class="cl"><span class="se"></span>  --masterauth <span class="s2">&#34;</span><span class="nv">$REDIS_PASSWORD</span><span class="s2">&#34;</span> --requirepass <span class="s2">&#34;</span><span class="nv">$REDIS_PASSWORD</span><span class="s2">&#34;</span></span></span></code></pre></div><p>Sentinel 或 Redis Cluster 配置見 <a href="/blog/backend/02-cache-redis/vendors/redis/sentinel-ha-failover/" data-link-title="Redis Sentinel 與 failover 時序：從 master 死掉到 client 重連的每一段" data-link-desc="Redis Sentinel 的 failover 不是一個瞬間動作，是 down 偵測 → quorum 確認 → 選主 → 提升 → 配置廣播 → client 重連的一條時序鏈，每一段都有自己的延遲與失敗模式。本文展開 Sentinel 的判定模型與這條時序、5 個讓 failover 卡住或丟資料的 production 踩坑，以及 Sentinel 撐不住該往 Cluster 或 managed 走的邊界">Sentinel HA Failover</a>。</p>
<h3 id="監控重建">監控重建</h3>
<p>ElastiCache CloudWatch metrics 對應的自管替代：</p>
<table>
  <thead>
      <tr>
          <th>ElastiCache metric</th>
          <th>自管替代</th>
          <th>來源</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>CurrConnections</td>
          <td><code>connected_clients</code></td>
          <td><code>INFO clients</code></td>
      </tr>
      <tr>
          <td>CacheHitRate</td>
          <td><code>keyspace_hits / (keyspace_hits + keyspace_misses)</code></td>
          <td><code>INFO stats</code></td>
      </tr>
      <tr>
          <td>ReplicationLag</td>
          <td><code>master_repl_offset - slave_repl_offset</code></td>
          <td><code>INFO replication</code></td>
      </tr>
      <tr>
          <td>EngineCPUUtilization</td>
          <td><code>used_cpu_sys + used_cpu_user</code></td>
          <td><code>INFO cpu</code></td>
      </tr>
      <tr>
          <td>DatabaseMemoryUsagePercentage</td>
          <td><code>used_memory / maxmemory</code></td>
          <td><code>INFO memory</code></td>
      </tr>
      <tr>
          <td>Evictions</td>
          <td><code>evicted_keys</code></td>
          <td><code>INFO stats</code></td>
      </tr>
  </tbody>
</table>
<p>用 <a href="https://github.com/oliver006/redis_exporter">Prometheus redis_exporter</a> 自動採集，接 Grafana dashboard。</p>
<h3 id="backup-重建">Backup 重建</h3>





<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="ln">1</span><span class="cl"><span class="c1"># cron job: 每日 BGSAVE + 等完成 + 上傳 S3</span>
</span></span><span class="line"><span class="ln">2</span><span class="cl"><span class="c1"># LASTSAVE 回傳 Unix timestamp，BGSAVE 完成後 LASTSAVE 會更新</span>
</span></span><span class="line"><span class="ln">3</span><span class="cl"><span class="m">0</span> <span class="m">3</span> * * * <span class="nv">BEFORE</span><span class="o">=</span><span class="k">$(</span>redis-cli -a <span class="s2">&#34;</span><span class="nv">$REDIS_PASSWORD</span><span class="s2">&#34;</span> LASTSAVE<span class="k">)</span> <span class="o">&amp;&amp;</span> <span class="se">\
</span></span></span><span class="line"><span class="ln">4</span><span class="cl"><span class="se"></span>  redis-cli -a <span class="s2">&#34;</span><span class="nv">$REDIS_PASSWORD</span><span class="s2">&#34;</span> BGSAVE <span class="o">&amp;&amp;</span> <span class="se">\
</span></span></span><span class="line"><span class="ln">5</span><span class="cl"><span class="se"></span>  <span class="k">while</span> <span class="o">[</span> <span class="s2">&#34;</span><span class="k">$(</span>redis-cli -a <span class="s2">&#34;</span><span class="nv">$REDIS_PASSWORD</span><span class="s2">&#34;</span> LASTSAVE<span class="k">)</span><span class="s2">&#34;</span> <span class="o">=</span> <span class="s2">&#34;</span><span class="nv">$BEFORE</span><span class="s2">&#34;</span> <span class="o">]</span><span class="p">;</span> <span class="k">do</span> sleep 5<span class="p">;</span> <span class="k">done</span> <span class="o">&amp;&amp;</span> <span class="se">\
</span></span></span><span class="line"><span class="ln">6</span><span class="cl"><span class="se"></span>  aws s3 cp /data/dump.rdb s3://backup-bucket/redis/<span class="k">$(</span>date +<span class="se">\%</span>Y<span class="se">\%</span>m<span class="se">\%</span>d<span class="k">)</span>.rdb</span></span></code></pre></div><p>Production 建議搭配 <a href="/blog/backend/02-cache-redis/vendors/redis/persistence-fork-latency/" data-link-title="Redis 持久化與 fork latency：AOF、RDB 與那一次卡住整個 cluster 的 fork" data-link-desc="Redis 的 RDB save 與 AOF rewrite 都靠一次 fork()，而 fork 在大記憶體實例上會凍結主執行緒數百毫秒、複製分頁讓記憶體逼近翻倍。本文展開 AOF / RDB 的機制與 fsync 取捨、copy-on-write 的記憶體放大、5 個把持久化寫成延遲尖峰與資料遺失的 production 踩坑，以及 cache 場景到底要不要持久化的邊界">persistence fork latency</a> 的監控，確認 BGSAVE 的 fork 不會造成延遲 spike。</p>
<h2 id="階段三資料搬遷與切換">階段三：資料搬遷與切換</h2>
<h3 id="搬遷策略">搬遷策略</h3>
<p>ElastiCache 的資料搬遷有兩條路：</p>
<p><strong>RDB export + import（適合 downtime 可接受的場景）</strong>：</p>
<ol>
<li>ElastiCache 建立手動 snapshot</li>
<li>把 snapshot export 到 S3（ElastiCache console → Export snapshot）</li>
<li>下載 RDB 檔，放到自管 Redis 的資料目錄</li>
<li>重啟自管 Redis 載入 RDB</li>
</ol>
<p><strong>雙寫期間遷移（適合零停機需求）</strong>：</p>
<ol>
<li>Application 同時寫 ElastiCache 和自管 Redis（雙寫）</li>
<li>讀取仍走 ElastiCache</li>
<li>監控自管 Redis 的資料量與命中率追上後，切讀取到自管</li>
<li>移除 ElastiCache 寫入</li>
<li>下線 ElastiCache</li>
</ol>
<p>雙寫的複雜度高於 RDB export。Cache 資料可重建的特性讓第一種策略在多數場景夠用 — 短暫 cache miss 的代價是回源到 DB，通常可接受。</p>
<h3 id="endpoint-切換">Endpoint 切換</h3>
<p>Application 用 endpoint 連 ElastiCache。切換時：</p>
<ol>
<li>把 application config 的 Redis host 改為自管 Redis endpoint</li>
<li>確認 TLS 與認證方式對齊（IAM token → password/ACL）</li>
<li>Rolling restart application</li>
<li>監控 cache hit rate 與 latency 回到 baseline</li>
</ol>
<p>如果用 DNS CNAME 間接指向 ElastiCache endpoint，可以直接改 CNAME 指向自管 Redis，application 不用改 config。</p>
<h2 id="階段四驗證與回退">階段四：驗證與回退</h2>
<h3 id="驗證清單">驗證清單</h3>
<table>
  <thead>
      <tr>
          <th>驗證項目</th>
          <th>通過條件</th>
          <th>工具</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>連線正常</td>
          <td>application 能 PING、無 auth error</td>
          <td>redis-cli + application log</td>
      </tr>
      <tr>
          <td>資料完整</td>
          <td>key count 跟 ElastiCache 一致（容許 TTL 過期差異）</td>
          <td><code>DBSIZE</code> 比對</td>
      </tr>
      <tr>
          <td>效能 baseline</td>
          <td>latency p99 與 hit rate 跟遷移前一致</td>
          <td>Prometheus + Grafana</td>
      </tr>
      <tr>
          <td>HA 測試</td>
          <td>kill primary，Sentinel promote replica，application 自動重連</td>
          <td>手動 failover drill</td>
      </tr>
      <tr>
          <td>Backup 測試</td>
          <td>BGSAVE 產生 RDB、上傳成功、可還原</td>
          <td>還原到測試 instance 驗證</td>
      </tr>
  </tbody>
</table>
<h3 id="回退路徑">回退路徑</h3>
<p>Cache 遷移的回退比 DB 遷移簡單 — cache 資料可重建。回退步驟：</p>
<ol>
<li>Application config 改回 ElastiCache endpoint（或 CNAME 指回）</li>
<li>Rolling restart</li>
<li>Cache miss 回源到 DB，自然 warm up</li>
</ol>
<p>ElastiCache 在遷移期間不要下線，保留 7-14 天作為回退保險。確認自管 Redis 穩定運行後再刪除 ElastiCache cluster。</p>
<h2 id="成本對照">成本對照</h2>
<table>
  <thead>
      <tr>
          <th>項目</th>
          <th>ElastiCache</th>
          <th>自管 Redis</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>Compute</td>
          <td>managed node pricing（含 premium）</td>
          <td>EC2 / K8s 原價</td>
      </tr>
      <tr>
          <td>HA</td>
          <td>auto failover 內建</td>
          <td>Sentinel 或 Cluster 自建</td>
      </tr>
      <tr>
          <td>監控</td>
          <td>CloudWatch 內建</td>
          <td>redis_exporter + Prometheus 自建</td>
      </tr>
      <tr>
          <td>Backup</td>
          <td>自動 snapshot</td>
          <td>cron + S3 自建</td>
      </tr>
      <tr>
          <td>人力</td>
          <td>低（AWS 管）</td>
          <td>高（on-call + upgrade + patch）</td>
      </tr>
      <tr>
          <td>靈活度</td>
          <td>受限（engine version、module）</td>
          <td>完全自控</td>
      </tr>
  </tbody>
</table>
<p>小規模（&lt; 50 GB、&lt; 5 cluster）通常 ElastiCache 的 managed premium 比自管人力便宜。Compute 跟 HA 的差額在小規模可忽略，但監控跟 backup 的自建成本是固定開銷 — 即使只管一個 cluster，redis_exporter + Prometheus + cron backup 的設定跟維護都要做。大規模（數百 GB、多叢集）或跨雲場景下，managed premium 累積到 cluster 數 × node 數的倍數，自管的邊際成本反而更低，遷出 ROI 才成立。</p>
<h2 id="交接路由">交接路由</h2>
<ul>
<li>Source vendor overview：<a href="/blog/backend/02-cache-redis/vendors/aws-elasticache/" data-link-title="AWS ElastiCache" data-link-desc="AWS managed Redis / Valkey / Memcached">AWS ElastiCache</a></li>
<li>Target vendor 操作：<a href="/blog/backend/02-cache-redis/vendors/redis/sentinel-ha-failover/" data-link-title="Redis Sentinel 與 failover 時序：從 master 死掉到 client 重連的每一段" data-link-desc="Redis Sentinel 的 failover 不是一個瞬間動作，是 down 偵測 → quorum 確認 → 選主 → 提升 → 配置廣播 → client 重連的一條時序鏈，每一段都有自己的延遲與失敗模式。本文展開 Sentinel 的判定模型與這條時序、5 個讓 failover 卡住或丟資料的 production 踩坑，以及 Sentinel 撐不住該往 Cluster 或 managed 走的邊界">Redis Sentinel HA</a>、<a href="/blog/backend/02-cache-redis/vendors/redis/cluster-resharding/" data-link-title="Redis Cluster Re-sharding：source = target，但 topology 重劃的 5 段流程" data-link-desc="Redis cluster re-sharding 是 5 type migration 漏類實證 — source / target 同 cluster、無 schema / paradigm 差、但 16384 slot 重分配是核心；本文涵蓋 4 種 re-sharding driver、slot migration 機制、redis-cli --cluster rebalance / reshard 工具、5 個 production 踩雷（cluster busy / replica lag / client cache stale / cross-slot transaction / monitor gap）">Redis Cluster Resharding</a></li>
<li>監控重建：<a href="/blog/backend/02-cache-redis/vendors/redis/memory-eviction-tuning/" data-link-title="Redis 記憶體與淘汰調校：maxmemory-policy、LFU 與碎片化的實戰判讀" data-link-desc="Redis 的記憶體是一條會在半夜爆掉的曲線：maxmemory 設多少、policy 選 LRU 還 LFU、碎片化什麼時候開始吃掉 30% RAM、OOM 時 noeviction 怎麼讓寫入全部失敗。本文展開 Redis 記憶體會計模型、eviction policy 的選型判讀、5 個把記憶體配置寫成 production 事故的踩坑，以及單機記憶體撞牆後該往 cluster 還是 DragonflyDB 走的邊界">Redis Memory Eviction Tuning</a>、<a href="/blog/backend/02-cache-redis/vendors/redis/persistence-fork-latency/" data-link-title="Redis 持久化與 fork latency：AOF、RDB 與那一次卡住整個 cluster 的 fork" data-link-desc="Redis 的 RDB save 與 AOF rewrite 都靠一次 fork()，而 fork 在大記憶體實例上會凍結主執行緒數百毫秒、複製分頁讓記憶體逼近翻倍。本文展開 AOF / RDB 的機制與 fsync 取捨、copy-on-write 的記憶體放大、5 個把持久化寫成延遲尖峰與資料遺失的 production 踩坑，以及 cache 場景到底要不要持久化的邊界">Redis Persistence Fork Latency</a></li>
<li>反向路徑：<a href="/blog/backend/02-cache-redis/vendors/redis/migrate-to-elasticache/" data-link-title="自管 Redis / Valkey → AWS ElastiCache：engine 不變、變的是誰運維" data-link-desc="自管 Redis/Valkey 遷到 ElastiCache 的特殊之處：engine 沒變（Redis 還是 Redis）、data model 沒變、API 沒變——變的只有運維責任歸屬。本文跑 6 維 diff audit 對映 Type C operational hybrid、展開 VPC/安全/cutover 的實際工作、以及『把 failover/patching 交出去、同時交出哪些控制權』的責任邊界，5 個 production 踩坑">Redis → ElastiCache</a></li>
</ul>
]]></content:encoded></item></channel></rss>