Prerequisites
- Your service needs 2 or more replicas. On a single-replica service, there’s nothing to pin to.
- Available on Enterprise by default when the feature is GA.
- Supported on standard ClickHouse Cloud services. BYOC isn’t supported yet.
Configuring replica-aware routing
Open a support ticket and ask to enable HTTP-based sticky replica routing. Include your service ID and why you need it (temporary tables, session state, cache reuse, or read-after-write consistency). Before migrating an existing service, ask Support to confirm that header-based routing is enabled for it. Continue usingsession_id until you receive confirmation; X-ClickHouse-Replica-Tag won’t provide sticky routing until the rollout reaches your service. No restart is required.
HTTP-based routing
- X-ClickHouse-Replica-Tag (preferred)
- session_id (legacy)
To pin a workload to a replica, send an For clickhouse-go (v2), set
X-ClickHouse-Replica-Tag header on the HTTPS interface. The proxy uses consistent hashing on the header value, so requests sharing it go to the same replica while the number of replicas remains unchanged. A different value hashes independently and may land on the same or a different replica, but you don’t choose which replica a value maps to.Use your existing service hostname. No special sticky hostnames or DNS changes are required. The header value can be any string you choose, such as an application name, user ID, or workload label. Requests without the header keep normal load balancing.Set the X-ClickHouse-Replica-Tag header on each request:Protocol: clickhouse.HTTP and pass the header with the HttpHeaders connection option.X-ClickHouse-Replica-Tag provides replica affinity without creating a ClickHouse HTTP session. Concurrent requests can reuse the same tag without encountering SESSION_IS_LOCKED.Read-after-write consistency
On a multi-replica service, a write on one replica may not be visible on the others until replication catches up. Send your write with anX-ClickHouse-Replica-Tag header, then reuse the same header value on follow-up reads. The proxy routes both to the same replica, so you read your own write even while other replicas are still behind. This pattern works for workloads that write and then immediately read back the same data, such as interactive applications or ETL jobs that validate inserts before moving on.For broader guarantees across all replicas, you can also set select_sequential_consistency to 1 on ClickHouse Cloud.Check which replica you hit
Run theSELECT hostName() example again with the same X-ClickHouse-Replica-Tag value. You should get the same hostname while the number of replicas remains unchanged. A different header value may map to a different replica.Legacy subdomain-based routing
Subdomain-based routing is no longer enabled on new services. If you already use sticky subdomains, contact Support to migrate to the HTTP header method.How legacy subdomain-based routing works
How legacy subdomain-based routing works
Previously, enabling replica-aware routing allowed a wildcard subdomain on top of the service hostname. For a service with the host name
abcxyz123.us-west-2.aws.clickhouse.cloud, any hostname matching *.sticky.abcxyz123.us-west-2.aws.clickhouse.cloud (e.g. aaa.sticky.abcxyz123.us-west-2.aws.clickhouse.cloud) was hashed by Envoy to a consistent replica. The original hostname continued to use LEAST_CONNECTION load balancing, the default routing algorithm.Limitations of replica-aware routing
Stickiness changes when the replica count changes
Scaling out or in changes the routing hash ring. Requests sharing the same routing value may then land on a different replica. If you rely on temporary tables or session-level settings, be ready to recreate them after a remap.Replica-aware routing isn’t workload isolation
Sticky routing only controls which replica handles a request. That replica may still serve other traffic. For dedicated compute, use compute-compute separation.Private Link and the legacy subdomain method
HTTP-based routing works with private networking on your normal service hostname. No extra DNS entries are required. The legacy subdomain method doesn’t: you must add DNS for the*.sticky.* hostname pattern, and incorrect setup can imbalance load across replicas.
Replica-aware routing requires the HTTP protocol
Sticky routing is keyed on an HTTP header or query parameter, depending on the routing method available for your service. The native binary protocol doesn’t carry either value for the HTTP proxy to hash on, so replica-aware routing isn’t available over the native protocol. Native-protocol clients must move the relevant workload to the HTTP interface to use this feature.Troubleshooting
Queries still land on different replicas with the same routing value- Confirm that you’re using the routing method available for your service: the
X-ClickHouse-Replica-Tagheader or the legacysession_idURL query parameter. - Confirm that every request uses exactly the same routing value.
- Wait briefly after enablement. It can take under a minute to take effect.
- Check whether the number of replicas recently changed; remapping is expected after scaling. Use
SELECT hostName()to discover the new mapping.