In my previous article, I talked about transactional replication and presented an example of how to configure and use it. Today, I’m going to investigate the second type of replication called Merge Replication.
Definition and Mechanism
Merge replication allows two or more databases to be kept in sync. Whenever changes happen on one database, they are automatically applied to the other databases. If the changes happen on the Publisher, they are applied to the Subcriber and vice versa.
Merge Replication uses the Merge Agent to control data. It is responsible for synchronizing the changes between the Publisher and its Subcriber. Because data can be changed on both the Publisher and the Subcriber, it will handle conflicts if they happen.
Merge replication allows the Subcriber to disconnect to the Publisher and they will be re-synchronized after re-connecting.
Merge Replication uses the Snapshot agent and Merge Agent instead of Log Reader Agent or Snapshot Agent and Distribution Agent, which were used by Transactional Replication.
My client has a website called Retails Management that serves online transactions for goods sold. They have another system to manage inventory that is called Stock Management. Whenever a customer buys an item, the system needs to update the quantity of the item on both Retail Management and Stock Management. When an item is imported into Stock Management it is also updated on Retail Management to display that an item is available or not. Because the client needs the data on both systems, we chose the MERGE replication type.
From the above image, the Merge Agent is responsible for synchronizing changed data between Publisher and Subscriber.
Merge Replication Configuration
Setting up database on SQLServer2012 and SQLServer2008R2
CREATE DATABASE Sales_Retail ON PRIMARY ( NAME=Sales_Retail, FILENAME='D:\DataSQL2012\Sales_Retail.mdf', SIZE=5MB, MAXSIZE=UNLIMITED, FILEGROWTH=10MB ) LOG ON ( NAME = Sales_Retail_log, FILENAME='D:\DataSQL2012\Sales_Retail_log.mdf', SIZE=10MB, MAXSIZE=UNLIMITED, FILEGROWTH=10MB ); GO USE Sales_Retail GO CREATE TABLE dbo.Product ( ProductID INT PRIMARY KEY, ProductNumber nvarchar(50), ProductNname nvarchar(100), Quantity INT, ModifiedDate DATE ) GO INSERT INTO dbo.Product VALUES(1,'AR-5381','Adjustable Race',2,GETDATE()), (2,'BA-8327','Bearing Ball',3,GETDATE()), (3,'BE-2349','BB Ball Bearing',4,GETDATE())
Setting up Publication
Setting up Subscriber
1- Connect to SQLServer2008 -> expand Replicaion folder -> New subscription -> Connect to Publisher on SQLServer2012 -> Choose Sales_Retail_Merge
2- Choose Run all agents at the Distributor
3- Merge Agent Security -> SQL Server service agent account
Verify Merge Replication mechanism
I have completed the configuration of Merge Repliction. Now, you can view its synchronization progress via the Replication Monitor. Right click on Sales_Retail_Merge -> Launch Replication Monitor
We will update data on SQLServer2012 to see the progress of synchronization.
UPDATE dbo.Product SET Quantity = 5 WHERE ProductID = 1
Continue to update data on SQLServer2008 to observe how Merge Replication works to re-synchronize data from the Subcriber to its Publisher. Update the quantity of ProductID 2 to 6:
UPDATE dbo.Product SET Quantity = 6 WHERE ProductID = 2
If you query data from SQLServer2012, you can see that Quantity of ProductID 2 was updated to six.
Merge Replication is one of replication types that allows us sychronize data among two or many database servers. Depending on your requirements, you can apply this type appropriately. One of the most important aspects of Merge Replication is how it deals with conflicts resolving while synchronizing data, I will talk about that in my next article.