{"id":1167,"date":"2025-04-19T09:45:22","date_gmt":"2025-04-19T09:45:22","guid":{"rendered":"https:\/\/humtechno.com\/master\/?p=1167"},"modified":"2025-04-19T09:47:28","modified_gmt":"2025-04-19T09:47:28","slug":"sql-server-alwayson-secondary-replica-read-only-routing","status":"publish","type":"post","link":"https:\/\/humtechno.com\/master\/2025\/04\/19\/sql-server-alwayson-secondary-replica-read-only-routing\/","title":{"rendered":"SQL Server Alwayson Secondary Replica Read Only Routing"},"content":{"rendered":"\n\n\n\n<p>HST Techno<\/p>\n\n<p>Kamran Hussain<\/p>\n\n<p><a href=\"https:\/\/humtechno.com\/master\/wp-content\/uploads\/2025\/04\/SQL-Server-Read-Only-Routing.pdf\">Download Complete Steps<\/a><\/p>\n\n<p><strong>SQL Server Read-Only Routing<\/strong> is a feature used with <strong>Always on Availability Groups<\/strong> that allows SQL Server to automatically route <strong>read-only queries<\/strong> (like SELECT statements) to a <strong>readable secondary replica<\/strong> instead of the primary.<\/p>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<p><strong>In Simple Words:<\/strong><\/p>\n\n<p>When your application sends a <strong>read-only request<\/strong>, SQL Server can <strong>redirect it to a secondary server<\/strong>, reducing the load on the primary server (which handles both reads and writes). This helps improve performance and scalability.<\/p>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<p><strong>How It Works:<\/strong><\/p>\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>Your application connects using a <strong>read-only intent connection string<\/strong>.<\/li>\n\n\n\n<li>SQL Server checks the routing rules in the Availability Group settings.<\/li>\n\n\n\n<li>It routes the request to a <strong>secondary replica<\/strong> that\u2019s set up for read-only workloads.<\/li>\n<\/ol>\n\n<p><strong><u>Execute on Primary Replica Machine of Database<\/u><\/strong><\/p>\n\n<p>&#8212; On Primary Replica<\/p>\n\n<p>ALTER AVAILABILITY GROUP [SQLCL]<\/p>\n\n<p>MODIFY REPLICA ON N&#8217;SQL1&#8242; WITH (SECONDARY_ROLE (ALLOW_CONNECTIONS = READ_ONLY));<\/p>\n\n<p>ALTER AVAILABILITY GROUP [SQLCL]<\/p>\n\n<p>MODIFY REPLICA ON N&#8217;SQL2&#8242; WITH (SECONDARY_ROLE (ALLOW_CONNECTIONS = READ_ONLY));<\/p>\n\n<p>ALTER AVAILABILITY GROUP [SQLCL]<\/p>\n\n<p>MODIFY REPLICA ON N&#8217;SQL3&#8242; WITH (SECONDARY_ROLE (ALLOW_CONNECTIONS = READ_ONLY));<\/p>\n\n<p>ALTER AVAILABILITY GROUP [SQLCL]<\/p>\n\n<p>MODIFY REPLICA ON N&#8217;SQL1&#8242;<\/p>\n\n<p>WITH (SECONDARY_ROLE (READ_ONLY_ROUTING_URL = &#8216;TCP:\/\/SQL1.hst.com:1433&#8217;));<\/p>\n\n<p>ALTER AVAILABILITY GROUP [SQLCL]<\/p>\n\n<p>MODIFY REPLICA ON N&#8217;SQL2&#8242;<\/p>\n\n<p>WITH (SECONDARY_ROLE (READ_ONLY_ROUTING_URL = &#8216;TCP:\/\/SQL2.hst.com:1433&#8217;));<\/p>\n\n<p>ALTER AVAILABILITY GROUP [SQLCL]<\/p>\n\n<p>MODIFY REPLICA ON N&#8217;SQL3&#8242;<\/p>\n\n<p>WITH (SECONDARY_ROLE (READ_ONLY_ROUTING_URL = &#8216;TCP:\/\/SQL3.hst.com:1433&#8217;));<\/p>\n\n<p>&#8212; When SQL1 is Primary<\/p>\n\n<p>ALTER AVAILABILITY GROUP [SQLCL]<\/p>\n\n<p>MODIFY REPLICA ON N&#8217;SQL1&#8242;<\/p>\n\n<p>WITH (PRIMARY_ROLE (READ_ONLY_ROUTING_LIST = (&#8216;SQL2&#8217;, &#8216;SQL3&#8217;)));<\/p>\n\n<p>&#8212; When SQL2 is Primary<\/p>\n\n<p>ALTER AVAILABILITY GROUP [SQLCL]<\/p>\n\n<p>MODIFY REPLICA ON N&#8217;SQL2&#8242;<\/p>\n\n<p>WITH (PRIMARY_ROLE (READ_ONLY_ROUTING_LIST = (&#8216;SQL1&#8217;, &#8216;SQL3&#8217;)));<\/p>\n\n<p>&#8212; When SQL3 is Primary<\/p>\n\n<p>ALTER AVAILABILITY GROUP [SQLCL]<\/p>\n\n<p>MODIFY REPLICA ON N&#8217;SQL3&#8242;<\/p>\n\n<p>WITH (PRIMARY_ROLE (READ_ONLY_ROUTING_LIST = (&#8216;SQL1&#8217;, &#8216;SQL2&#8217;)));<\/p>\n\n<p>ALTER AVAILABILITY GROUP [SQLCL]<\/p>\n\n<p>MODIFY REPLICA ON N&#8217;SQL2&#8242; WITH (SECONDARY_ROLE (ALLOW_CONNECTIONS = READ_ONLY));<\/p>\n\n<p>ALTER AVAILABILITY GROUP [SQLCL]<\/p>\n\n<p>MODIFY REPLICA ON N&#8217;SQL3&#8242; WITH (SECONDARY_ROLE (ALLOW_CONNECTIONS = READ_ONLY));<\/p>\n\n<p><strong><u>How to Verify<\/u><\/strong><\/p>\n\n<p>SELECT * FROM sys.availability_read_only_routing_lists;<\/p>\n\n<p>SELECT * FROM sys.availability_replicas;<\/p>\n\n<p><strong><u>Connection String for Application<\/u><\/strong><\/p>\n\n<p>Server=SQLLI;Database=YourDB;ApplicationIntent=ReadOnly;MultiSubnetFailover=True;<\/p>\n\n\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_siteseo_robots_primary_cat":"","pagelayer_contact_templates":[],"_pagelayer_content":"","neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","_themeisle_gutenberg_block_has_review":false,"footnotes":""},"categories":[17],"tags":[24],"class_list":["post-1167","post","type-post","status-publish","format-standard","hentry","category-mssql","tag-sql-server-read-only-routing"],"_links":{"self":[{"href":"https:\/\/humtechno.com\/master\/wp-json\/wp\/v2\/posts\/1167","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/humtechno.com\/master\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/humtechno.com\/master\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/humtechno.com\/master\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/humtechno.com\/master\/wp-json\/wp\/v2\/comments?post=1167"}],"version-history":[{"count":1,"href":"https:\/\/humtechno.com\/master\/wp-json\/wp\/v2\/posts\/1167\/revisions"}],"predecessor-version":[{"id":1169,"href":"https:\/\/humtechno.com\/master\/wp-json\/wp\/v2\/posts\/1167\/revisions\/1169"}],"wp:attachment":[{"href":"https:\/\/humtechno.com\/master\/wp-json\/wp\/v2\/media?parent=1167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/humtechno.com\/master\/wp-json\/wp\/v2\/categories?post=1167"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/humtechno.com\/master\/wp-json\/wp\/v2\/tags?post=1167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}