【Azure Redis 快取】Azure Redis 服務不支援指令CONFIG

路邊兩盞燈發表於2020-12-16

問題描述

在Azure Redis的門戶頁面中,通過Redis Console連線到Redis後,想通過CONFIG命令來配置Redis,但是系統提示CONFIG命令不能用。 錯誤訊息為:(error) ERR unknown command `config`。

【Azure Redis 快取】Azure Redis 服務不支援指令CONFIG

 

根本原因

因為 Azure Redis 快取例項的配置和管理由 微軟進行管理,所以禁用了以下命令。 如果嘗試呼叫它們,將收到一條類似於 "(error) ERR unknown command" 的錯誤訊息。

  • BGREWRITEAOF
  • BGSAVE
  • CONFIG
  • DEBUG
  • MIGRATE
  • SAVE
  • SHUTDOWN
  • SLAVEOF
  • CLUSTER - 群集寫命令已禁用,但允許使用只讀群集命令。

更詳細的說明見官方文件說明:Azure Redis 快取中不支援 Redis 命令

 

解決方案

方式一:在門戶上配置Redis的引數。如SSL, Maxmemory-Policy, Maxmemory-reserved, Maxfragmentationmemory-reserved. 

  1. SSL:預設情況下,Azure為新快取禁用非 TLS/SSL 訪問。 要啟用非 TLS 埠,需在如下截圖頁面中修改。
  2. Maxmemory policy 快取逐出策略, 預設值為volatile-lru, 即在配置過過期時間的Key中移除最近不使用的Key以騰出空間儲存新值。其他的逐出策略見本文附錄一
  3. Maxmemory-reserved:設定用於配置群集中保留給非快取操作(例如故障轉移期間的複製)的每個例項的記憶體量(以 MB 為單位)
  4. Maxfragmentationmemory-reserved:設定用於配置群集中保留以容納記憶體碎片的每個例項的記憶體量(以 MB 為單位)

而如果需要設定其他的配置,則只能通過 方式二PowerShell命令 來設定。

【Azure Redis 快取】Azure Redis 服務不支援指令CONFIG

 

 

 

方式二:通過PowerShell命令(Set-AzRedisCache)來修改Redis的配置。Set-AzRedisCache命令中,最主要的配置包含在RedisConfiguration引數中。

Set-AzRedisCache:

PS C:\> Get-Help Set-AzRedisCache -detailed

    NAME
        Set-AzRedisCache

    SYNOPSIS
        Set Azure Cache for Redis updatable parameters.

    SYNTAX
        Set-AzRedisCache -Name <String> -ResourceGroupName <String> [-Size <String>] [-Sku <String>]
        [-MaxMemoryPolicy <String>] [-RedisConfiguration <Hashtable>] [-EnableNonSslPort <Boolean>] [-ShardCount
        <Integer>] [<CommonParameters>]

    DESCRIPTION
        The Set-AzRedisCache cmdlet sets Azure Cache for Redis parameters.

    PARAMETERS
        -Name <String>
            Name of the Azure Cache for Redis to update.

        -ResourceGroupName <String>
            Name of the resource group for the cache.

        -Size <String>
            Size of the Azure Cache for Redis. The default value is 1GB or C1. Possible values are P1, P2, P3, P4, C0, C1, C2, C3,
            C4, C5, C6, 250MB, 1GB, 2.5GB, 6GB, 13GB, 26GB, 53GB.

        -Sku <String>
            Sku of Azure Cache for Redis. The default value is Standard. Possible values are Basic, Standard and Premium.

        -MaxMemoryPolicy <String>
            The 'MaxMemoryPolicy' setting has been deprecated. Please use 'RedisConfiguration' setting to set
            MaxMemoryPolicy. e.g. -RedisConfiguration @{"maxmemory-policy" = "allkeys-lru"}

        -RedisConfiguration <Hashtable>
            All Redis Configuration Settings. Few possible keys: rdb-backup-enabled, rdb-storage-connection-string,
            rdb-backup-frequency, maxmemory-reserved, maxmemory-policy, notify-keyspace-events, hash-max-ziplist-entries,
            hash-max-ziplist-value, set-max-intset-entries, zset-max-ziplist-entries, zset-max-ziplist-value.

        -EnableNonSslPort <Boolean>
            EnableNonSslPort is used by Azure Cache for Redis. The default value is null and no change will be made to the
            currently configured value. Possible values are true and false.

        -ShardCount <Integer>
            The number of shards to create on a Premium Cluster Cache.

        <CommonParameters>
            This cmdlet supports the common parameters: Verbose, Debug,
            ErrorAction, ErrorVariable, WarningAction, WarningVariable,
            OutBuffer, PipelineVariable, and OutVariable. For more information, see
            about_CommonParameters (https://go.microsoft.com/fwlink/?LinkID=113216).

Set-AzRedisCache 示例:(示例引用來自:如何設定讓Azure Redis中的RDB檔案暫留更久(如7天))

$RedisConfiguration = @{"rdb-backup-enabled"="true"; "rdb-backup-frequency"="60"; "rdb-storage-connection-string"="$StorageConnectionString"; "rdb-backup-max-days"="7"}
Set-AzRedisCache -ResourceGroupName $ResourceGroupName -Name $CacheName -RedisConfiguration $RedisConfiguration

注:配置完成後,可以使用 Get-AzRedisCache cmdlet 檢索有關快取的資訊。

 

附錄一:Redis逐出策略 [less recently used (LRU),Least Frequently Used(LFU) ]

  • volatile-lru :通過嘗試先刪除最近不使用的(LRU)金鑰來移出金鑰,但只有在已設定過期的金鑰中才能移出這些金鑰,以便為新增的新資料騰出空間。

  • allkeys-lru:通過嘗試先刪除最近不使用的(LRU)鍵來移出鍵,以便為新增的新資料騰出空間。

  • volatile-random:為新增新的鍵騰出空間,隨機逐出其他鍵,但是僅逐出設定了過期時間的鍵。

  • allkeys-random:隨機逐出其他鍵,以便為新增新的鍵騰出空間。

  • volatile-ttl:逐出設定過期的金鑰,並嘗試首先逐出具有較短生存時間(TTL)的金鑰,以便為新增新的鍵騰出空間。

  • noeviction:當達到記憶體限制並且客戶端嘗試執行可能導致使用更多記憶體的命令時,將返回錯誤(大多數寫入命令,但DEL和一些其他例外)。

  • volatile-lfu:使用設定過期並且使用頻率最少的鍵(LFU)中進行驅逐。

  • allkeys-lfu:使用頻率最少的鍵(LFU)逐出任何金鑰。

 

參考資料

如何設定讓Azure Redis中的RDB檔案暫留更久(如7天): https://www.cnblogs.com/lulight/p/13842979.html
Azure Redis 快取中不支援 Redis 命令: https://docs.azure.cn/zh-cn/azure-cache-for-redis/cache-configure#redis-commands-not-supported-in-azure-cache-for-redis
Redis Eviction policies: https://redis.io/topics/lru-cache#eviction-policies
更新 Azure Redis 快取: https://docs.azure.cn/zh-cn/azure-cache-for-redis/cache-how-to-manage-redis-cache-powershell#to-update-an-azure-cache-for-redis

 

相關文章