Deploy ClickHouse HA
ClickHouse HA: replicated analytics with Keeper, no data loss.
keeper-1
Just deployed
/var/lib/clickhouse
keeper-2
Just deployed
/var/lib/clickhouse
keeper-3
Just deployed
/var/lib/clickhouse
clickhouse-2
Just deployed
/var/lib/clickhouse
Just deployed
clickhouse-1
Just deployed
Deploy and Host ClickHouse HA on Railway
A replicated ClickHouse cluster with 1 shard, 2 replicas, and a 3-node Keeper ensemble. Built for production analytics workloads that need fault-tolerant columnar storage.
About Hosting
ClickHouse HA deploys a replicated cluster on Railway's private network. Two ClickHouse server nodes store identical data using ReplicatedMergeTree tables, coordinated by a 3-node ClickHouse Keeper ensemble (Raft consensus). A Caddy proxy provides a single public HTTPS endpoint that fails over automatically when a replica goes down. The Keeper ensemble ensures metadata consistency and replication coordination. Each server node has its own persistent volume, and the Keeper ensemble has volumes for coordination logs and snapshots. Tables created with ON CLUSTER are automatically replicated across both nodes. ClickHouse stores analytics data in columnar format, enabling fast queries over billions of rows. The proxy routes all traffic to the healthy replica, so a single node failure does not interrupt service.
Common Use Cases
- Real-time analytics — dashboards and reports over large datasets with sub-second query times
- Event and log aggregation — high-throughput ingestion of structured events and logs
- Time-series data — metrics, IoT telemetry, and monitoring data at scale
- Data warehousing — columnar storage for OLAP queries with compression
- A/B testing analytics — fast aggregation over experiment results
- Business intelligence — ad-hoc queries over large datasets for BI tools
Dependencies for
- ClickHouse 26.8 — official
clickhouse/clickhouse-server:26.8-alpineimage - ClickHouse Keeper 26.8 — official
clickhouse/clickhouse-keeper:26.8-alpineimage - Caddy 2 —
caddy:2-alpinefor the public proxy - Railway volumes — one per service (5 volumes total: 3 Keeper + 2 Server)
- Railway private networking — IPv6 inter-service communication
Implementation Details
The cluster uses upstream's replication for fault tolerance topology: 1 shard, 2 replicas, coordinated by a 3-node Keeper ensemble. All services build from this repository using subdirectory Dockerfiles (keeper/, server/, proxy/).
Architecture:
┌──────────────┐
HTTPS ───────────► │ proxy │ Caddy — one public URL, fails over
└──────┬───────┘
│ :8123
┌─────────────┴─────────────┐
▼ ▼
┌───────────────┐ ┌───────────────┐
│ clickhouse-1 │◄─────────►│ clickhouse-2 │ 1 shard / 2 replicas
└───────┬───────┘ └───────┬───────┘
└─────────────┬─────────────┘
▼ :9181
┌──────────┐ ┌──────────┐ ┌──────────┐
│ keeper-1 │ │ keeper-2 │ │ keeper-3 │ raft quorum on :9234
└──────────┘ └──────────┘ └──────────┘
Default credentials: Username is default. Password is auto-generated via ${{secret(32)}} on first boot (stored as SHA-256 hash). You can set CLICKHOUSE_PASSWORD before deploy to control it. Password changes require redeploying with a new value.
Accessing the HTTP interface:
- Open the public domain assigned to the
proxyservice - Use ClickHouse HTTP API:
https:/// - Authenticate with username
defaultand the generated password - Run queries via HTTP:
curl -u default: https:/// -d "SELECT 1"
Connecting via native protocol (TCP):
Use the ClickHouse native protocol on port 9000. Connect directly to a replica or through a TCP proxy:
clickhouse-client --host --port 9000 --user default --password
For internal Railway services, connect directly:
clickhouse-client --host clickhouse-1.railway.internal --port 9000 --user default --password
Creating replicated tables:
Use ON CLUSTER to create tables on all replicas automatically:
CREATE TABLE events ON CLUSTER default_cluster
(
ts DateTime,
user String,
action String
)
ENGINE = ReplicatedMergeTree
ORDER BY (ts, user);
A plain MergeTree (without ON CLUSTER) lives on one replica only and is not replicated.
Connecting via code (Python example):
import clickhouse_connect
client = clickhouse_connect.get_client(
host="proxy.railway.internal",
port=8123,
username="default",
password=""
)
client.command("CREATE TABLE IF NOT EXISTS test (id UInt64, name String) ENGINE = MergeTree ORDER BY id")
client.insert("test", [[1, "hello"], [2, "world"]])
result = client.query("SELECT * FROM test")
print(result.result_rows)
Ports:
| Port | Purpose | Exposure |
|---|---|---|
| 8123 | HTTP interface (queries, REST API) | Via proxy (public) |
| 9000 | Native TCP protocol (clickhouse-client, drivers) | Private (+ TCP proxy) |
| 9009 | Inter-server communication (replication) | Private (cluster only) |
| 9181 | Keeper client port | Private (cluster only) |
| 9234 | Keeper Raft inter-server | Private (cluster only) |
| 9182 | Keeper HTTP readiness (/ready) | Private (health check) |
Volumes: One volume per service at /var/lib/clickhouse. Railway volumes are 1:1.
Comparison with Alternatives
| Feature | ClickHouse HA (this template) | Single ClickHouse | PostgreSQL | BigQuery |
|---|---|---|---|---|
| Query speed (analytics) | ✅ Sub-second on billions of rows | ✅ Sub-second | ⚠️ Slower on large scans | ✅ Fast |
| Fault tolerance | ✅ 2 replicas + Keeper quorum | ❌ Single node | ⚠️ Needs HA setup | ✅ Managed |
| Cost (self-hosted) | ⚠️ 6 services | ✅ 1 service | ✅ 1 service | ❌ Usage-based (expensive) |
| Setup complexity | ⚠️ 6 services | ✅ 1 service | ✅ 1 service | ✅ Managed |
| Columnar storage | ✅ Native | ✅ Native | ❌ Row-based | ✅ Native |
| SQL compatibility | ✅ ClickHouse SQL | ✅ ClickHouse SQL | ✅ Standard SQL | ✅ Standard SQL |
| Data compression | ✅ Excellent (LZ4, ZSTD) | ✅ Excellent | ⚠️ Moderate | ✅ Automatic |
| Horizontal scaling | ✅ Add shards/replicas | ❌ | ⚠️ Limited | ✅ Automatic |
| Self-hosted | ✅ | ✅ | ✅ | ❌ |
Security Considerations
- Password: Auto-generated via
${{secret(32)}}and stored as SHA-256 hash. Never stored in plaintext. SetCLICKHOUSE_PASSWORDbefore deploy to control it. - Default user: Uses
defaultusername. SetCLICKHOUSE_USERto rename and remove the stockdefaultaccount. - Network access: ClickHouse HTTP (8123) and native (9000) are accessible via the proxy (public) or private networking. Keeper ports (9181, 9234) and inter-server (9009) are private only.
- Access management: The configured user has
access_management=1, allowing user/role management via SQL. - Volume encryption: Railway volumes are encrypted at rest.
- No public ClickHouse ports: Only the Caddy proxy is public. ClickHouse replicas are private.
Why Deploy ClickHouse HA on Railway?
Railway is a singular platform to deploy your infrastructure stack. Railway will host your infrastructure so you don't have to deal with configuration, while allowing you to vertically and horizontally scale it.
By deploying ClickHouse HA on Railway, you are one step closer to supporting a complete full-stack application with minimal burden. Host your servers, databases, AI agents, and more on Railway.
Six services with automatic replication, Keeper quorum, and a failover proxy — no ZooKeeper to manage, no manual XML configuration. Set the password and deploy.
Template Content
keeper-1
codestorm-official/clickhouse-hakeeper-2
codestorm-official/clickhouse-hakeeper-3
codestorm-official/clickhouse-haclickhouse-2
codestorm-official/clickhouse-haclickhouse-1
codestorm-official/clickhouse-ha