> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-home-button.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 副本感知路由

> 如何使用副本感知路由提高缓存复用率

export const PrivatePreviewBadge = () => {
  return <div className="privatePreviewBadge">
            <div className="privatePreviewIcon">
            <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path d="M5.33301 6.66667V4.66667V4.66667C5.33301 3.194 6.52701 2 7.99967 2V2C9.47234 2 10.6663 3.194 10.6663 4.66667V4.66667V6.66667" stroke="currentColor" strokeLinecap="round" strokeLinejoin="round" />
                <path d="M8.00033 9.33337V11.3334" stroke="currentColor" strokeLinecap="round" strokeLinejoin="round" />
                <path fillRule="evenodd" clipRule="evenodd" d="M11.333 14H4.66634C3.92967 14 3.33301 13.4033 3.33301 12.6666V7.99996C3.33301 7.26329 3.92967 6.66663 4.66634 6.66663H11.333C12.0697 6.66663 12.6663 7.26329 12.6663 7.99996V12.6666C12.6663 13.4033 12.0697 14 11.333 14Z" stroke="currentColor" strokeLinecap="round" strokeLinejoin="round" />
            </svg>
        </div>
            {'Private preview in ClickHouse Cloud'}
        </div>;
};

副本感知路由 (也称为 sticky sessions、sticky routing 或 session affinity) 利用了 [Envoy 代理的 ring hash 负载均衡](https://www.envoyproxy.io/docs/envoy/latest/intro/arch_overview/upstream/load_balancing/load_balancers#ring-hash)。副本感知路由的主要目的是提高缓存复用的概率。它并不能保证隔离性。

为某个服务启用副本感知路由时，我们会在该服务的主机名上额外允许一个通配符子域名。对于主机名为 `abcxyz123.us-west-2.aws.clickhouse.cloud` 的服务，你可以使用任何匹配 `*.sticky.abcxyz123.us-west-2.aws.clickhouse.cloud` 的主机名来访问该服务：

| 示例主机名                                                                    |
| ------------------------------------------------------------------------ |
| `aaa.sticky.abcxyz123.us-west-2.aws.clickhouse.cloud`                    |
| `000.sticky.abcxyz123.us-west-2.aws.clickhouse.cloud`                    |
| `clickhouse-is-the-best.sticky.abcxyz123.us-west-2.aws.clickhouse.cloud` |

当 Envoy 收到符合此类模式的主机名时，它会根据该主机名计算路由哈希，并依据计算出的哈希在哈希环上找到对应的 ClickHouse 服务器。假设该服务没有发生正在进行中的变更 (例如服务器重启、扩容或缩容) ，Envoy 将始终选择连接到同一台 ClickHouse 服务器。

请注意，原始主机名仍会使用 `LEAST_CONNECTION` 负载均衡，这也是默认的路由算法。

<div id="limitations-of-replica-aware-routing">
  ## 副本感知路由的局限性
</div>

<div id="replica-aware-routing-does-not-guarantee-isolation">
  ### 副本感知路由 不保证隔离
</div>

服务一旦发生任何变动，例如 server pod (容器组) 重启 (原因可能包括版本升级、崩溃、垂直扩缩容等) ，或 server 扩容 / 缩容，都会扰动路由哈希环。这会导致具有相同主机名的连接落到不同的 server pod (容器组) 上。

<div id="replica-aware-routing-does-not-work-out-of-the-box-with-private-link">
  ### 副本感知路由 与 Private Link 搭配时无法直接开箱即用
</div>

客户需要手动添加一条 DNS 记录，才能让新的主机名模式正常解析。如果配置不当，可能会导致服务器负载不均衡。

<div id="configuring-replica-aware-routing">
  ## 配置 副本感知路由
</div>

如需启用 副本感知路由，请联系[我们的支持团队](https://clickhouse.com/support/program)。
