powershelldba.de

Distributed Availability Groups: Your DBA Tool for Migrations and Multi-Site Deployments

Distributed Availability Groups (DAGs) solve a specific problem: how to move an entire SQL Server database across sites with zero downtime while maintaining geographic redundancy. Here's what they do and why they matter.

What is a Distributed Availability Group?

A Distributed Availability Group (DAG) chains two separate AlwaysOn availability groups across different sites. For automated setup of AlwaysOn infrastructure including DAG configurations, see our AlwaysOn Setup automation guide and the AlwaysOnSetup tool.

Site A (Primary): AlwaysOn AG with local replicas
Site B (Secondary): AlwaysOn AG with local replicas
Connection: Primary AG → (async) → Secondary AG

The primary AG in Site A serves the database normally. The entire primary AG replicates asynchronously to the secondary AG in Site B. Each site has its own availability group — you're replicating between AGs, not just between replicas.

This is different from a simple AlwaysOn replica spread across sites. A DAG gives you local failover capability at each site plus geographic distribution.

Why DAGs Exist: The Migration Problem

Before DAGs, migrating a production database across datacenters meant:

Even with "fast" failover, this was risky and required maintenance windows.

DAGs solve this: You can fail over from the primary AG to the secondary AG with zero downtime, no backup/restore, no cutover risk.

DAG Architecture

Two-Site Example

┌─ Datacenter A (Primary) ─────────────────┐
│                                           │
│  ┌─ Availability Group: AG_Primary ──┐   │
│  │ ┌─ Replica 1 (Primary)          │   │
│  │ ├─ Replica 2 (Secondary, Sync) │   │
│  │ └─ Replica 3 (Secondary, Async)│   │
│  └───────────────────────────────────┘   │
│                                           │
└───────────────────────────────────────────┘
              │
              │ Asynchronous replication
              │ (DAG link)
              ▼
┌─ Datacenter B (Secondary) ───────────────┐
│                                           │
│  ┌─ Availability Group: AG_Secondary ──┐ │
│  │ ┌─ Replica 1 (Primary in this AG) │  │
│  │ ├─ Replica 2 (Secondary, Sync)   │  │
│  │ └─ Replica 3 (Secondary, Async)  │  │
│  └───────────────────────────────────┘  │
│                                           │
└───────────────────────────────────────────┘

Key Components

How Failover Works

Normal Operation

  1. Applications connect to primary AG listener (Site A)
  2. Reads/writes hit the primary database
  3. Replicas in Site A sync synchronously (RPO = 0)
  4. Secondary AG (Site B) receives logs asynchronously
  5. If a Site A replica fails, local failover is fast

Site A Disaster (Full Datacenter Down)

  1. Primary AG is unreachable
  2. Applications get connection timeout
  3. DBA manually promotes secondary AG to primary (one command)
  4. Applications update their connection string to secondary AG listener
  5. Applications reconnect and continue
  6. RPO = asynchronous lag (potentially minutes of data loss)
  7. RTO = time to promote + application reconnect (typically minutes)

Planned Failover (Migration)

  1. Verify secondary AG is caught up
  2. Failover primary AG internally (replicas stay in Site A)
  3. Promote secondary AG to primary
  4. Redirect traffic from primary AG listener to secondary AG listener
  5. No data loss (if sync replicas caught up first)
  6. Zero downtime (applications stay connected via listeners)

DAG vs. Single-Site AlwaysOn

Aspect Single-Site AlwaysOn Distributed AG
Replicas All in same site Primary AG + Secondary AG (different sites)
Local Failover Fast (within site) Fast (within each site)
Site Failure Failover to secondary in same site (limited) Promote entire secondary AG to primary
Geo Distribution Can have remote replicas (async) Built-in geographic distribution
Migration Complex (requires manual failover) Simple (failover between AGs)
RPO Zero (sync replicas) Zero (primary site) + async to secondary
RTO Sub-minute (within site) Sub-minute local + minutes for site failover
Complexity Medium High (two AGs to manage)

When to Use Distributed Availability Groups

Excellent Use Cases

When DAGs are Overkill

The Migration Workflow

Step 1: Set Up Secondary AG

In the new site, create a new AlwaysOn AG with the same database (via backup/restore initially). Both AGs are independent at this point.

Step 2: Create the DAG Link

Link the primary AG to the secondary AG. Specify the forwarder replica and replication mode (async). The secondary AG now receives logs from the primary.

-- Pseudo-SQL (actual syntax varies)
CREATE DISTRIBUTED AVAILABILITY GROUP 'DAG_Prod'
PRIMARY AVAILABILITY GROUP 'AG_Site_A'
SECONDARY AVAILABILITY GROUP 'AG_Site_B'
LISTENER 'dag-listener'
WITH (ASYNC_COMMIT_TIMEOUT = 30);

Step 3: Verify Synchronization

Wait for the secondary AG to catch up. Monitor the forwarder replica's log-send queue. Ensure no lagging.

Step 4: Promote Secondary AG

When ready, promote the secondary AG to primary. This makes it the new primary and allows writes.

Step 5: Redirect Applications

Update connection strings from primary AG listener to secondary AG listener. Applications reconnect and continue.

Step 6: Decommission Old Primary (Optional)

Once applications are stable on the new site, you can remove the old primary AG. But many teams keep it as a cold standby.

Operational Challenges

Two Availability Groups to Monitor

DAGs double your monitoring complexity. You now track:

Asynchronous Replication Risk

The DAG link (primary AG to secondary AG) is asynchronous. If the primary site catastrophically fails, you may lose uncommitted transactions. This is acceptable for most use cases but not for zero-data-loss RPO requirements.

Application Connection Logic

Applications must either:

This is more complex than a single AlwaysOn AG listener.

Licensing

DAGs require SQL Server Enterprise Edition at both sites. Each replica is a licensed instance. Licensing costs scale with the number of sites and replicas.

Best Practices

1. Plan Your Topology Before Building

Decide:

2. Test Failover Regularly

Practice promoting the secondary AG to primary without real incidents. This tests your automation, runbooks, and application logic.

3. Monitor Forwarder Log Queue Depth

The forwarder replica sends logs to the secondary AG. If its queue grows, it means:

Alert on queue depth > 100 MB or > 10 seconds of lag.

4. Automate Failover Detection and Routing

Manual failover is error-prone. Implement automation (via orchestration tools or custom scripts) to:

5. Document Your DAG Topology

Distributed AG: DAG_Production
├─ Primary AG (Site A): AG_DatacenterA
│  ├─ Replica 1: sql-dc-a-01 (Primary)
│  ├─ Replica 2: sql-dc-a-02 (Secondary, Sync)
│  └─ Replica 3: sql-dc-a-03 (Secondary, Async, Forwarder)
│
└─ Secondary AG (Site B): AG_DatacenterB
   ├─ Replica 1: sql-dc-b-01 (Primary in AG)
   ├─ Replica 2: sql-dc-b-02 (Secondary, Sync)
   └─ Replica 3: sql-dc-b-03 (Secondary, Async)

Replication mode (DAG): Asynchronous, 30-second timeout
Last tested failover: 2024-01-20
Estimated RTO: 5–10 minutes (primary AG failure + app reconnect)
Estimated RPO: 1–2 minutes (async lag)

Real-World Example: Migration with Zero Downtime

A financial services company needed to move their core trading database from an old datacenter (NYC) to a cloud region (US-East). They:

  1. Set up AlwaysOn AG in the cloud with a backup from production
  2. Created a DAG linking NYC primary AG to cloud secondary AG
  3. Monitored sync for 2 weeks (to ensure stability and test failover)
  4. Promoted cloud AG to primary (1 command, 30 seconds)
  5. Updated connection strings (automated via infrastructure-as-code)
  6. Applications reconnected automatically (RTO = 2 minutes)
  7. Zero downtime, zero data loss, zero customer impact

Without DAGs, this would have required a maintenance window and manual cutover risk. With DAGs, it was a routine operation.

Alternatives to DAGs

Log Shipping

Simple, cheap, asynchronous. But recovery is slow and requires manual failover.

Transactional Replication

Works across continents. But not suitable for entire database failover — designed for partial replication.

Azure Site Recovery (ASR) or Backup + Restore

Automated failover for VMs or databases in the cloud. But requires planned maintenance windows.

AlwaysOn with Distributed Replicas (No DAG)

Put replicas in different sites within a single AG. Simpler than DAG but limits your failover options.

Summary

Distributed Availability Groups are a powerful tool for production migrations and geographic redundancy. They allow:

The trade-off is complexity: you manage two AGs instead of one, monitor more replicas, handle async replication risks, and update application connection logic.

Use DAGs when you need to migrate or distribute databases across sites without downtime. Use single-site AlwaysOn when you need high availability within a single site.

If you're planning a major infrastructure change or geographic expansion, DAGs belong in your design. If your database is staying put, they're unnecessary overhead.