

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 管理 Valkey 和 Redis OSS 的預留記憶體
<a name="redis-memory-management"></a>

預留記憶體是為非資料使用預留的記憶體。執行備份或容錯移轉時，Valkey 和 Redis OSS 會使用可用的記憶體，在叢集的資料寫入 .rdb 檔案時記錄對叢集的寫入操作。如果您沒有足夠的記憶體可供所有寫入使用，此程序會失敗。您可以在下面找到管理 ElastiCache for Redis OSS 預留記憶體的選項，以及如何套用這些選項的相關資訊。

**Topics**
+ [您需要多少預留記憶體？](#redis-memory-management-need)
+ [用於管理預留記憶體的參數](#redis-memory-management-parameters)
+ [指定您的預留記憶體管理參數](#redis-reserved-memory-management-change)

## 您需要多少預留記憶體？
<a name="redis-memory-management-need"></a>

如果您在 2.8.22 之前執行 Redis OSS 版本，則相較於執行 Redis OSS 2.8.22 或更新版本，為備份和容錯移轉預留更多記憶體。此要求是由於 ElastiCache for Redis OSS 實作備份程序的不同方式所致。經驗法則為 2.8.22 版之前的 Redis OSS 額外負荷預留節點類型`maxmemory`值的一半，為 Redis OSS 2.8.22 版及更新版本預留四分之一。

由於 ElastiCache 實作備份和複寫程序的方式不同，經驗法則是使用 `reserved-memory-percent` 參數保留節點類型`maxmemory`值的 25%。這是預設值，建議大多數情況下使用。

當爆量微型和小型執行個體類型操作接近`maxmemory`限制時，它們可能會遇到交換用量。為了在備份、複寫和高流量期間改善這些執行個體類型的操作可靠性，我們建議將小型執行個體類型的`reserved-memory-percent`參數值增加到 30%，微型執行個體類型增加到 50%。

對於具有資料分層的 ElastiCache 叢集上的大量寫入工作負載，我們建議將 增加`reserved-memory-percent`到節點可用記憶體的 50%。

如需詳細資訊，請參閱下列內容：
+ [確保您有足夠的記憶體來建立 Valkey 或 Redis OSS 快照](BestPractices.BGSAVE.md)
+ [同步與備份的實作方式](Replication.Redis.Versions.md)
+ [ElastiCache 中的資料分層](data-tiering.md)

## 用於管理預留記憶體的參數
<a name="redis-memory-management-parameters"></a>

自 2017 年 3 月 16 日起，Amazon ElastiCache 提供兩個互斥參數來管理您的 Valkey 或 Redis OSS 記憶體，`reserved-memory`以及 `reserved-memory-percent`。這些參數都不是 Valkey 或 Redis OSS 分佈的一部分。

根據您成為 ElastiCache 客戶的時間，這兩個參數的其中之一會是預設記憶體管理參數。當您建立新的 Valkey 或 Redis OSS 叢集或複寫群組並使用預設參數群組時，會套用此參數。
+ 對於在 2017 年 3 月 16 日之前啟動的客戶 - 當您使用預設參數群組建立 Redis OSS 叢集或複寫群組時，您的記憶體管理參數為 `reserved-memory`。這個情況下會保留零 (0) 位元組的記憶體。
+ 對於在 2017 年 3 月 16 日或之後啟動的客戶 – 當您使用預設參數群組建立 Valkey 或 Redis OSS 叢集或複寫群組時，您的記憶體管理參數為 `reserved-memory-percent`。在這種情況下，會保留節點 `maxmemory` 值的 25% 用於非資料目的。

讀取兩個 Valkey 或 Redis OSS 記憶體管理參數後，您可能偏好使用非預設或非預設值的參數。若是如此，您可以變更為另一個預留記憶體管理參數。

若要變更該參數的值，您可以建立自訂參數群組並進行修改，以使用您偏好的記憶體管理參數和值。然後，您可以在建立新的 Valkey 或 Redis OSS 叢集或複寫群組時，使用自訂參數群組。若是現有的叢集或複寫群組，您可以進行修改來使用您的自訂參數群組。

 如需詳細資訊，請參閱下列內容：
+ [指定您的預留記憶體管理參數](#redis-reserved-memory-management-change)
+ [建立 ElastiCache 參數群組](ParameterGroups.Creating.md)
+ [修改 ElastiCache 參數群組](ParameterGroups.Modifying.md)
+ [修改 ElastiCache 叢集](Clusters.Modify.md)
+ [修改複寫群組](Replication.Modify.md)

### reserved-memory 參數
<a name="redis-memory-management-parameters-reserved-memory"></a>

在 2017 年 3 月 16 日之前，所有 ElastiCache for Redis OSS 預留記憶體管理都是使用 參數 完成`reserved-memory`。`reserved-memory` 的預設值為 0。此預設值不會保留 Valkey 或 Redis OSS 額外負荷的記憶體，並允許 Valkey 或 Redis OSS 使用資料消耗節點的所有記憶體。

您必須建立自訂參數群組，才能變更 `reserved-memory`，以便有足夠的記憶體可供備份和容錯移轉使用。在此自訂參數群組中，您可以針對叢集和叢集節點類型上執行的 Valkey 或 Redis OSS 版本`reserved-memory`，將 設定為適合的值。如需詳細資訊，請參閱[您需要多少預留記憶體？](#redis-memory-management-need)

參數`reserved-memory`專屬於 ElastiCache，不屬於一般 Redis OSS 分佈。

下列程序說明如何使用 `reserved-memory`來管理 Valkey 或 Redis OSS 叢集上的記憶體。

**使用 reserved-memory 來保留記憶體**

1. 建立自訂參數群組，指定與您所執行引擎版本相符的參數群組系列，例如指定`redis2.8` 參數群組系列。如需詳細資訊，請參閱[建立 ElastiCache 參數群組](ParameterGroups.Creating.md)。

   ```
   aws elasticache create-cache-parameter-group \
      --cache-parameter-group-name redis6x-m3xl \
      --description "Redis OSS 2.8.x for m3.xlarge node type" \
      --cache-parameter-group-family redis6.x
   ```

1. 計算要為 Valkey 或 Redis OSS 額外負荷預留多少位元組的記憶體。您可以在 [Redis OSS 節點類型特定參數](ParameterGroups.Engine.md#ParameterGroups.Redis.NodeSpecific)中，找到您節點類型的 `maxmemory` 值。

1. 修改自訂參數群組，將參數 `reserved-memory` 設為您在上一個步驟中計算得到的位元組數。下列 AWS CLI 範例假設您正在 2.8.22 之前執行 Redis OSS 版本，且需要保留節點的一半 `maxmemory`。如需詳細資訊，請參閱[修改 ElastiCache 參數群組](ParameterGroups.Modifying.md)。

   ```
   aws elasticache modify-cache-parameter-group \
      --cache-parameter-group-name redis28-m3xl \
      --parameter-name-values "ParameterName=reserved-memory, ParameterValue=7130316800"
   ```

   您所使用的每個節點類型都需要個別的自訂參數群組，因為每個節點類型都有不同的 `maxmemory` 值。因此，每個節點類型都需要不同的 `reserved-memory` 值。

1. 修改 Redis OSS 叢集或複寫群組以使用您的自訂參數群組。

   下列 CLI 範例會修改叢集 ` my-redis-cluster`，以立即開始使用自訂參數群組 `redis28-m3xl`。如需詳細資訊，請參閱[修改 ElastiCache 叢集](Clusters.Modify.md)。

   ```
   aws elasticache modify-cache-cluster \
      --cache-cluster-id my-redis-cluster \
      --cache-parameter-group-name redis28-m3xl \
      --apply-immediately
   ```

   下列 CLI 範例會修改複寫群組 `my-redis-repl-grp`，以立即開始使用自訂參數群組 `redis28-m3xl`。如需更多詳細資訊，[修改複寫群組](Replication.Modify.md)。

   ```
   aws elasticache modify-replication-group \
      --replication-group-id my-redis-repl-grp \
      --cache-parameter-group-name redis28-m3xl \
      --apply-immediately
   ```

### reserved-memory-percent 參數
<a name="redis-memory-management-parameters-reserved-memory-percent"></a>

Amazon ElastiCache 於 2017 年 3 月 16 日推出 參數，`reserved-memory-percent`並在所有版本的 ElastiCache for Redis OSS 上提供該參數。`reserved-memory-percent` 旨在簡化您所有叢集上的預留記憶體管理工作。其做法是讓您針對每個參數群組系列 (例如 `redis2.8`)，各使用一個參數群組來管理叢集的預留記憶體，而不論節點類型為何。`reserved-memory-percent` 的預設值為 25 (25%)。

參數`reserved-memory-percent`專屬於 ElastiCache，不屬於一般 Redis OSS 分佈。

如果您的叢集使用 r6gd 系列的節點類型，且您的記憶體使用量達到 75%，則會自動觸發資料分層。如需詳細資訊，請參閱[ElastiCache 中的資料分層](data-tiering.md)。

**使用 reserved-memory-percent 來保留記憶體**  
若要使用 `reserved-memory-percent` 來管理 ElastiCache for Redis OSS 叢集上的記憶體，請執行下列其中一項操作：
+ 如果您執行 Redis OSS 2.8.22 或更新版本，請將預設參數群組指派給叢集。預設值 25% 應當足夠。如果為較舊的版本，請依照以下說明的步驟變更該值。
+ 如果您在 2.8.22 之前執行 Redis OSS 版本，您可能需要預留比 `reserved-memory-percent`的預設 25% 更多的記憶體。若要完成此操作，請遵循下列程序：

**變更 reserved-memory-percent 的百分比值**

1. 建立自訂參數群組，指定與您所執行引擎版本相符的參數群組系列，例如指定`redis2.8` 參數群組系列。必須有一個自訂參數群組，因為您無法修改預設參數群組。如需詳細資訊，請參閱[建立 ElastiCache 參數群組](ParameterGroups.Creating.md)。

   ```
   aws elasticache create-cache-parameter-group \
      --cache-parameter-group-name redis28-50 \
      --description "Redis OSS 2.8.x 50% reserved" \
      --cache-parameter-group-family redis2.8
   ```

   由於 `reserved-memory-percent` 是以節點 `maxmemory` 的百分比為單位來預留記憶體，因此您不需要每個節點類型都有一個自訂參數群組。

1. 修改自訂參數群組，將 `reserved-memory-percent` 設為 50 (50%)。如需詳細資訊，請參閱[修改 ElastiCache 參數群組](ParameterGroups.Modifying.md)。

   ```
   aws elasticache modify-cache-parameter-group \
      --cache-parameter-group-name redis28-50 \
      --parameter-name-values "ParameterName=reserved-memory-percent, ParameterValue=50"
   ```

1. 針對執行早於 2.8.22 的 Redis OSS 版本的任何 Redis OSS 叢集或複寫群組，使用此自訂參數群組。

   下列 CLI 範例會修改 Redis OSS 叢集`my-redis-cluster`，以立即`redis28-50`開始使用自訂參數群組。如需詳細資訊，請參閱[修改 ElastiCache 叢集](Clusters.Modify.md)。

   ```
   aws elasticache modify-cache-cluster \
      --cache-cluster-id my-redis-cluster \
      --cache-parameter-group-name redis28-50 \
      --apply-immediately
   ```

   下列 CLI 範例會修改 Redis OSS 複寫群組`my-redis-repl-grp`，以立即`redis28-50`開始使用自訂參數群組。如需詳細資訊，請參閱[修改複寫群組](Replication.Modify.md)。

   ```
   aws elasticache modify-replication-group \
      --replication-group-id my-redis-repl-grp \
      --cache-parameter-group-name redis28-50 \
      --apply-immediately
   ```

## 指定您的預留記憶體管理參數
<a name="redis-reserved-memory-management-change"></a>

如果您在 2017 年 3 月 16 日已是 ElastiCache 客戶，預設的預留記憶體管理參數會是 `reserved-memory`，其中保留了零 (0) 位元組的記憶體。如果您在 2017 年 3 月 16 日以後成為 ElastiCache 客戶，預設的預留記憶體管理參數會是 `reserved-memory-percent`，其中保留了 25% 的節點記憶體。無論您何時建立 ElastiCache for Redis OSS 叢集或複寫群組，都是如此。不過，您可以使用 AWS CLI 或 ElastiCache API 來變更預留記憶體管理參數。

參數 `reserved-memory` 和 `reserved-memory-percent` 不會同時存在。每個參數群組一律只有一個參數，但絕不能兩者都有。您可以透過修改參數群組，來變更參數群組用於預留記憶體管理的參數。此參數群組必須是自訂參數群組，因為您無法修改預設參數群組。如需詳細資訊，請參閱[建立 ElastiCache 參數群組](ParameterGroups.Creating.md)。

**指定 reserved-memory-percent**  
若要使用 `reserved-memory-percent` 做為您的預留記憶體管理參數，請使用 `modify-cache-parameter-group` 命令修改自訂參數群組。使用 `parameter-name-values` 參數來指定 `reserved-memory-percent` 和其值。

下列 CLI 範例會修改自訂參數群組 `redis32-cluster-on`，以使用 `reserved-memory-percent` 來管理預留記憶體。您必須指派一個值給 `ParameterValue`，參數群組才能使用 `ParameterName` 參數進行預留記憶體管理。如需詳細資訊，請參閱[修改 ElastiCache 參數群組](ParameterGroups.Modifying.md)。

```
aws elasticache modify-cache-parameter-group \
   --cache-parameter-group-name redis32-cluster-on \
   --parameter-name-values "ParameterName=reserved-memory-percent, ParameterValue=25"
```

**指定 reserved-memory**  
若要使用 `reserved-memory` 做為您的預留記憶體管理參數，請使用 `modify-cache-parameter-group` 命令修改自訂參數群組。使用 `parameter-name-values` 參數來指定 `reserved-memory` 和其值。

下列 CLI 範例會修改自訂參數群組 `redis32-m3xl`，以使用 `reserved-memory` 來管理預留記憶體。您必須指派一個值給 `ParameterValue`，參數群組才能使用 `ParameterName` 參數進行預留記憶體管理。由於引擎版本是 2.8.22 以後的版本，因此我們會將值設為 `3565158400`，這是 `cache.m3.xlarge` 的 25% `maxmemory`。如需詳細資訊，請參閱[修改 ElastiCache 參數群組](ParameterGroups.Modifying.md)。

```
aws elasticache modify-cache-parameter-group \
   --cache-parameter-group-name redis32-m3xl \
   --parameter-name-values "ParameterName=reserved-memory, ParameterValue=3565158400"
```