#1
Replication in ClickHouse ensures high availability and fault tolerance. Let’s integrate data replication with Spring Boot applications.

1. Create a Replicated Table

CREATE TABLE orders_local (
  id UInt32,
  customer String,
  amount Float32,
  created_at DateTime
) ENGINE = ReplicatedMergeTree('/clickhouse/tables/{shard}/orders', '{replica}')
ORDER BY id;
This table automatically syncs across replicas.

2. Create a Distributed Table

CREATE TABLE orders_dist AS orders_local
ENGINE = Distributed('cluster_name', 'default', 'orders_local', rand());
Applications query the distributed table.

3. Insert Data from Spring Boot

jdbcTemplate.update(
  "INSERT INTO orders_dist (id, customer, amount, created_at) VALUES (?,?,?,?)",
  1, "Alice", 300.75, LocalDateTime.now()
);
Writes are replicated across nodes automatically.

4. Query with Fault Tolerance

List<Map<String,Object>> summary = jdbcTemplate.queryForList(
  "SELECT customer, sum(amount) AS total FROM orders_dist GROUP BY customer"
);
Queries still work even if one replica is down.

5. Expose Data API

@GetMapping("/api/orders/replicated")
public List<Map<String,Object>> replicatedOrders() {
    return jdbcTemplate.queryForList("SELECT * FROM orders_dist LIMIT 20");
}
Clients access data from replicated nodes seamlessly.
#ads

image quote pre code