Replication and Database Mirroring
Replication and Database Mirroring
Summary:
This white paper describes how to use database mirroring to increase the availability of
the replication stream in a transactional environment. It covers setting up replication in
a mirrored environment, the effect of mirroring partnership state changes, and the
effect of mirroring failovers on replication. In addition, it describes how to use LSN-
based initialization to recover from the failover of a mirrored subscriber database.
Although brief overviews are given of both replication and database mirroring, it is
easier to understand this white paper if the reader has some experience with one or
both of these technologies, and has at least a rudimentary knowledge of database
concepts such as transactions.
Filename: 351311846 2
Copyright
The information contained in this document represents the current view of Microsoft Corporation on the
issues discussed as of the date of publication. Because Microsoft must respond to changing market
conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot
guarantee the accuracy of any information presented after the date of publication.
This White Paper is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS,
IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS DOCUMENT.
Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights
under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system,
or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise),
or for any purpose, without the express written permission of Microsoft Corporation.
Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights
covering subject matter in this document. Except as expressly provided in any written license agreement
from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks,
copyrights, or other intellectual property.
Unless otherwise noted, the example companies, organizations, products, domain names, e-mail addresses,
logos, people, places and events depicted herein are fictitious, and no association with any real company,
organization, product, domain name, email address, logo, person, place or event is intended or should be
inferred.
Microsoft, SQL Server, and the Server Identity Logo are either registered trademarks or trademarks of
Microsoft Corporation in the United States and/or other countries.
The names of actual companies and products mentioned herein may be the trademarks of their respective
owners.
Table of Contents
Introduction......................................................................................................1
Technologies.....................................................................................................2
Transactional Replication Architecture................................................................2
Database Mirroring Architecture........................................................................3
Deploying Database Mirroring and Replication Together.......................................5
SQL Server Replication: Providing High Availability using Database Mirroring 3
Introduction
Transactional replication is the mechanism that Microsoft SQL Server provides to
publish incremental data and schema changes to subscribers. The changes are
published (the replication stream) in the order in which they occur, and typically there
is low latency between the time the change is made on the Publisher and the time the
change takes effect on the Subscriber. This enables a number of scenarios, such as
scaling out a query workload or propagating data from a central office to remote offices
and vice-versa. This form of replication always uses a hierarchical hub and spoke
topology.
The addition of peer-to-peer transactional replication in SQL Server 2005 simplifies the
implementation of a bi-directional transactional replication topology, where the
replication stream flows both ways. In this topology, any participating node may read
or update the data. Properly partitioned modifications are propagated between all
nodes in a full mesh topology (as shown in Figure 1), allowing the data to be highly
available in the event that one server is unavailable. This feature has been further
improved in SQL Server 2008 with conflict detection and online changes for peer-to-
peer topologies.
Technologies
Transactional Replication Architecture
Transactional replication and peer-to-peer replication use the same architecture to
move changes between the servers in a replication topology. The following illustration is
an overview of the components involved in transactional replication.
For more detailed information on database mirroring, see the "Database Mirroring"
topic in SQL Server Books Online:
http://msdn.microsoft.com/en-us/library/bb934127(SQL.100).aspx for SQL
Server 2008
http://msdn.microsoft.com/en-us/library/ms177412.aspx for SQL Server 2005
technique described in this white paper) should require a full re-initialization of the
Subscriber, which would be unrealistic and work heavily against database availability.
This white paper looks at some of the concepts, best practices, and steps that help
redirect the Distribution Agents subscription database connection to the mirror. These
steps allow a mirrored subscription database to exist in the topology and a failover of
the subscription database to be achieved with minimal downtime.
16. Configure the subscription on SERVERSP, which can be either a push subscription or
a pull subscription.
Database mirroring between SERVERSP and SERVERSM can be enabled before or after
replication is configured, but if the Subscriber will be initialized with a snapshot, it is
more efficient to configure mirroring after the subscription database has been
initialized. This is to avoid all the logging activity as a result of the subscription
initialization process being mirrored.
SQL Server 2008 introduces a new synchronization type, initializing from an LSN, which
is used internally to support online changes to a peer-to-peer topology. It is a powerful
option that is similar to initializing from a backup except that initialization proceeds
from a supplied LSN (a Log Sequence Number that identifies a point within a databases
transaction log). This means the transactions that must be applied to the new
subscription database are limited to those after the supplied LSN, providing the
transactions still exist in the distribution database. This makes the option suitable for
disaster recovery scenarios (such as in the example configuration used previously in
this paper) because the initialization of the subscription database can be much faster
than a full re-initialization, and the Distribution Agent starts from where it left off at the
time of the mirroring failover.
After a mirroring failover of a subscription database to the mirror server, it is necessary
to redirect the replication stream to the new subscription database on the new
mirroring principal server (SERVERSM in the example configuration). Although this
involves creating a new subscription, the subscription does not require a full re-
initialization in this case, because the new synchronization option in SQL Server 2008
can be used.
The following section describes how initialization using an LSN (either user-specified or
retrieved from a backup) works and how it enables the replication stream to be re-
directed to the new subscription database after a mirroring failover without a full re-
initialization.
USE TicketOrdersSub;
GO
SELECT transaction_timestamp, *
FROM dbo.MSreplication_subscriptions
WHERE publisher = 'SERVERP'
AND publisher_db = 'TicketOrdersPub'
AND publication = 'TicketOrders';
GO
17. To clean up the old subscription from the new subscription database, run the
following code on the new Subscriber.
This code cleans up the old subscription so that there is no entry for it in the
replication metadata in the subscription database. If it still exists when the new
subscription is created, the transaction timestamp is reset to 0x00, resulting in all
transactions from the distribution database being re-applied to the subscription
database, causing data consistency errors.
USE TicketOrdersSub;
GO
EXEC sp_subscription_cleanup
@publisher = 'SERVERP',
@publisher_db = 'TicketOrdersPub',
@publication = 'TicketOrders';
GO
18. To create the new subscription, run the following code on the Publisher (SERVERP).
The code defines the new subscription on the new Subscriber, using the initialize
from LSN synchronization method with the LSN/timestamp saved in step 1. This
Microsoft Corporation 2008
SQL Server Replication: Providing High Availability using Database Mirroring 19
must be done for both pull and push subscriptions. Ensure that the correct
subscription type is set in the @subscription_type parameter. Substitute the
saved LSN/timestamp from step 1 as the value for the @subscriptionlsn
parameter.
USE TicketOrdersPub;
GO
EXEC sp_addsubscription
@publication = N'TicketOrders',
@subscriber = N'SERVERSM',
@destination_db = N'TicketOrdersSub',
@subscription_type = N'pull',
@sync_type = N'initialize from LSN',
@article = N'all',
@update_mode = N'read only',
@subscriber_type = 0,
@subscriptionlsn = 0x00000013000013380004000000000000;
GO
USE TicketOrdersSub;
GO
EXEC sp_addpullsubscription
@publisher = N'SERVERP',
@publication = N'TicketOrders',
@publisher_db = N'TicketOrdersPub',
@independent_agent = N'True',
@subscription_type = N'pull',
@description = N'',
@update_mode = N'read only',
@immediate_sync = 0;
GO
EXEC sp_addpullsubscription_agent
@publisher = N'SERVERP',
Go to step 7.
21. To finish configuring the push subscription, if appropriate, run the following code on
the Publisher.
This code finalizes creating a continuously-running push subscription.
USE TicketOrdersPub;
GO
EXEC sp_addpushsubscription_agent
@publication = N'TicketOrders',
@subscriber = N'SERVERSM',
@subscriber_db = N'TicketOrdersSub',
@job_login = NULL,
@job_password = NULL,
@subscriber_security_mode = 1,
@frequency_type = 64,
@dts_package_location = N'Distributor';
GO
Continue to step 7.
22. To remove the old subscription, run the following code on the Publisher.
This code drops the old subscription (from SERVERSP) to ensure that its agents do
not run automatically and cause problems with the new subscription.
USE TicketOrdersPub;
GO
EXEC sp_dropsubscription
@publication = N'TicketOrders',
@subscriber = N'SERVERSP',
@destination_db = N'TicketOrdersPub',
@article = N'all';
GO
Conclusion
This white paper shows how database mirroring can be combined with transactional
replication to achieve higher availability of the replication stream.
It describes the mechanism for mirroring the publication database, along with in-depth
explanations of setup steps and options. The behavior of the Log Reader Agent is
analyzed, and trace flag 1448 discussed. By using this trace flag it is possible for
replication to continue from a mirrored publication database if the mirroring partnership
is not synchronized.
The most important part of this white paper is the description of the initialize from LSN
synchronization method introduced in SQL Server 2008. By using this new feature, it is
possible to effectively mirror the subscription database without performing a full re-
initialization in the event of a mirroring failover, leading to less downtime in the event
of a disaster.
Did this paper help you? Please give us your feedback. Tell us on a scale of 1 (poor) to
5 (excellent), how would you rate this paper and why have you given it this rating? For
example:
Are you rating it high because it has good examples, excellent screenshots, clear
writing, or another reason?
Are you rating it low because of poor examples, fuzzy screenshots, unclear
writing?
This feedback will help us improve the quality of white papers we release. Send
feedback.