SQL 服务器 AlwaysOn 中的脏读

Dirty Reads in SQL Server AlwaysOn

我有一对 SQL Server 2014 数据库设置为同步 AlwaysOn 可用性组。

两台服务器都设置为 Synchronous commit 可用性模式,会话超时为 50 秒。辅助设置为 Read-intent only 可读辅助。

如果我写入主要数据然后立即从次要数据读取(通过 ApplicationIntent=ReadOnly),我会持续读取脏数据(即写入前的状态)。如果我在写入和读取之间等待大约一秒钟,我就会得到正确的数据。

这是预期的行为吗?如果是这样,我能做些什么来确保从辅助读取的数据是最新的吗?

我想将次要版本用作主要版本的只读版本(以及故障转移),以减少主要版本的负载。

除非使用无锁提示,否则无法获得脏读..

当您在 AlwaysOn 中启用只读辅助节点时..内部 SQL 使用行版本控制来存储该行的先前版本..

此外,您正在使用同步提交模式,这可确保日志记录首先在辅助提交,然后在主提交..

您看到的是数据延迟..

这个 whitePaper 处理这种情况..下面是相关部分,有助于更多地了解它..

The reporting workload running on the secondary replica will incur some data latency, typically a few seconds to minutes depending upon the primary workload and the network latency.

The data latency exists even if you have configured the secondary replica to synchronous mode. While it is true that a synchronous replica helps guarantee no data loss in ideal conditions (that is, RPO = 0) by hardening the transaction log records of a committed transaction before sending an ACK to the primary, it does not guarantee that the REDO thread on secondary replica has indeed applied the associated log records to database pages.

So there is some data latency. You may wonder if this data latency is more likely when you have configured the secondary replica in asynchronous mode. This is a more difficult question to answer. If the network between the primary replica and the secondary replica is not able to keep up with the transaction log traffic (that is, if there is not enough bandwidth), the asynchronous replica can fall further behind, leading to higher data latency.

In the case of synchronous replica, the insufficient network bandwidth does not cause higher data latency on the secondary but it can slow down the transaction response time and throughput for the primary workload