I'm using a simple availability group with a primary server and a secondary replica. They are configured like this:
I have situations where I will insert a row into Table A on the primary using a query like this:
INSERT INTO TableA(UniqueId, Column1)
SELECT 123, Column2
FROM @TVP
And after that I immediately query the values from TableA using a read-only connection.
SELECT Column1
FROM TableA
WHERE UniqueId = 123
Sometimes when making this query, we do not get any rows back. I assume this is because the read-only replica hasn't gotten the data from the primary replica yet but I thought that the insert query would not return until the data had been hardened to the secondary replica.
What is going on here?
With a synchronous AG replica, the commit is hardened on both the primary and secondary nodes when the INSERT
transaction commits. However, the changes will not be visible on the secondary until a redo thread on the secondary applies the changes. The latency is typically short but can be delayed due to blocking on high resource utilization on the secondary.
See the Data Latency topic in the documentation for more information.