I’ve grown up reading Tom Clancy and probably most of you have at least seen Red October, so this book caught my eye when browsing used books for a recent trip. It’s a fairly human look at what’s involved in sailing on a Trident missile submarine…
I apologize in advance if I mess up the terminology.
I’ve worked with a moderate read workload on a readable replica and I wanted to share some of the things I’ve seen.
First, how in sync is your replica?
To make an Availability Group replica readable, you have to set to asynchronous commit. This means that you might be reading old data. I recommend creating a tracking table. I think a similar technique is sometimes used in replication.
Run an Agent job on the primary replica that inserts a date every 5-10 seconds, and then make sure it stays up to date. While you can achieve a similar effect with PerfMon counters and monitoring scripts, you want a table like this so that your applications can see how up-to-date their data is. Here’s a simple example.
CREATE TABLE LatestDate (LogDate datetime2) GO CREATE CLUSTERED INDEX cx_LogDate on LatestDate(LogDate) GO --Run the below script as frequently as your environment demands. INSERT INTO LatestDate (LogDate) SELECT GETDATE()
What about query performance?
If you heavily use readable secondaries, you need to monitor the performance over there. Not just the CPU, memory and plan cache, but also the index usage and missing index DMVs. Here’s a script you can split into two parts to capture the missing index requests, I don’t have a script for index usage on my blog yet.
Isolation level on your replica (+ TempDB usage increase)
There’s also a couple more caveats here. First, the isolation level changes on readable replicas.
” All queries that run against the secondary databases are automatically mapped to snapshot isolation transaction level, even when other transaction isolation levels are explicitly set. ” Source
That means all the NOLOCK hints don’t do anything. That doesn’t mean there will be blocking, the snapshot isolation level should prevent that. But it does mean more TempDB usage in terms of the version store. Check out the dm_tran_version_store_usage .
One more thing
There’s probably a lot more that can be written on this subject. I want to conclude by saying I still think that readable replicas are very useful, and provide a list of a few closed bugs from recent SQL Server updates:
- Slow queries on secondaries KB article
- Bad query plan on secondaries when statistics on primary are updated with FULLSCAN
- Queries on secondary replica take twice as long to complete
Thanks for reading! Stay tuned.