您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

如何为高级 Azure Redis 缓存配置 Redis 群集功能How to configure Redis clustering for a Premium Azure Redis Cache

Azure Redis 缓存具有不同的缓存产品(包括群集、持久性和虚拟网络支持等高级层功能),使缓存大小和功能的选择更加灵活。Azure Redis Cache has different cache offerings, which provide flexibility in the choice of cache size and features, including Premium tier features such as clustering, persistence, and virtual network support. 本文介绍如何配置高级 Azure Redis 缓存实例中的群集功能。This article describes how to configure clustering in a premium Azure Redis Cache instance.

有关其他高级缓存功能的信息,请参阅 Azure Redis 缓存高级层简介For information on other premium cache features, see Introduction to the Azure Redis Cache Premium tier.

什么是 Redis 群集?What is Redis Cluster?

Azure Redis 缓存提供的 Redis 群集与 在 Redis 中实施的一样。Azure Redis Cache offers Redis cluster as implemented in Redis. Redis 群集具有以下优势:With Redis Cluster, you get the following benefits:

  • 能够在多个节点中自动拆分数据集。The ability to automatically split your dataset among multiple nodes.
  • 能够在部分节点遇到故障或无法与群集其余部分通信的情况下继续运行。The ability to continue operations when a subset of the nodes is experiencing failures or are unable to communicate with the rest of the cluster.
  • 更大的吞吐量:增加分片数时,吞吐量呈线性增加。More throughput: Throughput increases linearly as you increase the number of shards.
  • 更大的内存大小:增加分片数时,内存大小呈线性增加。More memory size: Increases linearly as you increase the number of shards.

群集不会增加可用于群集缓存的连接数。Clustering does not increase the number of connections available for a clustered cache. 若要深入了解高级缓存的大小、吞吐量和带宽,请参阅应使用哪种类型和大小的 Redis 缓存产品/服务?For more information about size, throughput, and bandwidth with premium caches, see What Redis Cache offering and size should I use?

在 Azure 中,Redis 群集以主/副模型提供。在该模型中,每个分片都有一个带副本的主/副对,副本由 Azure Redis 缓存服务管理。In Azure, Redis cluster is offered as a primary/replica model where each shard has a primary/replica pair with replication where the replication is managed by Azure Redis Cache service.

群集功能Clustering

在创建缓存期间,在“新建 Redis 缓存”边栏选项卡上启用群集功能。Clustering is enabled on the New Redis Cache blade during cache creation.

若要创建高级缓存,请登录到 Azure 门户,并单击“创建资源” > “数据库” > “Redis 缓存”。To create a premium cache, sign in to the Azure portal and click Create a resource > Databases > Redis Cache.

创建缓存

备注

除了在 Azure 门户中创建缓存以外,也可以使用 Resource Manager 模板、PowerShell 或 Azure CLI 创建。In addition to creating caches in the Azure portal, you can also create them using Resource Manager templates, PowerShell, or Azure CLI. 有关创建 Azure Redis 缓存的详细信息,请参阅创建缓存For more information about creating an Azure Redis Cache, see Create a cache.

若要配置高级功能,请先在“定价层”下拉列表中选择一个高级定价层。To configure premium features, first select one of the premium pricing tiers in the Pricing tier drop-down list. 若要详细了解每个定价层,请单击“查看全部定价详细信息”,并从“选择你的定价层”边栏选项卡中选择一个定价层。For more information about each pricing tier, click View full pricing details and select a pricing tier from the Choose your pricing tier blade.

选择定价层

在“Redis 群集”边栏选项卡上配置群集功能。Clustering is configured on the Redis Cluster blade.

群集功能

群集中最多可以有 10 个分片。You can have up to 10 shards in the cluster. 单击“启用”,滑动滑块或者针对“分片计数”键入一个 1 到 10 之间的数字,并单击“确定”。Click Enabled and slide the slider or type a number between 1 and 10 for Shard count and click OK.

每个分片都是一个由 Azure 管理的主/副缓存对,而缓存的总大小则通过将定价层中选择的缓存大小乘以分片数来计算。Each shard is a primary/replica cache pair managed by Azure, and the total size of the cache is calculated by multiplying the number of shards by the cache size selected in the pricing tier.

群集功能

创建缓存后,即可连接到该缓存,并像非群集缓存一样使用,Redis 会将数据分发到整个缓存分片中。Once the cache is created you connect to it and use it just like a non-clustered cache, and Redis distributes the data throughout the Cache shards. 如果诊断已启用,则会为每个分片单独捕获度量值,这些度量值可在 Redis 缓存边栏选项卡中查看If diagnostics is enabled, metrics are captured separately for each shard and can be viewed in the Redis Cache blade.

备注

配置了群集时,客户端应用程序的要求会有一些细微差异。There are some minor differences required in your client application when clustering is configured. 有关详细信息,请参阅使用群集功能时,是否需要对客户端应用程序进行更改?For more information, see Do I need to make any changes to my client application to use clustering?

有关使用 StackExchange.Redis 客户端的群集功能的示例代码,请参阅 Hello World 示例的 clustering.cs 部分。For sample code on working with clustering with the StackExchange.Redis client, see the clustering.cs portion of the Hello World sample.

更改正在运行的高级缓存上的群集大小Change the cluster size on a running premium cache

若要更改正在运行并且已启用群集功能的高级缓存上的群集大小,请在“资源菜单”中单击“Redis 群集大小”。To change the cluster size on a running premium cache with clustering enabled, click Redis Cluster Size from the Resource menu.

备注

虽然 Azure Redis 缓存高级层已发行正式发布版,但 Redis 群集大小功能目前以预览版提供。While the Azure Redis Cache Premium tier has been released to General Availability, the Redis Cluster Size feature is currently in preview.

Redis 群集大小

要更改群集大小,请使用滑块,或在“分片计数”文本框中键入 1 到 10 之间的数字,并单击“确定”进行保存。To change the cluster size, use the slider or type a number between 1 and 10 in the Shard count text box and click OK to save.

增加群集大小会增加最大吞吐量和缓存大小。Increasing the cluster size increases max throughput and cache size. 增加群集大小不会增加用于客户端的最大连接数据。Increasing the cluster size doesn't increase the max. connections available to clients.

备注

缩放群集会运行 MIGRATE 命令,此命令需耗费大量资源,因此为使其影响最小,请考虑在非高峰时段运行此操作。Scaling a cluster runs the MIGRATE command, which is an expensive command, so for minimal impact, consider running this operation during non-peak hours. 在迁移过程中,服务器加载将达到峰值。During the migration process, you will see a spike in server load. 缩放群集的运行过程耗时较长,所花费的时间量取决于密钥数以及与这些密钥相关联的值的大小。Scaling a cluster is a long running process and the amount of time taken depends on the number of keys and size of the values associated with those keys.

群集功能常见问题Clustering FAQ

以下列表包含有关 Azure Redis 缓存群集功能常见问题的解答。The following list contains answers to commonly asked questions about Azure Redis Cache clustering.

使用群集功能时,是否需要对客户端应用程序进行更改?Do I need to make any changes to my client application to use clustering?

密钥在群集中是如何分布的?How are keys distributed in a cluster?

请参阅 Redis Keys distribution model(密钥分布模型)文档:密钥空间拆分成 16384 个槽。Per the Redis Keys distribution model documentation: The key space is split into 16384 slots. 每个密钥都经过哈希处理并分配到其中一个槽,这些槽分布在群集的节点中。Each key is hashed and assigned to one of these slots, which are distributed across the nodes of the cluster. 对密钥的哪部分进行哈希处理是可以配置的,这样可确保多个使用哈希标记的密钥位于同一分片。You can configure which part of the key is hashed to ensure that multiple keys are located in the same shard using hash tags.

  • 使用哈希标记的密钥 - 如果将密钥的任意部分括在 {} 中,则只会对密钥的该部分进行哈希处理,以便确定密钥的哈希槽。Keys with a hash tag - if any part of the key is enclosed in { and }, only that part of the key is hashed for the purposes of determining the hash slot of a key. 例如,以下 3 个密钥将位于同一分片中:{key}1{key}2{key}3,因为只对名称的 key 部分进行了哈希处理。For example, the following 3 keys would be located in the same shard: {key}1, {key}2, and {key}3 since only the key part of the name is hashed. 如需密钥哈希标记规范的完整列表,请参阅Keys hash tags(密钥哈希标记)。For a complete list of keys hash tag specifications, see Keys hash tags.
  • 没有哈希标记的密钥 - 使用整个密钥名称进行哈希处理。Keys without a hash tag - the entire key name is used for hashing. 从统计学意义上来说,这样会导致密钥平均分布到缓存的各个分片中。This results in a statistically even distribution across the shards of the cache.

为了优化性能和吞吐量,建议将密钥平均分布。For best performance and throughput, we recommend distributing the keys evenly. 如果使用带哈希标记的密钥,则应用程序会负责确保密钥平均分布。If you are using keys with a hash tag it is the application's responsibility to ensure the keys are distributed evenly.

有关详细信息,请参阅Keys distribution mode(密钥分布模型)、Redis Cluster data sharding(Redis 群集数据分片)和 Keys hash tags(密钥哈希标记)。For more information, see Keys distribution model, Redis Cluster data sharding, and Keys hash tags.

有关在 StackExchange.Redis 客户端中使用群集和查找同一分片中的密钥的示例代码,请参阅 Hello World 示例的 clustering.cs 部分。For sample code on working with clustering and locating keys in the same shard with the StackExchange.Redis client, see the clustering.cs portion of the Hello World sample.

我可以创建的最大缓存大小是多大?What is the largest cache size I can create?

高级缓存的最大大小为 53 GB。The largest premium cache size is 53 GB. 可以创建多达 10 个分片,因此最大大小为 530 GB。You can create up to 10 shards giving you a maximum size of 530 GB. 如果需要的大小更大,则可请求更多If you need a larger size you can request more. 有关详细信息,请参阅 Azure Redis 缓存定价For more information, see Azure Redis Cache Pricing.

是否所有 Redis 客户端都支持群集功能?Do all Redis clients support clustering?

目前,并非所有客户端都支持 Redis 群集功能。At the present time not all clients support Redis clustering. StackExchange.Redis 是不支持该功能的客户端。StackExchange.Redis is one that does support for it. 有关其他客户端的详细信息,请参阅 Redis cluster tutorial(Redis 群集教程)的 Playing with the cluster(操作群集)部分。For more information on other clients, see the Playing with the cluster section of the Redis cluster tutorial.

Redis 群集协议要求每个客户端直接以群集模式连接到每个分片。The Redis clustering protocol requires each client to connect to each shard directly in clustering mode. 尝试使用不支持群集的客户端可能会导致大量 MOVED 重定向异常Attempting to use a client that doesn't support clustering will likely result in a lot of MOVED redirection exceptions.

备注

如果使用 StackExchange.Redis 作为客户端,请确保使用最新版本的 StackExchange.Redis,即 1.0.481 或更高版本,以便群集功能能够正常使用。If you are using StackExchange.Redis as your client, ensure you are using the latest version of StackExchange.Redis 1.0.481 or later for clustering to work correctly. 如果对 move 异常有任何疑问,请参阅 move 异常了解详细信息。If you have any issues with move exceptions, see move exceptions for more information.

启用群集功能后,如何连接到我的缓存?How do I connect to my cache when clustering is enabled?

连接到缓存时,可以使用的终结点端口密钥与连接到未启用群集功能的缓存时使用的相同。You can connect to your cache using the same endpoints, ports, and keys that you use when connecting to a cache that does not have clustering enabled. Redis 在后端管理群集功能,因此不需要你通过客户端来管理它。Redis manages the clustering on the backend so you don't have to manage it from your client.

我可以直接连接到缓存的各个分片吗?Can I directly connect to the individual shards of my cache?

群集协议要求客户端进行正确的分片连接。The clustering protocol requires that the client make the correct shard connections. 因此客户端应该为你正确地执行此操作。So the client should do this correctly for you. 话虽如此,但每个分片都是由主/副缓存对组成的,该缓存对统称为缓存实例。With that said, each shard consists of a primary/replica cache pair, collectively known as a cache instance. 可以在 GitHub 上通过 Redis 存储库的不稳定分支使用 redis-cli 实用程序连接到这些缓存实例。You can connect to these cache instances using the redis-cli utility in the unstable branch of the Redis repository at GitHub. 使用 -c 开关启动后,此版本可实现基本的支持。This version implements basic support when started with the -c switch. 有关详细信息,请参阅 http://redis.ioRedis cluster tutorial(Redis 群集教程)中的操作群集For more information see Playing with the cluster on http://redis.io in the Redis cluster tutorial.

对于非 ssl,请使用以下命令。For non-ssl, use the following commands.

Redis-cli.exe –h <<cachename>> -p 13000 (to connect to instance 0)
Redis-cli.exe –h <<cachename>> -p 13001 (to connect to instance 1)
Redis-cli.exe –h <<cachename>> -p 13002 (to connect to instance 2)
...
Redis-cli.exe –h <<cachename>> -p 1300N (to connect to instance N)

对于 ssl,请将 1300N 替换为 1500NFor ssl, replace 1300N with 1500N.

我可以为以前创建的缓存配置群集功能吗?Can I configure clustering for a previously created cache?

目前,只能在创建缓存时启用群集。Currently you can only enable clustering when you create a cache. 创建缓存后,可以更改群集大小,但不能将群集添加到高级缓存,或者从高级缓存中删除群集。You can change the cluster size after the cache is created, but you can't add clustering to a premium cache or remove clustering from a premium cache after the cache is created. 已启用群集且只包含一个分片的高级缓存不同于具有相同大小且没有群集的高级缓存。A premium cache with clustering enabled and only one shard is different than a premium cache of the same size with no clustering.

我可以为基本缓存或标准缓存配置群集功能吗?Can I configure clustering for a basic or standard cache?

群集功能仅适用于高级缓存。Clustering is only available for premium caches.

能否在 Redis ASP.NET 会话状态和输出缓存提供程序中使用群集功能?Can I use clustering with the Redis ASP.NET Session State and Output Caching providers?

我在使用 StackExchange.Redis 和群集功能时出现 MOVE 异常,应该怎么办?I am getting MOVE exceptions when using StackExchange.Redis and clustering, what should I do?

如果使用的是 StackExchange.Redis 并在使用群集功能时收到 MOVE 异常,请确保使用的是 StackExchange.Redis 1.1.603 或更高版本。If you are using StackExchange.Redis and receive MOVE exceptions when using clustering, ensure that you are using StackExchange.Redis 1.1.603 or later. 有关如何配置 .NET 应用程序以使用 StackExchange.Redis 的说明,请参阅配置缓存客户端For instructions on configuring your .NET applications to use StackExchange.Redis, see Configure the cache clients.

后续步骤Next steps

了解如何使用更多的高级版缓存功能。Learn how to use more premium cache features.