--- title: "Replication Subscribers & availability groups (SQL Server)" description: Learn about configuring a replication subscriber with a SQL Server Always On availability group. ms.custom: seo-lt-2019 ms.date: "08/08/2019" ms.prod: sql ms.reviewer: "" ms.technology: high-availability ms.topic: conceptual helpviewer_keywords: - "failover subscribers with AlwaysOn" - "Availability Groups [SQL Server], interoperability" - "replication [SQL Server], AlwaysOn Availability Groups" ms.assetid: 0995f269-0580-43ed-b8bf-02b9ad2d7ee6 author: MashaMSFT ms.author: mathoma --- # Replication Subscribers and Always On Availability Groups (SQL Server) [!INCLUDE[appliesto-ss-xxxx-xxxx-xxx-md](../../../includes/appliesto-ss-xxxx-xxxx-xxx-md.md)] When an Always On availability group containing a database that is a replication subscriber fails over, the replication subscription might fail. For transactional replication push subscribers, the distribution agent will continue to replicate automatically after a failover if the subscription was created using the AG listener name. For transactional replication pull subscribers, the distribution agent will continue to replicate automatically after a failover, if the subscription was created using the AG listener name and the original subscriber server is up and running. This is because the distribution agent jobs only get created on the original subscriber (primary replica of the AG). For merge subscribers, a replication administrator must manually reconfigure the subscriber, by recreating the subscription. ## What is Supported [!INCLUDE[ssNoVersion](../../../includes/ssnoversion-md.md)] replication supports the automatic failover of the publisher and the automatic failover of transactional subscribers. Merge subscribers can be part of an availability group, however manual actions are required to configure the new subscriber after a failover. Availability Groups cannot be combined with WebSync and SQL Server Compact scenarios. ## How to Create Transactional Subscription in an Always On Environment For transactional replication, use the following steps to configure and failover a subscriber availability group: 1. Before creating the subscription, add the subscriber database to the appropriate Always On availability group. 2. Add the subscriber's availability group Listener as a linked server to all nodes of the availability group. This step ensures that all potential failover partners are aware of and can connect to the listener. 3. Using the script in the **Creating a Transactional Replication Push Subscription** section below, create the subscription using the name of the availability group listener of the subscriber. After a failover, the listener name will always remain valid, whereas the actual server name of the subscriber will depend on the actual node that became the new primary. > [!NOTE] > The subscription must be created by using a [!INCLUDE[tsql](../../../includes/tsql-md.md)] script and cannot be created using [!INCLUDE[ssManStudio](../../../includes/ssmanstudio-md.md)]. 4. To create a pull subscription: 1. Using the sample script in the **Creating a Transactional Replication Pull Subscription** section below, create the subscription using the name of the availability group listener of the subscriber. 2. After a failover, create the distribution agent job on the new Primary replica using the **sp_addpullsubscription_agent** stored procedure. When you create a pull subscription, with the subscription database in an Availability Group, after every failover, it is recommended to disable the distribution agent job on the old Primary replica and enable the job on the new primary replica. ## Creating a Transactional Replication Push Subscription ``` -- commands to execute at the publisher, in the publisher database: use [] EXEC sp_addsubscription @publication = N'', @subscriber = N'', @destination_db = N'', @subscription_type = N'Push', @sync_type = N'automatic', @article = N'all', @update_mode = N'read only', @subscriber_type = 0; GO EXEC sp_addpushsubscription_agent @publication = N'', @subscriber = N'', @subscriber_db = N'', @job_login = null, @job_password = null, @subscriber_security_mode = 1; GO ``` ## Creating a Transactional Replication Pull Subscription ``` -- commands to execute at the subscriber, in the subscriber database: use [] EXEC sp_addpullsubscription @publisher= N'', @publisher_db= N'', @publication= N'', @subscription_type = N'pull'; Go EXEC sp_addpullsubscription_agent @publisher = N'', @subscriber = N'', @publisher_db= N'', @publication= N'' ; @job_login = null, @job_password = null, @subscriber_security_mode = 1; GO ``` ## To Resume the Merge Agents After the Availability Group of the Subscriber Fails Over For merge replication, a replication administrator must manually reconfigure the subscriber with the following steps: 1. Execute **sp_subscription_cleanup** to remove the old subscription for the subscriber. Perform this action on the new primary replica (which was formerly the secondary replica). 2. Recreate the subscription by creating a new subscription, beginning with a new snapshot. > [!NOTE] > The current process is inconvenient for merge replication subscribers, however the main scenario for merge replication is disconnected users (desktops, laptops, handset devices) which will not use Always On availability groups on the subscriber.