Databases & Storage
Distributed Databases Explained: How They Work and When You Need One
By NileVPS Team · Published · 4 min read
اقرأ المقال بالعربيA distributed database stores and serves data from more than one machine while presenting itself to your application as a single logical database. That sounds simple, but spreading data across servers changes almost everything: how writes are confirmed, what happens when a network link drops, and how you back up and restore. This guide explains the core ideas in plain terms, so you can judge whether a distributed setup solves a real problem for you or just adds moving parts.
Why distribute a database at all?
A single well-tuned database server on a VPS can handle a surprising amount of traffic. Teams usually move to a distributed design for one of three reasons:
- Availability: the service must keep running when one server fails or is rebooted for maintenance.
- Scale: the data set or write volume has outgrown what one machine can hold or process.
- Geography: users in different regions need low-latency reads, or data must stay in a specific region.
If none of these applies yet, a single primary with good backups is often the better choice. Distribution is a tool, not an upgrade badge.
The building blocks
Replication
Replication keeps copies of the same data on several nodes. In single-leader replication, one node accepts writes and streams changes to followers, which can serve reads. This is how PostgreSQL streaming replication and MySQL replication work by default. In multi-leader or leaderless designs, several nodes accept writes, which improves availability but forces you to deal with conflicting updates.
Replication can be synchronous (the write is confirmed only after a replica has it) or asynchronous (the leader confirms immediately and replicas catch up). Synchronous is safer; asynchronous is faster but can lose the last few transactions if the leader dies. Read more in our guide to data replication.
Sharding
Sharding (partitioning) splits the data so each node holds only part of it, for example customers A-M on one shard and N-Z on another, or rows grouped by a hash of the customer ID. Sharding is what lets write capacity grow with the number of nodes. The cost is that queries touching many shards become slower and more complex, and choosing a bad shard key is hard to undo.
Consensus
When several nodes must agree on the order of writes or on who the leader is, they use a consensus algorithm such as Raft or Paxos. A cluster of N nodes can usually tolerate the failure of a minority: three nodes survive one failure, five survive two. That is why distributed databases are deployed in odd numbers.
CAP and the trade-off you cannot avoid
The CAP theorem states that during a network partition, a system must choose between consistency (every read sees the latest write) and availability (every request gets a response). Outside of partitions, the practical trade-off is between consistency and latency. Systems make different choices:
| Design style | Typical behavior during a partition | Examples |
|---|---|---|
| Consensus-based SQL (CP) | Minority side stops accepting writes | CockroachDB, YugabyteDB, TiDB |
| Leaderless / tunable (AP) | Both sides keep serving, reconcile later | Apache Cassandra, ScyllaDB |
| Primary plus replicas | Failover needed; async replicas may lag | PostgreSQL, MySQL, MariaDB |
None of these is "best". Banking ledgers lean toward consistency; activity feeds and metrics can often accept eventual consistency.
Practical checklist before going distributed
- Measure first. Check slow queries, indexes and memory usage. Many "we need to scale" problems are missing indexes; see database query optimization.
- Define your failure targets. How much data can you lose (RPO) and how long can you be down (RTO)?
- Plan network latency. Synchronous replication between distant regions adds round-trip time to every write. Keep consensus nodes close together unless you truly need cross-region writes.
- Use private networking between nodes where possible, and firewall database ports so only cluster members and app servers can reach them.
- Automate failover carefully. Tools such as Patroni for PostgreSQL manage leader election, but test failover regularly rather than trusting it on paper.
- Keep real backups. Replication copies mistakes too. A dropped table disappears from every replica within seconds.
A minimal example: three-node cluster on VPS servers
A common starting point is three VPS instances in the same location, each running the database plus a consensus component. For PostgreSQL that might mean Patroni with etcd on each node and HAProxy in front of them. Before going live, check the cluster state:
patronictl -c /etc/patroni/patroni.yml listThen simulate a failure by stopping the leader and confirming that a replica is promoted and applications reconnect:
sudo systemctl stop patroniDeploy identical nodes from the same image and take snapshots before major upgrades, which gives you a quick rollback point for the operating system and configuration.
Monitoring what matters
Watch replication lag, the number of healthy nodes, leader changes, disk usage per node and query latency percentiles. A cluster that silently runs with one node down is one failure away from an outage, so alert on degraded states, not only on total failure.
FAQ
Is a distributed database faster than a single server?
Not automatically. Reads can scale out across replicas, but writes often become slower because nodes must coordinate. For small and medium workloads, a single tuned server usually gives lower latency.
How many nodes do I need for high availability?
For consensus-based systems, three nodes is the practical minimum because it tolerates one failure while keeping a majority. Five nodes tolerate two failures at the cost of more coordination.
Do replicas replace backups?
No. Replicas protect against hardware failure, but they faithfully copy deletions, bad migrations and corruption caused by application bugs. Keep independent, tested backups stored away from the cluster.