How Azure can help your company expand in multiple regions (2 of 5)
Tags: azure, database, georeplication
Published: 2021-06-27 16:21:03

In my previous article, we created a proposal based on the company’s overview, goals, and technology, then highlighting the benefits of an architecture base on Azure resources and giving an overview of its flow. In today’s article, I am going to start guiding you by implementing the proposed architecture by creating an Azure SQL database resource in our main company’s database in Montana, replicating it in Europe, and then synchronizing it with the On-Premises database.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lnz7b66gldxs12pls3r0.png) 

#Create an Azure SQL Database
- Sign in to the Azure portal.
- Select **+ Create a resource**, filter the result for **Databases**, and click on **SQL Database**

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dk39n1c8mpyyvwfbrmak.png) 

- On the creation tab, select a resource group, insert a database name, create a new server in a Central US (primary location of the company), and select a database’s configuration.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1srz8amj4da1xyriqky4.png) 

- Click **Review + create** to create the Azure SQL database resource.

#Replicate the database
- Once the creation process is completed, select your SQL server, and click your SQL database from the list of available databases.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/39y9mrxtw0vpbthm1820.png) 

- Select the **Geo-Replication** tab and click North Europe (closest region to the company’s target audience).

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bizgyhtgnu0we5upyxff.png) 

- In the creation form, create a new server in North Europe, select the same configuration as the primary location, and click OK.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i6mcdkau72pd821hstvn.png) 

- Azure will then initialize the new instance and then seed the data from the primary database to your replica.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/apwgos0nql2lxea2s2cd.png) 

#Synchronize your On-Premise database to Azure SQL database
- Select your primary SQL database from the list of available databases.
- Click the **Sync to other databases** tab and click **New Sync Group**.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u8yuvw4uijzod0i468qm.png) 

- On the creation tab, insert a group name, select the **Existing database** and enable the automatic synchronization. By doing so, you will set the **frequency** with which the primary instance of your Azure SQL database will synchronize with the group and **resolve possible conflicts**. The possible options are:
1. **Hub win:** When conflicts occur, data in the hub database overwrite conflicting data in the member database.
2. **Member win:** When conflicts occur, data in the member database overwrite conflicting data in the hub database.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/flk9pjyyak1jkluhz0em.png) 

- Once the synchronization for the group is created, you will see in the status column, **Not Ready**.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fos1emwzwb2ewalgfg6w.png) 

- Select the synchronization group, click databases, and then click the **Add an On-Premises Database** button.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bu3c9682efbx4pgsjebi.png) 

To complete the configuration of an On-Premises database, we must set up an **SQL Azure Data Sync agent** on the machine where the SQL server is located and then Select which Database to sync.

##Set up the SQL Azure Data Sync Agent
- Click **Choose the sync Agent Gateway**.
- If you don’t have an agent already installed on your machine, select the **Create a new agent** radio button.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rxr3x08s2lynrzlg7j3v.png) 

- Download the data sync agent from the following link https://www.microsoft.com/en-us/download/details.aspx?id=27693.
- Open the installer and start the installation wizard.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m0hye90w8fxttio68b8f.png) 

- Read and accept the **License Agreement and Privacy Information**, select **Accept** and click **Next**.
- Enter the credential of the account with Network Access to run Windows Service (for example, contoso\gtrekter) and click **Next**.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2dhujnwzz12n9tmljstv.png) 

- Enter the location where you want to install the agent and click **Next** to start the installation.
- Once the installation is complete, launch the **Microsoft SQL Data Sync Agent**.
- Before continuing, go back to Azure, fill the agent name textbox, and click **Create and Generate Key** button.
Once the key is generated, copy it to the clipboard.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7o4weytophjp4asnhg8l.png) 

- In the Microsoft SQL Data Sync, click the **Submit Agent Key** button.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kmjzobh64zkgrwiescem.png) 

- In the configuration window, add the **Agent Key** generated by Azure and the **Login** and **Password** fields. Enter the Azure SQL Database server’s credentials where the Hub database is located.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4d487quyu30hmilzl9e2.png) 

- Test that everything is working properly by clicking on the **Test Connection** button and then **OK**.
- Click the Register button, and in the **SQL Server Configuration** window, select the **Authentication type**, the **Server**, and the **Database** to synchronize.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8k6fhgjvisqt556fghoa.png) 

- Once again, check that everything is fine by clicking on the **Test Connection** button and **Save**.

##Select the database
To complete the configuration of your On-Premises database, follow the following steps:
- Click **select the database**.
- The tab that will appear, add a **Sync Member Name**, select the **On-premises database** that you have previously registered in the Microsoft SQL Data Sync and select the **Sync Directions**. The available options are:
1. **Bi-directional Sync:** Data changes on either the on-premises SQL Server database or the hub database will be written to the other database.
2. **To the Hub:** Changes in the on-premises SQL Server database are written to the hub database but not vice versa.
3. **From the Hub:** Changes in the hub are written to the on-premises SQL Server, but not vice versa.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zdy4njj9edgstgfdldoz.png)
 
- Click **OK**

Congratulations! You have successfully created an Azure SQL database resource, geo-replicated it in a secondary location, and synchronized it with your On-Premises database!