For performance reasons we are looking for a way to split our write-operations from our read operations. A good way to do that is by duplicating the database onto another server (on-premise) or platform (Azure SQL DB). When you look at the options (Availability Groups (AG), database mirroring, clustering, replication, Azure sync groups, etc.) the easiest way to quickly duplicate a database and keep the data up-to-date at the same time is SQL Server replication.
Replication to another on-premise instance is easy. You just follow the steps in the wizard, it works out-of-the-box, and the chances of this process failing are small. With replicating data to an Azure SQL database it’s a bit more of a struggle. Just one single word took me a few HOURS of investigation and a lot of swearing…
Setting up replication and databases
Before you start with setting the whole thing up, make sure that the replication is installed on your instance. If this is not done yet, install the needed components via the installer. This component can be installed without rebooting the server, or the need to restart the instance.
If you needed to install replication, make sure you restart the SQL Server Agent. For some reason, the configuration change works on the instance without any problems, but the Agent can’t run the needed jobs until you restart it.
The next step is creating 2 databases: a publisher (on the on-premise instance) and a subscriber (an Azure SQL database):
And create a table that can be replicated:
CREATE TABLE dbo.SomeObject (ID INT IDENTITY(1,1), SomeValue VARCHAR(100), PRIMARY KEY (ID))
Setup the publisher
Now that you have 2 databases, it’s time to setup the publisher. I’m not guiding you through the whole replication setup (there are plenty of tutorials out there that are WAY better than my explanation), but I’ll show you the highlights.
First, start by clicking “New Publication” on your on-premise instance:
I chose to run the distributor on the same server.
If you follow the wizard, it will prompt you with the question on where to store the snapshots:
Copy this link into for example notepad, because it comes in handy later in the process.
As publication type, choose “Transactional publication”:
Again, follow the wizard and choose the objects you want to replicate (in my case just that single table):
For the security settings, I chose to use the SQL Server Agent
After finishing the steps in the wizard, give your publication a name, and finish this setup.
Create a subscriber
On the on-premise instance, right-click the newly created publication, and add a new subscriber:
The distribution agent will remain on the distributor (push subscription).
When you’re prompted to add a subscriber, choose “Add SQL Server Subscriber”:
Here comes the part that took me a while to figure out. When you try to replicate changes to an Azure SQL database, and you use the normal connection, it will fail to connect in the end. But if you use a secure connection, replication works like a charm. So make sure you use the following server name ([Server Name].database.secure.windows.net):
Hopefully this will save you a few hours of investigation, looking for dodgy error messages, ending up in finding an “errorcode 40852”, and leading you to the solution.
When this is set up, select the target database (subscriber):
When you’re prompted for the distribution agent security information, make sure you use a SQL Server login that has the correct permissions on the Azure SQL database:
Start the replication
Now that everything is in place and configured, it’s time to create the initial snapshot. This snapshot will be used to create all the objects and data that are supposed to be replicated on the Azure SQL database. Open the snapshot agent by clicking “View Snapshot Agent Status” on the publication:
The snapshot agent hasn’t run yet, so press “Start” to start the process:
When this process has completed, you should see something similar like this:
Now it’s time for a final test. Insert a record on the source database (publisher):
INSERT INTO dbo.SomeObject (SomeValue) VALUES ('Test value')
You should see this record pop-up on the subscriber after a few seconds (default latency is 5 seconds):
SELECT * FROM dbo.SomeObject
You can also check the “Synchronization Status”, and you should see a command being replicated:
Out of all the available options, I still think replication is the easiest way to replicate data into Azure. The only downside is that there doesn’t seem to be a lot of information about this, especially when you look at the “database.SECURE.windows.net” part.
If you need more than just the one-way traffic, or you want a bit more functionality, maybe replication isn’t your weapon of choice. But for me, it worked like a charm… Eventually…