Replication Strategies in System Architecture
medium · System Architecture
Database Replication Strategies Replication is the architectural backbone of high availability and global performance. By distributing data copies across multiple nodes, we eliminate single points of failure and place data geographically closer to the end user. The strategy you choose depends on whether your priority is absolute data consistency or raw write throughput. 1. Leader-Follower (Master-Slave) Replication This is the most common replication pattern. A single node is designated as the Leader (Primary), and all other nodes are Followers (Replicas). Mechanics: All INSERT , UPDATE , and DELETE requests must be sent to the Leader. The Leader executes the transaction and then streams the changes to the Followers asynchronously. Production Use Case: Standard web applications where the majority of traffic is read-only, but strict consistency is required for critical transactional updates. The Bottleneck: If the Leader crashes, the system stops accepting writes until a Follower is promoted to become the new Leader (a process called "failover"). 2. Multi-Master (Leader-Leader) Replication In a Multi-Master setup, there is no single source of truth for writes. Every node in the cluster acts as a Leader and can accept both read and write operations. Mechanics: When a write hits Node A, Node A must propagate that change to Node B and Node C. This creates the "Conflict Resolution" problem: what happens if a user updates the same row on Node A and Node B at the exact same millisecond? Production Use Case: Highly available systems where write operations must be possible at all times, even during network partitions or node outages. The Trade-off: Extremely complex implementation. Most multi-master systems sacrifice strong consistency for availability (Eventual Consistency), meaning users might occasionally see slightly different data depending on which node they query. 3. Read/Write Separation This is an architectural optimization pattern built on top of the Leader-Follower model. It physically separates traffic streams at the routing layer to maximize performance. The Pipeline: 1. The Application Layer uses a smart driver or proxy (like ProxySQL or HAProxy) to inspect every incoming SQL query. 2. INSERT/UPDATE queries are routed to the Primary Leader. 3. SELECT queries are load-balanced across the pool of Read Replicas. The Benefit: This allows you to scale your system's total throughput horizontally simply by adding more Read Replicas, without putting any additional stress on the Primary node. 4. Geo-Replication Geo-Replication involves distributing replicas across different physical geographic data centers (e.g., US-East, Europe-West, India-South) to reduce latency and provide disaster recovery. Latency Optimization: A user in Hyderabad can read their data from a local data center replica in India rather than waiting for a round-trip request to a server in the US. Disaster Recovery: If an entire data center region goes offline due to a natural disaster or power failure, the global database remains online because the other regional clusters hold a full copy of the data. The Challenge: Data must travel over long-distance fiber optic cables. Cross-region replication latency is significantly higher than local replication, making "Strong Consistency" across regions very difficult to achieve. Replication Strategy Reference Matrix Strategy Write Handling Consistency Level Primary Benefit Leader-Follower Leader only High Simple, predictable, ACID-compliant. Multi-Master Any node Variable (Eventual) High availability, local write latency. Read/Write Sep. Leader only High Scales read throughput horizontally. Geo-Replication Regional Leader Eventual Global low latency, disaster resilience.