Skip to main content

Database Replication

Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/replication

In short

Database replication is the continuous copying of data from one database server to others, so several servers hold the same data for reliability and scale.

What is database replication?

Replication keeps copies of the same database on more than one server. In the most common setup, one server, called the primary or leader, accepts all writes, and one or more replicas, also called followers or read replicas, receive a stream of those changes and apply them to their own copy. Older documentation calls this master-slave replication.

Replication serves three main purposes. It improves availability, because if the primary fails a replica can be promoted to take its place, a process called failover; it scales reads, because read-only queries can be spread across replicas; and it can place copies of the data closer to users in other regions. Most relational and NoSQL databases, including PostgreSQL, MySQL, MongoDB, and Cassandra, support it.

Replication can be synchronous or asynchronous. With synchronous replication, the primary waits for a replica to confirm each change before reporting success, which is safer but slower; with asynchronous replication, it doesn't wait, so replicas can lag slightly behind and a user might not see their own update if the next read goes to a lagging replica. Some systems use multi-leader or leaderless replication, where several nodes accept writes, at the cost of having to resolve conflicting updates.

It is a bit like a teacher's answer key photocopied for several assistants: any assistant can answer questions, but corrections are made on the original and then copied out again. Replication is often confused with backups and with sharding. A replica copies mistakes such as an accidental DELETE almost instantly, so it is not a backup, and unlike sharding, which splits different data across servers, replication gives each server the same data.

At a glance

The app sends every write to the primary database, which replicates the changes to two replicas; the app reads from the replicas, and the second one is slightly behind.AppPrimaryaccepts all writesReplica 1up to dateReplica 2slightly behindwritesreadsreadsreplicationasynchronous
Writes go to one primary and are copied to the replicas, which spread the reads. With asynchronous replication a replica can lag a little behind.

Key takeaways

  • Replication copies the same data to multiple database servers.
  • In primary-replica setups, the primary handles writes and replicas serve reads.
  • Failover promotes a replica if the primary goes down.
  • Asynchronous replication is faster but lets replicas lag behind the primary.
  • A replica is not a backup, because mistakes are replicated too.

Example

Sending writes to the primary and reads to a replicajavascript
// Using node-postgres (pg) with two connection pools
import pg from "pg";

const primary = new pg.Pool({ connectionString: process.env.PRIMARY_DATABASE_URL });
const replica = new pg.Pool({ connectionString: process.env.REPLICA_DATABASE_URL });

await primary.query("UPDATE users SET name = $1 WHERE id = $2", ["Ada", 42]);

// With asynchronous replication, this read may briefly return the old name
const { rows } = await replica.query("SELECT name FROM users WHERE id = $1", [42]);

Readers ask

What is the difference between replication and sharding?

Replication copies the same data to several servers, mainly for availability and read scaling. Sharding splits different parts of the data across servers to scale storage and writes, and each shard is often replicated as well.

What is replication lag?

Replication lag is the delay between a change being committed on the primary and that change appearing on a replica. It is usually milliseconds but can grow under heavy load, so reads that must see the latest data should go to the primary.

Is database replication the same as a backup?

No. Replication quickly copies every change, including accidental deletes and corrupted data, to the replicas. Backups are point-in-time snapshots that let you restore data from before a mistake, so you need both.

Often compared

See also

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings