1.Primary/replica architecture and sync vs async replication
A primary/replica setup has one instance (the primary) that accepts writes, and one or more replicas that receive a continuous copy of those writes. Synchronous replication waits for a replica to confirm before the primary reports success — safer, slower. Asynchronous replication reports success immediately and streams the change to replicas afterward — faster, with a real chance of losing the most recent writes if the primary fails first.
Key features
- Primary (leader): the only instance that accepts writes in a single-primary setup
- Replica (follower): receives a stream of the primary's changes and applies them, usually serving reads
- Synchronous replication: the primary waits for a replica to acknowledge before confirming the write — no data loss on primary failure, but higher write latency
- Asynchronous replication: the primary confirms the write immediately, replicating in the background — lower latency, but the most recent writes can be lost if the primary fails before replicating
- Semi-synchronous is a common middle ground: wait for at least one replica to acknowledge, replicate to the rest asynchronously
Synchronous vs asynchronous replication
- Client — writes
- leads to Primary (write)
- Primary — accepts the write
- leads to Replica (replicate)
- on error, leads to Confirm (async) (immediately)
- Confirm (sync) — after replica ack
- Confirm (async) — before replica has it
- Replica — receives the stream
- leads to Confirm (sync) (ack received)
Command
write → primary
primary → [sync] wait for replica ack → confirm to client
primary → [async] confirm to client → stream to replicaPrimary accepts writes, replicas receive a stream of them. Synchronous replication waits for a replica ack before confirming (safer, slower); asynchronous confirms immediately (faster, can lose the newest writes on failover).