Database Replication
Database replication is the process of copying and synchronizing data from a primary database to one or more secondary databases, used to improve read scalability, availability, and disaster recovery.
Definition
Database replication is the process of copying and synchronizing data from a primary database to one or more secondary databases, used to improve read scalability, availability, and disaster recovery.
Overview
In the most common configuration, a primary (or "leader") node accepts all writes and streams a log of those changes to one or more replica ("follower" or "secondary") nodes, which apply the same changes to stay in sync. Applications can then route read-heavy traffic to replicas, freeing the primary to handle writes, and can fail over to a replica if the primary becomes unavailable — a core availability pattern for production databases like PostgreSQL and MySQL. Replication can be synchronous, where the primary waits for a replica to confirm it has received a write before acknowledging it to the client (stronger consistency, higher latency), or asynchronous, where the primary acknowledges writes immediately and replicas catch up shortly after (lower latency, but a window where a replica can be slightly behind — "replication lag"). Some systems support multi-primary (multi-master) replication, allowing writes to be accepted on more than one node, at the cost of needing conflict-resolution logic when the same data is modified concurrently on different nodes. Replication is closely related to, but distinct from, sharding: replication duplicates the same data for redundancy and read scaling, while sharding partitions different data across nodes for write and storage scaling; large systems frequently combine both, sharding data across clusters and then replicating each shard for durability. Replication topology and lag behavior are directly shaped by the CAP theorem trade-off a system has chosen — synchronous replication tends toward stronger consistency, while asynchronous replication favors availability and lower write latency. Managed cloud databases like Amazon Redshift and Azure SQL Database automate much of this replication topology, but understanding the underlying trade-offs remains essential for anyone designing a production data tier, a topic covered in PostgreSQL Mastery.
Key Concepts
- Primary node accepts writes; replica nodes apply a synced stream of changes
- Synchronous replication trades latency for stronger write consistency
- Asynchronous replication favors lower latency at the cost of replication lag
- Multi-primary (multi-master) replication requires conflict resolution logic
- Enables read scaling by routing read traffic to replica nodes
- Supports failover and disaster recovery when the primary becomes unavailable
- Complements, but is distinct from, sharding for horizontal scale
Use Cases
Frequently Asked Questions
From the Blog
Project: Build a Full-Stack To-Do App with React, Node.js and MongoDB
A full-stack to-do app is the perfect first MERN project — it covers every concept you'll use in production: REST APIs, database CRUD operations, JWT authentication, and deploying a frontend and backend separately. Build it once, understand the full stack.
Read More Projects & Case StudiesProject: Build a REST API with Python and FastAPI
FastAPI is the fastest-growing Python web framework — and for good reason. In this hands-on project you'll build a fully functional REST API with auto-generated documentation, database persistence, and deployment on Render, all in a single afternoon.
Read More ProgrammingAsync Python: asyncio Explained for Beginners
Async Python lets a single thread handle hundreds of concurrent I/O operations — making it essential for web APIs, database calls, and AI integrations. This guide explains coroutines, the event loop, await, gather, and real patterns you'll use in FastAPI, httpx, and LLM streaming.
Read More