{
  "id": "database-replication",
  "title": "Database Replication",
  "lang": "en",
  "source_lang": "en",
  "summary": "Database replication maintains coordinated copies of data across systems to improve availability, distribute workloads, and support recovery and data integration.",
  "category": "computer-science",
  "subcategory": "software",
  "type": "concept",
  "aliases": [
    "Data replication in databases",
    "replicated database management"
  ],
  "generated_at": "2026-11-03",
  "model": "pro/gpt-6.1-sol",
  "url": "https://aiwiki.page/en/database-replication/",
  "translations": {
    "zh-Hans": {
      "url": "https://aiwiki.page/zh/database-replication/",
      "json": "https://aiwiki.page/zh/database-replication.json"
    }
  },
  "sections": [
    {
      "heading": "",
      "text": "Database replication is the process of maintaining copies of a database, or selected parts of it, on multiple computing systems by propagating changes between them. These copies, called replicas, may reside on separate servers, in different data centers, or across geographical regions. Replication can improve service availability, distribute read traffic, reduce access latency, and provide data to other systems. Its defining problem is not simply copying information, but coordinating copies while transactions continue and machines or networks fail.\n\nReplication mechanisms differ in what they transfer, which replicas may accept writes, when a transaction is acknowledged, and what readers are guaranteed to observe. Some systems reproduce a primary server’s storage-level changes; others distribute logical operations or use a consensus protocol to maintain an agreed sequence of updates. These choices determine whether replicas can temporarily disagree, which failures the system can tolerate, and how much coordination each operation requires. Replication is therefore a central topic in distributed computing and database management system design, rather than a single interchangeable feature."
    },
    {
      "heading": "Purposes, boundaries, and historical development",
      "text": "Replication addresses several distinct requirements. A standby replica can take over when the active database server fails. Read replicas can answer queries without consuming all the primary server’s resources. Geographically distributed copies can place data closer to users or preserve it after the loss of a site. Replicated data can also feed a data warehouse, search service, or reporting system. These uses share the movement of updates, but they do not necessarily require the same freshness, ordering, or recovery guarantees.\n\nReplication is different from backup. An ordinary replica usually follows current database state, so an accidental deletion, application error, or malicious update may quickly reach every replica. A backup preserves recoverable state from an earlier time, often with independent retention and access controls. A delayed replica can retain an older state temporarily, but its usefulness depends on detecting an error before the delay expires. Replication and backup consequently solve overlapping but nonidentical problems: keeping service running and recovering historical information.\n\nReplication also differs from partitioning, caching, and federation. Partitioning divides a dataset into subsets, while replication creates additional copies of a subset or of the whole dataset. A cache may retain only selected results and may discard them without preserving a complete transactional history. A federated database presents data from multiple autonomous sources without necessarily copying them into equivalent stores. Real systems frequently combine these techniques, but their failure and consistency properties remain distinguishable.\n\nResearch on replicated databases developed alongside distributed transaction processing during the 1970s and 1980s. Leslie Lamport’s 1978 work on logical clocks clarified how events can be ordered without a universally shared physical clock. David Gifford’s 1979 work on weighted voting established influential quorum techniques for coordinating redundant data. Research subsequently connected replication to transaction serializability, failure detection, and agreement among machines. Commercial systems developed log shipping, standby databases, and publication-and-subscription mechanisms as practical ways to support recovery and distribute data.\n\nLater systems emphasized different operating environments. Amazon’s Dynamo paper, published in 2007, described a highly available key–value architecture that accepted some conflicting versions rather than making every operation wait for global coordination. Google’s Spanner paper, published in 2012, described a globally distributed transactional database combining synchronous replication with a clock-uncertainty mechanism. These systems illustrate that replication does not imply one consistency model: its design depends on the relationship among availability, transaction semantics, geographical placement, and operational cost."
    },
    {
      "heading": "Physical, logical, and operation-based replication",
      "text": "A central distinction concerns the representation of changes. Physical replication transfers information tied to a database’s storage structures, such as modified pages or low-level recovery records. Many implementations build on write-ahead logging, in which recovery information is recorded before the corresponding data pages are written. A replica replays this information to reconstruct the source database’s changes. Because the mechanism operates below the level of application queries, it can reproduce transactions without independently executing their original business logic.\n\nPhysical replication commonly requires compatible database versions, storage formats, and configuration. A page-level or recovery-record stream may represent internal structures that another database engine cannot interpret. Such replication can nevertheless be efficient and faithful: changes to tables, indexes, and internal structures are carried in the form the engine already uses for recovery. PostgreSQL streaming replication, for example, transmits write-ahead-log records to standby servers. A standby’s receipt of records and its replay of them are separate stages, so received data need not yet be visible to queries.\n\nLogical replication expresses changes in terms of database objects and values. A stream may identify a table, an operation such as insertion or deletion, a row’s key, and its changed columns. This representation can support selective replication, different physical layouts, or movement between unlike systems. However, the destination still needs compatible interpretations of data types, identifiers, constraints, and transaction boundaries. Logical replication is not automatically independent of schema changes: adding a column or changing a type can require coordinated changes in the producer and consumer.\n\nStatement-based replication sends commands for execution at the destination. It can be compact when a short command changes many rows, but it depends on deterministic behavior. A statement involving an unstable row order, an external function, or a locally evaluated random value may produce different results unless the replication mechanism controls those inputs. Row-based replication instead communicates the resulting row changes, reducing dependence on repeated statement execution while potentially generating much larger streams. MySQL supports statement-based, row-based, and mixed binary logging, illustrating the practical coexistence of these approaches.\n\nOperation-based replication can also distribute application-level commands, such as adding an element to a set or incrementing a counter. This can preserve more semantic information than copying final values, but it creates requirements about duplicate suppression and ordering. An increment applied twice is not equivalent to an increment applied once. Systems therefore associate operations with identifiers, ordering metadata, or algebraic rules that make repeated delivery safe. The representation chosen for replication determines which information is available for conflict resolution and which assumptions consumers must share."
    },
    {
      "heading": "Replication topologies and data placement",
      "text": "In a single-leader topology, one server accepts writes for a replicated dataset and sends changes to followers. Older documentation often calls these roles master and slave; primary and replica are also common terms. A single write authority simplifies ordering because followers can reproduce one history instead of reconciling independently created histories. The arrangement does not by itself specify whether replication is synchronous, whether followers serve reads, or how a new leader is chosen.\n\nFollower placement can form a star, a chain, or a cascading tree. In a star, each follower receives changes directly from the primary. In cascading replication, an intermediate replica forwards changes to downstream replicas, reducing the primary’s network fan-out. A chain can assign update propagation and read servicing to particular positions, with the exact protocol determining acknowledgment and failure behavior. These arrangements change bandwidth usage and failure dependencies: a downstream replica may lose its update source even while the original primary remains healthy.\n\nMulti-leader replication allows writes at more than one location. Each leader propagates its changes to other leaders, often to support geographically local writes or continued operation during disconnection. The topology may be a mesh, a ring, or a directed set of replication links. Because concurrent changes can originate independently, such systems need rules for detecting and resolving conflicts. Preventing cycles from endlessly circulating the same update also requires origin identifiers, sequence tracking, or equivalent mechanisms.\n\nLeaderless replication exposes another organization. A coordinator sends a request to several replicas and waits for a configured set of responses rather than routing every update through one permanent primary. Reads may consult multiple replicas, compare versions, and repair discrepancies. “Leaderless” does not mean that no coordination exists: the system still needs replica membership, request routing, version metadata, and rules governing which responses count as sufficient.\n\nReplication is often combined with sharding. A database divided into 100 shards might replicate each shard three times, rather than placing three full copies on three machines. Leadership and replication membership can then differ by shard. A transaction touching two shards may require coordination across two independently replicated groups. Moving a shard between machines also becomes a replication problem because a new copy must catch up before ownership can safely change.\n\nReplica count alone is an incomplete measure of resilience. Three replicas on one physical server share its failure risk; three servers in one building may share power and network dependencies. Placement across failure domains can protect against broader outages, but usually increases communication latency and cost. Some systems also use witnesses or voting members that participate in leadership decisions without maintaining a complete queryable dataset. Consequently, the number of voters, durable data copies, and read-serving copies may differ."
    },
    {
      "heading": "Synchronization, acknowledgments, and durability",
      "text": "Synchronous and asynchronous replication describe when the primary acknowledges an operation relative to work performed elsewhere. With asynchronous replication, the primary can acknowledge a transaction before another replica has durably received it. This reduces dependence on remote response time, but a primary failure can leave acknowledged transactions absent from the replica selected for promotion. The amount at risk depends on the backlog and on the exact failure sequence, not simply on the configured number of replicas.\n\nSynchronous replication requires some remote milestone before success is returned. That milestone must be specified carefully. A follower might acknowledge receiving bytes in memory, writing them to an operating-system buffer, flushing them to persistent storage, or replaying them into database state. These events provide different guarantees. Data durable on a follower may still be unavailable to its queries until replay catches up. Conversely, data visible in volatile memory may not survive a correlated power failure.\n\nConfigurations commonly wait for one designated standby, any one of several standbys, or a quorum. Waiting for every replica provides a different availability profile from waiting for a majority. If one required replica is unreachable, writes may stop even though the primary itself remains operational. Some systems permit an automatic fallback from synchronous to asynchronous behavior. Such a fallback changes the durability contract and cannot be understood merely as a performance optimization.\n\nReplication delay has several components: generating changes, sending them, persisting them remotely, and applying them. A replica may have almost no network backlog but a large replay backlog because its storage or processors cannot keep up. Systems therefore track multiple positions in a log, rather than treating “replication lag” as one universal measurement. A time-based lag value is also difficult to interpret during idle periods: a replica can be fully caught up even though its last replayed transaction is old.\n\nNetwork distance creates an unavoidable coordination cost. If acknowledgment requires a remote round trip, the write’s critical path includes that communication plus processing and storage work. Transactions can overlap, and group commit can amortize persistence costs across multiple operations, so latency does not directly determine maximum throughput. Nevertheless, geographically synchronous replication cannot eliminate the time required for information to travel.\n\nDelivery guarantees require similar precision. A replication transport may redeliver changes after a connection failure because the sender cannot determine whether the last message was applied. Idempotent application, transaction identifiers, and durable progress records can prevent duplicate effects. Claims of exactly-once processing are meaningful only within a defined boundary: applying a database transaction once does not automatically guarantee that an external email, payment request, or other side effect occurs once."
    },
    {
      "heading": "Consistency models and transaction guarantees",
      "text": "Replication consistency describes what combinations of values and operation orders clients may observe. Linearizability requires each operation on an object to appear to occur at one instant between invocation and completion, respecting real-time order. If one client completes a write before another begins a read, a linearizable read cannot return a value preceding that write. A stale follower generally cannot provide this guarantee merely because its update stream is reliable.\n\nEventual consistency provides a weaker condition: if updates cease and communication and repair proceed successfully, replicas eventually converge according to the system’s rules. It does not specify a universal maximum delay, nor does it guarantee that intermediate reads preserve application invariants. Two replicas can each behave correctly under an eventually consistent design while temporarily returning different values. Convergence also requires more than eventual message delivery if conflicts are resolved nondeterministically.\n\nBetween these models are several useful guarantees. Read-your-writes consistency allows a client to observe its own completed updates. Monotonic reads prevent that client from moving backward to an older observed state. Causal consistency preserves dependencies: an operation that depends on an earlier operation must not become visible before that dependency. These guarantees can sometimes be implemented with session tokens, version vectors, or replica-selection rules without requiring every request to contact every region.\n\nReplication consistency is distinct from transaction isolation. A transaction may provide the ACID properties locally while its effects reach remote replicas later. Serializability concerns whether concurrent transactions are equivalent to a serial execution. Strict serializability additionally respects real-time ordering. A replicated database can offer different guarantees for different operations—for example, strongly consistent writes alongside explicitly stale reads—so a product-wide label such as “strongly consistent” may conceal important API distinctions.\n\nAtomic visibility also matters. If a transaction updates an order row and its payment row, a consumer should not necessarily see only half the transaction. Logical replication pipelines may preserve source transaction boundaries, split them across destinations, or expose changes incrementally. A target that lacks equivalent transactional capabilities may not reproduce the source’s semantics even when it receives every row change.\n\nThe CAP theorem identifies a limitation under network partition: a distributed service cannot guarantee both linearizability and availability, in the theorem’s specific sense of answering requests at nonfailed nodes. It does not mean that every database permanently chooses any two of three independent features, nor does it explain every latency trade-off. During normal communication, systems still face coordination costs; during partitions, they must determine which operations can proceed without violating their chosen guarantees. Asynchronous replication alone does not establish that a system is always available, and consensus replication does not imply that every possible read is linearizable."
    },
    {
      "heading": "Concurrent writes and conflict resolution",
      "text": "When separate replicas accept writes without first agreeing on an order, they can create concurrent versions. Suppose two disconnected locations each update the same customer’s delivery address. Neither update necessarily includes knowledge of the other, so a simple “newer than” relationship may not exist. Conflict detection therefore requires metadata that distinguishes causal succession from concurrency, or an application rule that avoids this distinction by always choosing one winner.\n\nA common policy is last-writer-wins, using timestamps and a deterministic tie-breaker. It is simple and allows replicas to converge, but its name can be misleading: unsynchronized physical clocks may rank an earlier real-world write after a later one. Even with well-controlled clocks, selecting one value discards another concurrent intention. Timestamp choice is therefore part of data semantics, not merely an internal implementation detail.\n\nVersion vectors record progress associated with multiple writers or replica identities. If one vector incorporates all the progress in another, the corresponding version can be treated as causally later. If neither dominates, the versions may be concurrent. The metadata can grow as writers proliferate or membership changes, leading systems to use bounded or compacted variants. Logical clocks establish useful ordering relationships, but assigning every operation a sortable identifier does not by itself preserve every application dependency or establish real-time order.\n\nConflicts can occur even when writers update different rows. Two regions might independently create accounts with the same supposedly unique username or each sell the final unit of stock. Row-level merging cannot enforce an invariant spanning those operations. Strong uniqueness, referential integrity, and nonnegative inventory often require coordination, partitioned ownership, preallocated rights, or a domain-specific reconciliation process. A primary key that is globally unique prevents identifier collisions but does not prevent conflicting business decisions.\n\nConflict-free replicated data types provide mathematically defined merge behavior for particular structures. A grow-only counter can store one component per participant and merge corresponding components by taking their maximum. Its reported value is the sum of the components. State-based designs typically use merge operations that are associative, commutative, and idempotent, allowing duplicate and reordered state exchanges to converge. More expressive counters, sets, and sequences require additional metadata and carefully defined semantics.\n\nDeletion introduces another difficulty. A deleted record may need a tombstone so that an old replica does not later reintroduce it during repair. Removing tombstones too early can resurrect data; retaining them indefinitely consumes space and complicates maintenance. Systems therefore need conditions for safe garbage collection, such as evidence that relevant replicas have advanced beyond the deletion. Long-offline devices and changing replica membership make those conditions harder to establish."
    },
    {
      "heading": "Failover, consensus, and recovery",
      "text": "Failover replaces an unavailable serving node with another node capable of continuing its role. The difficult part is not starting a second server but establishing which server may act as leader and which transactions remain authoritative. A network timeout is evidence of missing communication, not proof that the old primary has stopped. If both old and new primaries accept writes, split-brain behavior can produce divergent histories or conflicting effects on shared resources.\n\nConsensus protocols provide a way for a group to agree on decisions despite specified failures. Paxos and Raft are widely used foundations for replicated logs and leader election. In a conventional majority-based group with \\(2f+1\\) voters, a majority remains possible after up to \\(f\\) voter failures. This statement assumes the protocol’s failure model and suitable communication conditions; it does not imply tolerance of arbitrary malicious behavior.\n\nA majority is useful because majorities intersect, but intersection alone is not the whole safety argument. Election rules, log ordering, and restrictions on commitment ensure that a newly elected leader cannot discard previously committed history. In Raft, terms distinguish leadership epochs, and voting restrictions favor candidates whose logs are sufficiently up to date. When a former leader reconnects, uncommitted entries can be replaced to align its log with the elected history.\n\nConsensus-based replication usually follows a state machine replication model: replicas apply an agreed sequence of commands and produce equivalent state. Commands must be deterministic, or the protocol must agree on nondeterministic inputs as part of the replicated operation. A consensus-backed database may also need safe read protocols. A server that still believes it is leader cannot always answer a current read safely without establishing that its authority remains valid.\n\nSystems with asynchronous standbys face a different promotion problem. The most advanced available replica may still lack transactions acknowledged by the former primary. Recovery can require locating additional log records, accepting a defined amount of data loss, or delaying service until the authoritative history is known. Fencing mechanisms prevent an obsolete primary from continuing to write, for example through storage permissions, network isolation, or monotonically increasing ownership tokens checked by downstream resources.\n\nRecovery objectives distinguish service interruption from information loss. Recovery time objective concerns the intended time to restore service; recovery point objective concerns the acceptable gap in recoverable data. A configuration can have fast failover yet lose recent transactions, or preserve acknowledged transactions while requiring a longer interruption. Reintroducing the old primary also requires care: divergent state may need to be rewound, reconciled, or discarded before the machine can safely become a follower."
    },
    {
      "heading": "Performance, geographical distribution, and scaling limits",
      "text": "Read replication can increase query capacity when replicas have spare processing and storage resources. A routing layer may distribute eligible queries through load balancing, while writes continue to reach the leader. The benefit depends on workload composition. If most resource consumption comes from writes or from maintaining indexes, additional followers do not remove the leader’s bottleneck. Each follower must also apply the replicated changes, consuming resources that are unavailable for serving queries.\n\nReplica reads create application-visible timing issues. A user may submit an update to the leader and immediately read from a lagging follower, apparently losing the update. Session affinity can reduce this problem but does not establish correctness if the selected replica is behind. A stronger method carries a required log position or commit token and serves the read only after a replica reaches it. Waiting improves freshness guarantees at the cost of latency and possible unavailability.\n\nReplication consumes network bandwidth in proportion to the encoded change volume and topology. If a primary produces changes at \\(B\\) bytes per second and sends a full stream directly to \\(k\\) followers, its replication egress is approximately \\(kB\\), before protocol overhead and compression. Cascading can redistribute this traffic, but it does not eliminate the total work of maintaining copies. Logical streams may be smaller or larger than physical streams depending on row sizes, indexes, full-page records, and update patterns.\n\nApply throughput is often constrained by ordering dependencies. Independent transactions may be replayed in parallel, but transactions touching the same records or requiring ordered visibility can limit concurrency. Large transactions create bursts of network, memory, and storage demand. A follower optimized for analytical queries may also replay writes more slowly because of competing scans or different indexing. Lag is therefore not solely a network-distance problem.\n\nIn a simplified backlog model, let the incoming change rate be \\(\\lambda\\) and the replica’s sustainable apply rate be \\(\\mu\\), measured in comparable units. A persistent workload with \\(\\lambda \\geq \\mu\\) cannot drain an existing backlog. If \\(\\mu > \\lambda\\), a backlog of size \\(D\\) takes approximately\n\n\\[T = \\frac{D}{\\mu-\\lambda}\\]\n\nto clear under steady conditions. Real workloads vary, but the relationship explains why a replica with only a small amount of spare capacity may take hours to recover from a brief interruption.\n\nGeographical replication adds placement decisions to these resource constraints. A majority-based group spread across regions normally needs acknowledgments from enough locations to form a quorum. Moving voters closer can reduce latency but concentrate failure risk; placing them farther apart can improve site resilience while increasing communication cost. Distributed SQL systems may use separate replication groups for different data ranges, placing leaders near dominant writers while preserving remotely durable copies. Cross-range transactions then incur additional coordination beyond the replication of any one range."
    },
    {
      "heading": "Initialization, operations, security, and integration",
      "text": "A new replica usually begins with a snapshot and then consumes subsequent changes. The snapshot must correspond to a known point in the change history. If copying and change capture are not coordinated, updates can be missed or counted twice. An online initialization procedure may take a transactionally consistent snapshot, record its associated log position, transfer the snapshot, and replay later records. The source must retain enough history for the replica to complete this process.\n\nLog retention creates an operational coupling between the source and slow consumers. PostgreSQL replication slots, for example, can preserve required write-ahead-log records for a consumer, but a stalled consumer can cause retained storage to grow. Other systems expire history after a configured interval, requiring an excessively delayed replica to be rebuilt. Monitoring therefore distinguishes the health of a replication connection from the amount of retained data, the apply backlog, and the time available before required history disappears.\n\nSchema evolution is particularly important in logical replication. Producers and consumers may temporarily run different software versions during an upgrade. A newly introduced field may be harmless to an extensible consumer, while changing a key or narrowing a numeric type can break application or decoding. Some systems replicate data-definition operations; others require separate schema deployment. Compatibility of ordinary row updates does not establish compatibility of every administrative command.\n\nReplication links also expand the security perimeter. They can carry complete records, deleted values, and sensitive log contents. Authentication, access control, and encrypted transport, commonly using Transport Layer Security, protect different aspects of the channel. Encryption at rest protects stored copies but does not limit which authenticated replication account can retrieve data. Geographical placement and selective replication affect data privacy because each durable copy creates another location where information is retained and governed.\n\nChange data capture exposes database changes for integration rather than necessarily creating an interchangeable standby. Streams can feed search indexes, caches, analytical stores, or stream-processing applications. Consumers must interpret transaction boundaries, duplicate events, deletions, and schema changes. A pipeline that accurately delivers source changes can still produce a different destination state if its transformations are nondeterministic or if failed writes are silently skipped.\n\nValidation and testing address errors that ordinary connectivity checks miss. Row counts can reveal gross omissions but cannot establish equality of content. Range checksums, key-by-key comparisons, and repair scans can detect divergence, although comparisons need a consistent reference point while writes continue. Fault tests commonly include process termination, delayed messages, interrupted links, disk exhaustion, and restart during replay. Observable outcomes include lost acknowledged writes, stale reads, duplicate application, recovery duration, and whether an obsolete leader can continue making externally visible changes."
    }
  ],
  "links": [
    "database",
    "data-center",
    "distributed-computing",
    "database-management-system",
    "data-warehouse",
    "data-backup",
    "write-ahead-logging",
    "database-schema",
    "database-sharding",
    "idempotence",
    "eventual-consistency",
    "database-transaction",
    "acid-properties",
    "cap-theorem",
    "primary-key",
    "distributed-consensus",
    "raft-consensus-algorithm",
    "load-balancing",
    "authentication",
    "access-control",
    "transport-layer-security",
    "data-privacy",
    "change-data-capture",
    "stream-processing"
  ],
  "sources": [],
  "license": "https://creativecommons.org/licenses/by/4.0/"
}