High availability (HA) ensures your Spring Boot app always works even if one ClickHouse node fails. Let’s set up HA easily.
1. Use Replicated Tables
CREATE TABLE app_logs ON CLUSTER my_cluster (
id UUID,
ts DateTime,
message String
) ENGINE = ReplicatedMergeTree(
'/clickhouse/tables/{shard}/app_logs',
'{replica}'
)
ORDER BY ts;
ReplicatedMergeTree keeps data consistent across nodes.
2. Create a Distributed Table
CREATE TABLE app_logs_dist ON CLUSTER my_cluster AS app_logs
ENGINE = Distributed(my_cluster, default, app_logs, rand());
Apps query this distributed table instead of a single node.
3. Configure Spring Boot
spring:
datasource:
url: jdbc:clickhouse://cluster-node:8123/default
username: default
password:
Point to a cluster endpoint or load balancer.
4. Load Balancing Queries
ClickHouse JDBC supports multiple hosts:
spring:
datasource:
url: jdbc:clickhouse://node1:8123,node2:8123/default
If one node fails, the driver retries others.
5. Insert Data with JdbcTemplate
jdbcTemplate.update(
"INSERT INTO app_logs_dist (id, ts, message) VALUES (?, ?, ?)",
UUID.randomUUID(), LocalDateTime.now(), "App started"
);
Data automatically replicates across cluster nodes.
image quote pre code