SQL Server Replication:
Intro:
Earlier, most of the applications were using standalone environment where a single centralized server
was responding to multiple users, working in different locations.
Problems:
Performance
Availability
Maintenance
To overcome the above problems, we use Replication.
Replication allows to maintain same database multiple copies at different locations.
Log shipping and mirroring allows to maintain complete database redundancy whereas
replication allows to maintain some part of the database(tables and view) at users location to
maintain consistency.
Advantages:
Improved performance and availability.
Easy maintenance
Reduce locking conflicts when multiple users are working
To move data closer to user.
To allow sites work independently.
Replication Entities:
Replication is based on “Publish and Subscribe” Metaphor.
1. Publisher
2. Article
3. Publication
4. Distributor
5. Subscriber
6. Subscription
7. Push Subscription
8. Pull Subscription
1.Publisher:
It is source database where replication starts.
It makes data available for replication.
Publishers define what they publish through a publication.
2.Article:
Actual database objects included in replication like tables, views, indexes, etc.
It can be filtered when sent to subscriber.
3.Publication:
A group of article is called publication
An article cannot be distributed individually. Hence publication is required.
4.Distributor:
Intermediary between publisher and subscriber
It receives published transactions or snapshots and then stores and then forwards these
publications to the subscriber. i.e., the server responsible for storing replication information
used during synchronization.
It has six system databases including distribution
Replication Architecture:
5.Subscriber:
It is destination database where replication ends.
It can subscribe to multiple publications from multiple publishers.
It can send data back to publisher or publish data to other subscribers.
6.Subscription:
It is request by subscriber to receive a publication.
Two types – Push and Pull
Push Subscription:
With this subscription, the publisher is responsible for updating all the changes to the
subscriber without the subscriber asking those changes.
Push subscriptions are created at the Publisher server.
Pull Subscription:
With this subscription the subscriber initiates the replication instead of the publisher.
The subscriptions are created at the Subscriber server.
Replication Agents:
Replication process works in the background with the help of jobs. These jobs are called
Agents.
These jobs internally uses respective .exe files present in …………….. \110\COM folder.
All the agents information is present in Distribution db in the following tables.
o dbo.MSxxx_agents
o dbo.MSxxx_history
Snapshot Agent:
It is an executable file that prepares snapshot files containing schema and data of published
tables and db objects
It stores the files in the snapshot folder, and records synchronization jobs in the distribution
database
Distribution Agent:
It is used with snapshot and transactional replication
It applies the initial snapshot to the Subscriber and moves transactions held in the Distribution
db to Subscribers.
It runs at either the Distributor for push subscriptions or at the Subscriber for pull subscriptions
Log Reader Agent:
It is used with transactional replication, which moves transactions marked for replication from
the transaction log on the publisher to the distribution db.
Each db has its own Log Reader Agent that runs on the Distributor and connects to the
Publisher.
Merge Agent:
It is used with merge replication.
It applies the initial snapshot to the Subscriber and moves incremental data changes that occur.
Each merge subscription has its own Merge Agent that connects to both the Publisher and the
Subscriber and updates both.
It captures changes using triggers.
Queue Reader Agent:
It is used with transactional replication with the queued updating option.
It runs at the Distributor and moves changes made at the Subscriber back to the Publisher.
Unlike Distribution Agent and Merge Agent, only one instance of the Queue Reader Agent
exists to service all Publishers and publications for a given distribution db.
REPLICATION TYPES:
Snapshot
Transactional
Merge
1.Snapshot Replication:
Used to provide initial data set for transactional and merge replication.
Copies and distribute data and database objects exactly as they appear at the current
moment of time.
Note: The initial data set is typically a snapshot that is created by the Snapshot agent and
distributed and applied by the Distribution agent.
2.Transactional replication:
Used in Server-to-server scenarios that require high throughput including improving
scalability & availability, integration of data from multiple sites, integrating heterogenous
data, data warehousing and reporting and offloading batch processing.
3.Merge replication:
Primarily designed for mobile applications or distributed server applications that have
possible data conflicts.
Common scenarios include: exchanging data with mobile users, consumer point-of-scale
(POS) applications and integration of data from multiple sites.