Sub topic 3
Replicating State and Recovering Safely
Assume you understand basic client-server architecture, load balancers, DNS, databases, timeouts, and health checks. This chapter focuses on keeping state consistent while a service changes instances.
Synchronous and Asynchronous Replication
Replication copies state, such as database records, from one component to another.
- Synchronous replication confirms a write only after the required replicas store it. It gives a low RPO (recovery point objective) because little or no acknowledged data is lost. The trade-off is higher write latency and reduced availability when a replica or network link is unavailable.
- Asynchronous replication confirms a write before every replica receives it. It usually improves latency and availability, but a failure can lose changes still waiting to replicate.
| Choice | Main benefit | Main risk |
|---|---|---|
| Synchronous | Minimal acknowledged-data loss | Writes wait for replicas |
| Asynchronous | Lower latency and better partition tolerance | Recent writes may be lost |
RTO and RPO
RTO (recovery time objective) is the maximum acceptable time to restore service. RPO is the maximum acceptable amount of recent data loss, measured in time.
Replication affects both objectives, but it does not guarantee either one. A design with synchronous replication can still have a long RTO if promotion requires manual approval. A design with asynchronous replication can meet a short RTO while accepting a five-minute RPO.
Quorum and Leader Election
A quorum is the minimum number of members required to make a valid decision. In a three-member cluster, a majority of two can elect a leader. The leader accepts writes, while followers replicate them.
Majorities prevent two isolated groups from both believing they control the cluster. Configure an odd number of voting members when possible, and place them in separate fault domains.
Split Brain and Fencing
Split brain occurs when two nodes both act as leader. It can cause conflicting writes. Quorum reduces this risk, but systems also use fencing, which removes the old leader's ability to write.
A fencing token is a monotonically increasing number. Storage rejects writes with an older token:
class FencedStorage:
def __init__(self):
self.highest_token = 0
self.value = None
def write(self, token, value):
if token < self.highest_token:
raise PermissionError("stale fencing token")
self.highest_token = token
self.value = valueFailback and Testing
Failback returns service to the recovered component. First resynchronize it, verify its data, and restore redundancy. Switch traffic gradually rather than treating recovery as proof of correctness.
Test failover with scheduled drills, game days, and fault injection. Record detection time, promotion time, RTO, observed data loss, and recovery steps. Test lost nodes, broken network paths, stale replicas, and delayed health checks.
Exercise
A database has a five-minute asynchronous replication lag. What RPO should you plan for during a primary failure?
Show answer
Plan for up to five minutes of lost acknowledged data, unless another mechanism provides a stricter guarantee.