How would you design SQL Server high availability and disaster recovery for a payments database that must survive losing a datacenter with at most seconds of data loss?
answer
- two sites, two commit modes
- automatic failover needs synchronous partners
- quorum votes outside both sites
- replicas copy mistakes too
basics
~20 sUse an availability group with synchronous, automatic-failover replicas in the main datacenter and an asynchronous replica in the second, place cluster quorum so one site's loss is survivable, and keep log backups or delayed log shipping, because replicas copy mistakes too.
solid answer
~50 sI would start from what each failure costs. Inside the main datacenter, two replicas in **synchronous-commit** mode with `FAILOVER_MODE = AUTOMATIC` give zero-loss automatic failover when a node dies. The second datacenter gets an **asynchronous-commit** replica: no latency added to payments and usually seconds of lag, but losing the main site means a forced failover (`FORCE_FAILOVER_ALLOW_DATA_LOSS`) that loses whatever had not shipped, so I would alert on `secondary_lag_seconds` against the promise. If the inter-site round trip is small enough, a synchronous replica there removes that loss at a cost on every commit. Cluster votes need balancing between the sites plus a witness in a third location, or forced quorum becomes part of the runbook. Clients connect through the listener with `MultiSubnetFailover=True`. And because replicas apply a bad `DELETE` within seconds, frequent log backups with tested restores, or log shipping with a restore delay, stay in the design.
go deeper
Recall that synchronous commit can lose nothing and fail over automatically, while asynchronous commit can lose recent work and fails over only by force.
Explain why the main-site replicas are synchronous and the remote one asynchronous, and what the listener and cluster quorum each contribute.
Walk through a site loss end to end: forced quorum, forced failover, suspended databases on the old primary, and how you measure the data-loss window beforehand.
Own the trade between per-commit latency and site-loss data loss, the cost of a third-site witness and licences, and the drills that keep the promise honest.
## Start from the failures A payments database has to answer three different questions, and SQL Server has a different tool for each: | Failure | Tool | Typical data loss | |---|---|---| | A server or instance dies | Availability group, synchronous commit, automatic failover | None | | The whole main datacenter is lost | Replica in the second datacenter | Seconds with asynchronous commit, none with synchronous | | A person or a bug destroys data | Log backups, or log shipping with a restore delay | Back to the moment before the mistake | "At most seconds" is a statement about the second and third rows. The first row should lose nothing. ## Inside the main datacenter Put two (or three) replicas of an **Always On availability group** in the main datacenter, configured `AVAILABILITY_MODE = SYNCHRONOUS_COMMIT` and `FAILOVER_MODE = AUTOMATIC`. Every commit waits for the local partner to harden the log, which costs a local network round trip, and a node failure becomes an automatic failover with no committed data lost, provided the partner was `SYNCHRONIZED` and the cluster had quorum. Why an availability group and not only a failover cluster instance: an FCI keeps one copy of the data on shared storage, so it protects against a server failing but not against losing that storage or the site that holds it. An FCI can still host one replica of the group, but such a replica is limited to manual group failover. ## Across datacenters: the commit-mode decision This is the real judgement call. - **Asynchronous commit to the second site.** No latency added to payments; the remote copy is usually seconds behind. Losing the main site means a forced failover with possible loss of whatever had not been sent. Automatic failover is not possible to an asynchronous replica. - **Synchronous commit to the second site.** Zero loss even for a site failure, but every commit pays the inter-site round trip and the remote log flush, and a struggling link slows payments. It suits sites close enough that the round trip is a small fraction of commit time. - **A distributed availability group.** Two separate groups, each on its own cluster, linked through their listeners; one site's cluster problems cannot take down the other, and failover between them is always manual. Many teams pick asynchronous commit and then measure the promise: `secondary_lag_seconds` and `log_send_queue_size` in `sys.dm_hadr_database_replica_states`, with an alert when lag exceeds the seconds the business accepted. If the promise is "zero", the answer is synchronous commit or a closer second site, not monitoring. ## Quorum and the listener On Windows the group relies on a **Windows Server Failover Cluster**, and the cluster needs a majority of votes to stay online. With two nodes in the main site and one in the second, losing the main site leaves one vote of three, so the cluster stops and an operator must **force quorum** on the surviving node before forcing failover. Balancing the votes between the two sites, removing one with the cluster's NodeWeight setting if needed, and adding a witness in a third location means that losing either site still leaves a majority. The listener should carry an IP address in each site's subnet, and clients should use `MultiSubnetFailover=True` so reconnection is quick. ## What replication does not protect Replicas replay every change, including mistakes. A `DELETE` without a `WHERE` reaches the second datacenter in seconds. So the design also needs: 1. **Frequent log backups**, taken on whichever replica the backup preference chooses, with a tested restore to a point in time into a separate database. 2. Optionally **log shipping** to another server with a restore delay, so a recent mistake can be read from a copy that has not applied it yet. 3. Restore drills, because an untested backup is a hypothesis. ## Operating it - Keep logins, permissions and SQL Server Agent jobs identical on every replica, or use a **contained availability group** (SQL Server 2022 and later) so they fail over with the group. - Write jobs so that they act only on the current primary. - Rehearse both a planned failover and a forced one, and time the forced one end to end, including the forced-quorum step. - After a forced failover, the old primary's databases come back suspended; resume them only after deciding what to do with any transactions they hold that never reached the new primary. The standing cost of all this is licences, a third site or witness, and a team that practises failovers. That cost is the decision a lead owns.
- When would you choose a SQL Server distributed availability group over one availability group stretched across both datacenters?When each datacenter should run its own cluster, so that a quorum or cluster problem in one site cannot take the other offline, or when the sites sit in different domains or platforms. The distributed group links two groups through their listeners, the second group's primary forwards the log to its own secondaries, and failing over between the groups is always manual.
- What does a contained availability group change in this two-site design?From SQL Server 2022, a contained availability group has its own master and msdb databases named after the group, so logins, permissions and SQL Server Agent jobs created through its listener fail over with the databases. Without it every replica needs those objects copied by hand, and a disaster-recovery failover often fails on a missing login or job rather than on data.
- A synchronous replica in the second datacenter costs nothing if the network is fast.
- An availability group replaces backups because every replica is already a copy.
- An asynchronous replica can be configured as an automatic failover target.
- A failover cluster instance on one site's shared storage survives losing that site.
- Quorum takes care of itself with an equal number of votes in each datacenter.