Monday, January 6, 2020

SQL Server HA methods & making use of “Read-Only Routing” feature of “Always On Availability Groups”



What is High Availability SQL Server

High Availability means that the SQL Server instances, or databases will be available and reachable, with the least possible downtime, in case of any server crash or failure.

Below are the listed ways of achieving HA
  1. Log shipping
  2. Replication
  3. Mirroring
  4. Always On Failover Cluster
  5. Always On Availability Groups

Log shipping

SQL Server log shipping topography

Replication

SQL Server Transactional replication components and data flow

Mirroring

SQL Server database mirroring topography

Always On Failover Cluster


SQL Server Always on Failover Cluster topography
 


Always On Availability Group

SQL Server AlwaysOn Availability Groups - primary data center and disaster recovery data center

SQL Server tree view of Always On Availability Group




“Read Only routing” feature

•Always On secondary replica has feature to handle read-only connection requests
•This feature is called as Read-only routing feature
•By default both read and write operations goes to primary server
•If Application Intent is mentioned as ReadOnly in request connection, then request goes to secondary database
•Supports max 8 secondary databases
•Depending on the version, requests are served either from same secondary server or in round-robin fashion


Steps for trying from SSMS



Application code changes

Make sure the HA method is AlwaysOn AG method
Create new connection string by adding ApplicationIntent=ReadOnly in connection string
While making read-only stored procedure DB calls, use the read-only connection string

Code snippet

<add name="ReadOnlyDBConnection" connectionString="data source=XXXXXX;initial catalog=XXXXX;integrated security=True;MultiSubnetFailover=True;ApplicationIntent=ReadOnly" providerName="System.Data.EntityClient" />

public static string ReadOnlyConnectionString
        {
            get
            {
                return ConfigurationManager.ConnectionStrings[“ReadOnlyDBConnection”].ConnectionString;
            }
        }

private DbConnection GetReadOnlyConnection()
        {
            var connection = this.providerFactory.CreateConnection();
            connection.ConnectionString = ReadOnlyConnectionString;
            return connection;
        }

Big benefits

Taking off the read load from primary database
Reduces lock situations
Both read and write operations will be faster as they happen in different databases.