Database

What is Primary-replica replication?

A database replication architecture in which a primary (master) database node accepts all write operations and propagates changes asynchronously or synchronously to one or more replica (slave) nodes that serve read-only copies of the data.

Primary-Replica Replication Overview

Primary-replica replication, also known as master-slave or primary-secondary replication, is a fundamental data replication pattern used to distribute database workloads, improve availability, and enhance system resilience. In this architecture, a single primary node is designated as the authoritative source of truth and receives all write operations (INSERT, UPDATE, DELETE), while one or more replica nodes maintain synchronized copies of the data and typically handle read operations only.

How Primary-Replica Replication Works

The replication process operates through a unidirectional data flow from the primary to replica nodes. When a client application submits a write operation to the primary database, the operation is executed locally and recorded in the primary's transaction log or binary log. This log is then transmitted to connected replica nodes, which apply the same operations to their local copies of the data in the same sequence. This ensures eventual consistency across all replicas, meaning that after a period of time, all nodes will contain identical data.

The replication mechanism typically involves three key components:

  • Binlog/Transaction Log: The primary database maintains a detailed record of all write operations in binary or text format, which serves as the source of truth for replication.
  • Replication Stream: A communication channel that transmits the transaction log entries from the primary to replicas, either continuously or in batches.
  • Replication Thread: Background processes on replica nodes that read log entries and apply them sequentially to maintain consistency.

Synchronization Modes

Primary-replica systems can operate in different synchronization modes, each with distinct trade-offs between consistency and performance:

  1. Asynchronous Replication: The primary commits transactions without waiting for replicas to acknowledge receipt or application. This provides optimal performance but risks data loss if the primary fails before changes propagate. If the primary crashes, some recent transactions may not have been replicated.
  2. Synchronous Replication: The primary waits for at least one replica to acknowledge receipt and application of changes before confirming the transaction. This ensures stronger consistency but introduces latency overhead and can impact write performance.
  3. Semi-synchronous Replication: A hybrid approach where the primary waits for replicas to receive (but not necessarily apply) log entries before acknowledging transactions to the client. This balances consistency guarantees with acceptable performance.

Key Advantages and Use Cases

Primary-replica replication provides numerous benefits for production database systems. Scalability is achieved by distributing read queries across multiple replicas, allowing systems to handle higher read throughput than a single database node could support. High availability is improved because if the primary fails, one of the replicas can be promoted to become the new primary, minimizing downtime. Backup and disaster recovery are simplified because replicas serve as continuously updated backups without requiring separate backup processes. Geographic distribution allows data to be replicated across multiple data centers or regions, enabling lower-latency access for geographically dispersed users and improving disaster recovery capabilities.

Common use cases include read-heavy web applications, reporting systems that query historical data, content delivery networks requiring cached data at multiple locations, and business continuity scenarios requiring failover capabilities.

Limitations and Challenges

Replication lag is a critical concern in asynchronous systems. During high write volumes, replicas may fall behind the primary, creating windows where replica data is stale. Applications reading from replicas may observe inconsistent or outdated information. Write scaling limitations exist because all write operations must still be processed by the primary—replication does not distribute write load, only read load.

Data consistency issues can arise when failures occur. In asynchronous replication, if the primary fails, uncommitted transactions may be lost. If multiple replicas exist and network partitions occur, different replicas may diverge from the primary, creating split-brain scenarios where data consistency is compromised. Operational complexity increases with monitoring and failover automation requirements, especially in determining when a replica has sufficiently caught up to be promoted as the new primary.

Comparison with Other Replication Topologies

Primary-replica replication differs from multi-master (peer-to-peer) replication, where all nodes accept write operations and must resolve conflicts. While multi-master allows write scaling, it introduces complexity in conflict resolution. Primary-replica's simpler model eliminates write conflicts by design but sacrifices write scaling. Chain replication offers another alternative where replicas forward changes to subsequent replicas in a chain, reducing network overhead but increasing replication latency.

Implementation in Modern Databases

Most production databases implement primary-replica replication as a core feature:

  • MySQL/MariaDB: Uses binary logging for asynchronous replication with options for semi-synchronous modes via plugins.
  • PostgreSQL: Provides streaming replication with both asynchronous and synchronous options.
  • MongoDB: Implements replica sets where one primary and multiple secondaries maintain synchronized data.
  • Redis: Offers master-slave replication for data persistence and failover scenarios.
  • Cloud services: AWS RDS, Azure SQL Database, and Google Cloud SQL provide managed replication with automatic failover.

Best Practices and Considerations

Organizations should monitor replication lag continuously and alert when it exceeds acceptable thresholds. Replica promotion procedures must be carefully planned and tested to ensure data consistency when switching primary roles. Write acknowledgment settings should be chosen based on required consistency guarantees and performance requirements—stricter requirements increase latency. Network topology should be designed to minimize replication lag, preferably locating primary and replicas in low-latency network segments. Backup coordination should prevent backups during critical operations and ensure backup data accurately reflects application state at a point in time. Applications must be designed to handle eventual consistency when reading from replicas, avoiding assumptions of immediate consistency after writes.

Studying for CompTIA (Database)?

ExamWizardz turns the official objectives into a guided study plan — with practice tests, real PBQs, and a readiness score. Join the waitlist to be first in when CompTIA A+ launches.