Overview
An Always On Availability Group (AG) keeps one or more databases synchronised from a primary replica to up to eight secondary replicas by shipping transaction log records. It provides high availability (automatic failover), disaster recovery (asynchronous remote replicas), and optionally read scale-out. It is not a backup (sqlserver-backup-recovery).
This note is written from Microsoft documentation; an AG needs multiple instances and was not executed in the test environment.
Core concepts
- Replica modes: synchronous commit (the primary waits until the secondary has hardened the log; zero data loss; adds commit latency, visible as
HADR_SYNC_COMMITwaits, sqlserver-wait-stats) and asynchronous commit (no wait; possible data loss on failover). - Failover modes: automatic (only between synchronous replicas, with a quorum and a cluster manager) and manual.
- Cluster manager: Windows Server Failover Cluster (WSFC), Pacemaker on Linux, or clusterless AGs (
CLUSTER_TYPE = NONE) for read scale and migrations, with no automatic failover. - Listener: a virtual name and IP that clients connect to; use
MultiSubnetFailover=TrueandApplicationIntent=ReadOnlyto route reads to readable secondaries. - Readable secondaries run on row versioning, so they add version-store load (sqlserver-isolation-rcsi).
- Basic AGs (Standard edition) allow two replicas and one database each; Distributed AGs link two AGs for DR or migration; Contained AGs (2022) replicate logins and Agent jobs with the group.
- Compared with older options: Failover Cluster Instances (shared storage, whole-instance failover), log shipping (simple DR, minutes of lag), replication (data distribution, not HA).
Worked example
Minimal shape of a two-replica AG (T-SQL from the documentation; endpoints and certificates omitted):
CREATE AVAILABILITY GROUP ag_app
WITH (CLUSTER_TYPE = WSFC)
FOR DATABASE appdb
REPLICA ON
N'sql1' WITH (ENDPOINT_URL = N'TCP://sql1:5022', AVAILABILITY_MODE = SYNCHRONOUS_COMMIT,
FAILOVER_MODE = AUTOMATIC, SECONDARY_ROLE (ALLOW_CONNECTIONS = READ_ONLY)),
N'sql2' WITH (ENDPOINT_URL = N'TCP://sql2:5022', AVAILABILITY_MODE = SYNCHRONOUS_COMMIT,
FAILOVER_MODE = AUTOMATIC, SECONDARY_ROLE (ALLOW_CONNECTIONS = READ_ONLY));Health and lag from the primary:
SELECT ar.replica_server_name, drs.synchronization_state_desc, drs.log_send_queue_size,
drs.redo_queue_size, drs.last_commit_time
FROM sys.dm_hadr_database_replica_states drs
JOIN sys.availability_replicas ar ON ar.replica_id = drs.replica_id;A growing log_send_queue_size means the network or secondary cannot keep up; a growing redo_queue_size means the secondary is slow replaying.
Pitfalls and production notes
- Logins, Agent jobs and linked servers are instance-level and are not replicated (except in a contained AG). Script and synchronise them or failover breaks the application.
- A synchronous replica that goes down can stall the primary unless the cluster demotes it; understand
REQUIRED_SYNCHRONIZE_COMMIT_TO_COMMIT. - Quorum design matters; use a witness (file share, cloud) to avoid split brain.
- Test failover and application reconnection regularly; connection strings must use the listener.
- The log cannot truncate until all replicas have received it; a dead replica makes the primary log grow (
log_reuse_wait_desc = AVAILABILITY_REPLICA). - Patch secondaries first, then fail over, then patch the old primary.
Practice questions
- Which replica configuration gives zero data loss with automatic failover, and what does it cost?
- Why can a failed secondary fill the primary disk?
- How do you keep logins in sync across replicas?
References
- Microsoft Learn, What is an Always On availability group?
Related
sqlserver-backup-recovery · sqlserver-wait-stats · sqlserver-agent-maintenance · pg-replication-failover