MSAZ-282 · Lesson 6 of 12 · 22 min

Active geo-replication

▶ Video coming soon A video version of this lesson is on the way.

Built-in availability keeps a database running when a node or a zone fails. It does not help when the whole region is in trouble, or when a networking fault cuts a region off from the rest of the internet. For that, a copy of the database must already be running somewhere else. Azure SQL Database offers two features for this: active geo-replication, which works database by database, and failover groups, which are built on the same replication and add group management and stable connection names. This lesson covers geo-replication, because it is the foundation; lesson 7 covers failover groups, which Microsoft recommends for most production designs. Active geo-replication is a feature of Azure SQL Database only. Managed Instance uses failover groups for the same purpose.

How geo-replication works

Active geo-replication creates a readable secondary database on a logical server in another region, or in the same region if that is what you need. The secondary is seeded with a full copy of the primary and then kept up to date by streaming the transaction log, using the same technology as availability groups. Replication is asynchronous: a transaction commits on the primary as soon as it is hardened there, without waiting for the secondary. That keeps the primary fast whatever the distance between regions, at the cost of a small window of data, usually well under a few seconds, that has not yet reached the secondary. A primary can have up to four secondaries, and a secondary can itself be the source of a further secondary, although Microsoft advises keeping chains short.

Two region boxes. The left box is labelled primary region and contains a logical server with the Finch Rewards database marked primary, read-write. The right box is labelled paired region and contains a second logical server with a database marked geo-secondary, read-only. An arrow from left to right is labelled asynchronous log stream, lag usually under a few seconds. Below, a planned failover arrow shows the roles swapping with no data loss, and a forced failover arrow shows the secondary becoming primary immediately, with a note that data not yet replicated may be lost.
Active geo-replication streams the log asynchronously to a readable secondary in another region; failover can be planned, with no data loss, or forced.

Each secondary is a separate database with its own compute size, its own firewall context through its logical server, and its own cost. Microsoft's guidance is that a secondary that may become the primary should use the same service tier and compute size as the primary. A smaller secondary saves money until the day it must take the whole production workload, when it may also fall behind during busy periods because it cannot apply the log as fast as a larger primary generates it. Secondaries are readable, so they can carry reports or read-only API calls in the other region, which offsets some of their cost.

Configuring geo-replication

Geo-replication can be set up in the portal, with PowerShell or the CLI, or with T-SQL run in the master database of the primary's logical server. The secondary's logical server must already exist, and the identity used must have permission on both servers. If the database uses Microsoft Entra authentication only, the Entra administrators and users carry over, because users are stored in the database; logins on the logical servers, used by SQL authentication, must be created on the secondary server with the same security identifiers, or users will be orphaned after a failover.

Creating a geo-secondary of Finch Rewards in the paired region with the same compute size, then listing the replication links.
az sql db replica create --resource-group rg-rewards-prod-cc --server hf-sql-rewards-prod-cc \
    --name finchrewards --partner-resource-group rg-rewards-dr-ce --partner-server hf-sql-rewards-dr-ce \
    --service-objective GP_Gen5_8 --secondary-type Geo

az sql db replica list-links --resource-group rg-rewards-prod-cc --server hf-sql-rewards-prod-cc \
    --name finchrewards --output table
Adding a geo-secondary with T-SQL, and checking its state and lag.
-- Run in master on the primary's logical server.
ALTER DATABASE finchrewards
    ADD SECONDARY ON SERVER [hf-sql-rewards-dr-ce]
    WITH (ALLOW_CONNECTIONS = ALL, SERVICE_OBJECTIVE = 'GP_Gen5_8');

-- Check the link from the primary database.
SELECT partner_server, partner_database, replication_state_desc, role_desc,
       replication_lag_sec, last_replication
FROM sys.dm_geo_replication_link_status;

Choosing the secondary region

Each Azure region has a paired region, chosen by Microsoft, which is usually the natural place for a disaster-recovery secondary: Azure staggers planned updates across pairs, prioritizes the recovery of one region of each pair in a widespread outage, and geo-redundant storage, including the default backup storage, replicates to the pair. For Harrow & Finch the pair is Canada East, in Quebec City. Pairing is not compulsory: a secondary can be placed in any region where the service is available, and some newer regions have no pair at all. The choice should weigh distance, which affects replication lag, residency rules, which keep personal information in Canada under the company's own policy, and the capacity available in the target region in a crisis.

Monitoring replication

The most important number for geo-replication is the replication lag: the time between a transaction committing on the primary and it being applied on the secondary. It is reported by sys.dm_geo_replication_link_status as replication_lag_sec, and as a metric that can drive the alerts from lesson 3. Lag rises during large batch operations, index rebuilds and data loads, because they generate log faster than it can be shipped and replayed, and it is the best estimate of the data that would be lost in a forced failover at that moment. Walt sets a metric alert at 30 seconds of lag, sustained for 10 minutes, for the Finch Rewards secondary.

Worked problem

How much data could a forced failover lose?

During the Saturday evening peak, Finch Rewards commits about 45 point-earning transactions per second. The replication lag is normally 2 seconds, but during a nightly index rebuild it reached 18 seconds. Estimate the transactions at risk in a forced failover in each case.

Show solution
  1. Normal: 45 transactions per second × 2 seconds = 90 transactions.
  2. During the rebuild: 45 × 18 = 810 transactions, although the evening load would not usually coincide with the 02:00 rebuild.
  3. At 02:00 the load is about 3 transactions per second, so the real exposure during the rebuild is about 3 × 18 = 54 transactions.

Answer: About 90 transactions are at risk at the evening peak and about 54 during the rebuild. Both are within the 1-hour RPO for Finch Rewards, and the loyalty team can replay missed point awards from the till system's own records.

Planned and forced failover

There are two ways to change roles. A planned failover, which the portal calls Failover and T-SQL calls FAILOVER, first synchronizes the secondary completely, then swaps the roles, so no committed data is lost. It needs both regions to be working, and is used for drills, planned region moves and failing back after a disaster. A forced failover, called forced failover allowing data loss, makes the secondary the primary at once without waiting for synchronization. It is used when the primary region is down or unreachable, and anything that had not reached the secondary is lost. After a forced failover, when the old primary comes back, it becomes a secondary and any transactions it had that were never replicated are discarded.

The two role changes for a geo-replicated database. Both are run on the server that will become primary.
-- Planned failover: run in master on the SECONDARY's logical server.
ALTER DATABASE finchrewards FAILOVER;

-- Forced failover when the primary region is unavailable, also run on the secondary's server:
ALTER DATABASE finchrewards FORCE_FAILOVER_ALLOW_DATA_LOSS;

Applications are the hard part of geo-replication. Because each database's primary and secondary have different server names, an application connected to hf-sql-rewards-prod must be told to connect to hf-sql-rewards-dr after a failover, by changing its configuration, its DNS alias or its connection logic. For one database, this is manageable. For a group of databases that must fail over together, such as an application that uses three databases, doing it one database at a time risks a split where some databases are in one region and some in the other. That is exactly the problem failover groups solve.

Geo-replication and failover groups compared. Failover groups are the subject of the next lesson.
QuestionActive geo-replicationFailover group
Available forAzure SQL DatabaseAzure SQL Database and Managed Instance
Unit of failoverOne databaseA group of databases (all user databases on Managed Instance)
Secondaries per primaryUp to fourOne, in another region
Connection names after failoverChange: each database's server name differsStay the same: listener endpoints follow the primary
Secondary in the same regionPossibleNo
Typical useRead scale-out across regions, special casesRecommended for most disaster-recovery designs

Cost and licensing

A geo-secondary is billed like any database of its size. For Finch Rewards, an 8-vCore General Purpose secondary roughly doubles the database's compute cost, plus storage in the second region. Two features can reduce the bill. If the secondary is used only for disaster recovery and carries no read workload, it can be designated as a standby replica, which removes the SQL Server licence portion of the vCore price under Microsoft's failover rights. And a secondary used for reads replaces separate reporting copies that would otherwise cost money too.

Worked problem

What does the standby designation save?

Finch Rewards' secondary uses 8 vCores. Use illustrative prices: 0.40 dollars per vCore hour for compute with the SQL licence included, of which 0.14 is the licence portion. The secondary runs 730 hours a month. What does the secondary cost a month with and without the standby designation?

Show solution
  1. With the licence included: 8 × 730 × 0.40 = 2,336 dollars a month.
  2. Without the licence portion: 8 × 730 × (0.40 − 0.14) = 8 × 730 × 0.26 = 1,518.40 dollars.
  3. Saving: 2,336 − 1,518.40 = 817.60 dollars a month.

Answer: At these illustrative prices the standby designation saves about 818 dollars a month, but only if the secondary carries no read workload. If the loyalty team wants regional reads, the saving is lost.

Geo-replication and security

Security settings travel unevenly between a primary and its secondary, and the differences surface only after a failover. Transparent data encryption is replicated with the database: if the primary uses a customer-managed key in Azure Key Vault, the secondary's logical server must have access to the same key, or to a key vault in its own region holding the same key material, before the link is created, otherwise replication cannot start. Auditing settings, Microsoft Defender for SQL and diagnostic settings belong to the logical server and the database resource, so they must be configured on the secondary too. Walt adds a short check to the template that creates each secondary: it deploys the same auditing, diagnostic and Defender settings as the primary, so the secondary is never the one database in the estate that nobody is watching.

Networking is the other half. If the primary is reached only through a private endpoint, the secondary needs its own private endpoint in a virtual network in the secondary region, and the application servers that will run there after a failover need a route to it. Public network access, firewall rules and connection policy are server settings and are not copied. In practice, most of the work of geo-replication is not the replication itself, which is a single command, but making sure everything around the secondary is ready for the day it becomes the primary.

When geo-replication is the right choice

Because failover groups are recommended for most disaster-recovery designs, Walt asks Elena when he would still choose plain geo-replication. She gives three cases. The first is read scale-out across regions: a database that needs readable copies in two or more other regions, for example for a reporting team in another country, can have up to four geo-secondaries, while a failover group has only one secondary. The second is a single database whose application already handles the change of server name, such as a service that reads its connection string from a configuration store that can be updated during failover. The third is a migration or region move: a secondary is created in the new region, the application is switched with a planned failover, and the old primary is then removed. For Finch Rewards, which needs one secondary for disaster recovery and stable connection names, a failover group is the better fit, and the next lesson builds one on top of the same replication.

Common mistakes

  • MistakeGeo-replication is synchronous, so a failover never loses data.

    InsteadGeo-replication is asynchronous; a forced failover can lose what has not yet replicated.

  • MistakeManaged Instance supports active geo-replication.

    InsteadActive geo-replication is for SQL Database; Managed Instance uses failover groups.

  • MistakeA planned failover is the right command when the primary region is down.

    InsteadA planned failover needs both regions; use a forced failover allowing data loss when the primary is unavailable.

  • MistakeA smaller secondary is a safe way to save money.

    InsteadA secondary that may become primary should match the primary's tier and size.

Summary and key terms

  • Active geo-replication keeps up to four readable secondaries of a SQL database in other regions by streaming the log asynchronously.
  • Match the secondary's tier and size to the primary's, and consider the paired region first.
  • Replication lag, shown in sys.dm_geo_replication_link_status, estimates the data a forced failover would lose.
  • A planned failover synchronizes first and loses nothing; a forced failover is immediate and may lose recent changes.
  • Each database's servers have different names, so applications must change connections after a failover, which failover groups avoid.
  • Secondaries cost like any database; a standby designation can remove the licence portion if no reads run there.
Active geo-replication
A SQL Database feature that keeps readable secondary copies of a database in other regions.
Geo-secondary
A readable copy of a database kept up to date by geo-replication.
Replication lag
The delay between a commit on the primary and its application on the secondary.
Planned failover
A role change that synchronizes first, so no data is lost.
Forced failover
An immediate role change that allows the loss of changes not yet replicated.
Paired region
The region Microsoft pairs with another for staggered updates and recovery priority.

Practice problems

Work each problem on paper first, then reveal the answer to check.

  1. How many geo-secondaries can one primary have?

    Show answer

    Up to four.

  2. Which view shows the replication lag of a geo-replication link?

    Show answer

    sys.dm_geo_replication_link_status, in the replication_lag_sec column.

  3. Where is the planned failover command run?

    Show answer

    On the logical server of the secondary that will become primary.

  4. Why do users become orphaned after failover with SQL authentication?

    Show answer

    The logins on the secondary server must exist with the same security identifiers.

  5. What is the Harrow & Finch secondary region?

    Show answer

    Canada East, the paired region.

Lesson quiz · 5 questions

Score 60% or more to complete this lesson.

Take the quiz