Quick Tip: Taking advantage of Read-Only SQL Server replicas in your C# application

If you happen to be using C# there is a very easy way to set your application up to take advantage of Read-Only replicas.

Using this connection string: (Some portions were left out for brevity)

"Server=tcp:<servername,port>;Initial Catalog=<database name>;ApplicationIntent=ReadOnly;"

The option ApplicationIntent=ReadOnly tells the system to first look for a replica when asking for a connection, but if there is no replica, then connect to the existing primary(read/write) server.

I have 2 connection strings in my APIs, one that includes that option and one that doesn't. This helps balance the calls between all the available backend resources without doing any special work within the calls themselves.