# Database Replication

Database replication is the process of keeping a copy of the same data on multiple machines (nodes) that are connected via a network.

## 1. Why Replicate?

*   **High Availability:** If one machine goes down, others can continue serving data.
*   **Latency:** Keep data geographically closer to users (e.g., US users connect to US replica, EU users to EU replica).
*   **Scalability:** Distribute the load. You can have one machine handle writes and 10 machines handle reads.

## 2. Leader-Based Replication (Master-Slave)

This is the most common mode (used by MySQL, PostgreSQL, MongoDB).

*   **Leader (Master):** Handles all **Write** requests (INSERT, UPDATE, DELETE). It writes to its local storage and sends the data change stream to followers.
*   **Followers (Slaves):** Handle **Read** requests. They receive the change stream from the leader and update their own local copy.

### Pros & Cons
*   **Pros:** Simple to understand. No write conflicts (only one place to write).
*   **Cons:** If the leader fails, a failover process must promote a follower to be the new leader. Write throughput is limited to the capacity of a single node.

## 3. Multi-Leader Replication (Multi-Master)

In this setup, more than one node can accept writes. Replication happens between all leaders.

*   **Use Case:** Multi-datacenter applications. You have a leader in the US and a leader in the EU.
*   **Conflict Resolution:** The biggest challenge. If User A updates a record in the US and User B updates the *same* record in the EU at the same time, the databases must resolve the conflict (e.g., Last Write Wins).

## 4. Synchronous vs. Asynchronous

*   **Synchronous:** The leader waits for the follower to confirm the write before telling the client "Success".
    *   *Pros:* Guaranteed consistency. No data loss if leader fails.
    *   *Cons:* If the follower is slow or down, the write blocks.
*   **Asynchronous:** The leader sends the write to the follower but doesn't wait for confirmation.
    *   *Pros:* Fast writes. Leader isn't slowed down by followers.
    *   *Cons:* If the leader crashes before sending the data, that write is lost (Eventual Consistency).

[[programming/distributed-systems]]
[[programming/database-basics]]
[[programming/cap-theorem]]