---
title: "Database clustering explained: replicas, failover and sharding · CloudSat"
description: "What database clustering really covers: replicas, synchronous commits, multi-primary, sharding and quorum, plus a PostgreSQL 18 hot standby to build."
url: https://cloudsat.io/learn/database-clustering-replicas-failover-sharding
updated: 2026-11-18T12:00:00Z
source: CloudSat (https://cloudsat.io/). Prices in USD.
---

[Databases](https://cloudsat.io/learn/topic/databases)

# Database clustering explained: replicas, failover and sharding

What database clustering really covers: replicas, synchronous commits, multi-primary, sharding and quorum, plus a PostgreSQL 18 hot standby to build.

CloudSat team November 18, 2026 6 min read

"Database clustering" is a label for several techniques that solve different problems. A read replica will not save you from a dropped table, and sharding will not make one slow query fast. Start from the failure or the limit that worries you, then pick the smallest technique that addresses it. This guide maps the options, then builds the most common one: a PostgreSQL primary with a hot standby.

## Start from the problem

| If your problem is | Reach for | The catch |
| --- | --- | --- |
| Reads overload one server | Read replicas | Asynchronous replicas lag slightly behind |
| One server failure takes the app down | A standby plus failover | You need a way to promote it and redirect clients |
| A committed write must never be lost | Synchronous replication | Every commit waits for a network round trip |
| Writes or data outgrow one machine | Sharding | The biggest change to your application |
| Writes must be accepted in several places | Multi-primary | Conflicts become your problem |

Before any of these, consider a bigger machine. More vCPU, RAM and NVMe often costs less than the engineering time a cluster takes, and on CloudSat you can [resize a server](https://cloudsat.io/docs/resize-a-server) to a larger plan from the console.

## The building blocks

### Primary and replicas

One node accepts writes and the others replay its change log. PostgreSQL streams its write-ahead log (WAL), MySQL and MariaDB ship the binary log, and MongoDB replica sets copy the oplog and elect a new primary on their own. Replication is asynchronous by default: the commit returns before replicas confirm, so if the primary crashes you can lose whatever had not arrived yet, usually a fraction of a second of writes.

### Synchronous replication

The primary waits for at least one standby to confirm each commit before telling the client it succeeded. An acknowledged transaction survives the loss of the primary, at the price of one round trip per commit. If the only synchronous standby goes down, writes stall, so plan for two standbys or accept that tradeoff knowingly. In PostgreSQL, `synchronous_standby_names = 'ANY 1 (s1, s2)'` waits for either of two named standbys.

### Multi-primary

Every node accepts writes. Galera Cluster, built into MariaDB, replicates synchronously and certifies each transaction at commit: if it conflicts with one committed elsewhere, it is rolled back and the application must retry. Multi-primary is about availability, not write throughput, because every node still applies every write.

### Sharding

Data is split across nodes by a key such as customer or tenant, so each shard holds part of the data and writes scale out. Queries and transactions that span shards get harder, and the shard key is the decision that is most painful to undo. MongoDB shards natively, PostgreSQL does it through the Citus extension (AGPL-3.0), and MySQL through Vitess.

### Quorum and fencing

Automatic failover needs an odd number of voters, usually three, so a majority can agree on who the primary is. A two-node cluster cannot tell "the other node died" from "the network between us broke", and if both sides promote themselves you get split brain: two primaries accepting different writes. Fencing is the other half of the job: making sure the old primary cannot accept writes after a failover, by stopping it or taking its address away.

## Build it: a PostgreSQL 18 primary with a hot standby

You need two Ubuntu 26.04 servers with `sudo apt install postgresql`, which installs PostgreSQL 18. The primary is `203.0.113.10` and the standby is `203.0.113.11`. Allow port 5432 between these two addresses only.

### On the primary

Create a replication role (you are prompted for its password), listen on the public address, and cap how much WAL a stalled standby can pin:

```bash
sudo -u postgres createuser --replication --pwprompt replicator
sudo -u postgres psql -c "ALTER SYSTEM SET listen_addresses = 'localhost,203.0.113.10';"
sudo -u postgres psql -c "ALTER SYSTEM SET max_slot_wal_keep_size = '10GB';"
```

Allow the standby in `/etc/postgresql/18/main/pg_hba.conf`. Physical replication uses the special `replication` database name, and `hostssl` refuses unencrypted connections:

```text
hostssl    replication    replicator    203.0.113.11/32    scram-sha-256
```

Restart with `sudo systemctl restart postgresql`. The defaults for `wal_level` (`replica`) and `max_wal_senders` (10) already fit.

### On the standby

Stop PostgreSQL, move the empty cluster aside, and clone the primary:

```bash
sudo systemctl stop postgresql
sudo -u postgres mv /var/lib/postgresql/18/main /var/lib/postgresql/18/main.empty
sudo -u postgres pg_basebackup -h 203.0.113.10 -U replicator \
  -D /var/lib/postgresql/18/main -X stream -C -S standby1 -R -P
sudo systemctl start postgresql
```

`-C -S standby1` creates a replication slot on the primary so it keeps the WAL the standby still needs. `-R` writes `standby.signal` plus the connection settings, password included, into `postgresql.auto.conf` inside the data directory, which only the `postgres` user can read. `-X stream` copies WAL during the backup, and `-P` shows progress.

### Check that it works

On the primary, `sudo -u postgres psql` and run:

```sql
SELECT client_addr, state, sync_state, replay_lag FROM pg_stat_replication;
```

On the standby:

```sql
SELECT pg_is_in_recovery();
SELECT now() - pg_last_xact_replay_timestamp() AS replay_delay;
```

The first query returns `t` on a standby. Insert a test row on the primary and read it on the standby; an `INSERT` on the standby fails with a read-only error, which is what you want.

### Rehearse a failover

Practice before you need it. Stop PostgreSQL on the primary, then promote the standby:

```bash
sudo -u postgres psql -c "SELECT pg_promote();"
```

The standby becomes a writable primary within seconds. Point the application at it: a [floating IP](https://cloudsat.io/docs/floating-ips) moved to the promoted server lets clients keep the same address. Never restart the old primary as a primary. Rebuild it as a standby of the new one, either with a fresh `pg_basebackup` or with `pg_rewind`.

## Automate failover only when you are ready

Manual promotion suits many small systems: a few minutes of downtime, and no risk of a tool deciding wrong at 3 a.m. Automation means software that watches health, holds a leader lock in a consensus store, promotes a standby and fences the old primary. For PostgreSQL, Patroni (MIT license) with etcd or Consul is a common choice, and pg\_auto\_failover is a lighter alternative. On MariaDB, Galera is usually paired with a proxy such as HAProxy or MaxScale. MongoDB replica sets handle elections themselves.

## What a cluster does not replace

Replication copies mistakes as fast as it copies good data. A `DROP TABLE` or a broken migration reaches the standby in under a second. You still need backups with point-in-time recovery (pgBackRest and WAL-G are the usual tools for PostgreSQL), stored away from the cluster and proven by restoring them. The [backup plan guide](https://cloudsat.io/learn/business-data-on-a-vps-access-and-backups) covers the off-server part.

## FAQ

### How many nodes do I need?

Two for manual failover: a primary and a standby. For automatic failover, three voters, such as a primary and two standbys, or two database nodes plus a small third machine that only runs the consensus store.

### Should the standby live in another region?

For disaster recovery, yes: an asynchronous standby in a second region survives the loss of a whole site. Keep synchronous standbys close to the primary, because every commit waits for the round trip.

### Can I send reads to the standby?

Yes. A hot standby serves read-only queries. Route only reads that tolerate slight lag, and expect long queries to be canceled when they conflict with replay; `hot_standby_feedback` and `max_standby_streaming_delay` trade that off against bloat and lag on the primary.

### Is it different for MySQL or MariaDB?

The concepts are the same and the mechanics differ: binary log replication with GTIDs for primary and replica setups, Galera for synchronous multi-primary, and a proxy in front to route clients after a failover.

**Run it on CloudSat.** Cloud VPS on AMD EPYC and NVMe from $1.98/mo in 13 regions, delivered in under 5 minutes and paid in crypto. [See plans](https://cloudsat.io/plans) or [deploy a server](https://dash.cloudsat.io/console/deploy).

## Keep reading

[All guides in Databases](https://cloudsat.io/learn/topic/databases)

- [Business data on a VPS: access, encryption and backups](https://cloudsat.io/learn/business-data-on-a-vps-access-and-backups): Self-hosting Business data on a VPS: access, encryption and backups 6 min read
- [MongoDB or MariaDB: choosing between documents and tables](https://cloudsat.io/learn/mongodb-or-mariadb-documents-vs-tables): Databases MongoDB or MariaDB: choosing between documents and tables 6 min read
- [ETL, ELT and CDC: how to choose a data integration approach](https://cloudsat.io/learn/etl-elt-cdc-data-integration-guide): Databases ETL, ELT and CDC: how to choose a data integration approach 7 min read
- [Views vs materialized views: when to store the result](https://cloudsat.io/learn/views-vs-materialized-views-explained): Databases Views vs materialized views: when to store the result 6 min read
