The behavior of a synchronous commit in AlwaysON is to commit the transaction on the local replica as well as the secondary replica before the transaction is considered complete. If you have used database mirroring you may be familiar with the term ‘High Availability’ to describe a similar behavior.
A common misperception is that the commit request originates on the primary, SQL Server commits to stable media on primary and then sends a message to the secondary to commit the transaction wait for the response. However, in order to scale AlwaysON does NOT send a specific message to the secondary(s) for each commit request but instead uses multiple messages and active workers to optimize the performance. This blog will walk you through how a synchronous commit, showing you how the process works in AlwaysON.
Step |
Action |
Connect |
The secondary establishes a valid connection to the primary using the configured mirroring endpoints. |
Request Data |
The secondary initiates a request to the primary, asking for the log blocks to be shipped. The secondary and primary will negotiate the proper LSN staring point and other information necessary. |
Start Log Scanner |
A log scanner worker is started on the primary. The log scanner ships log blocks to the secondary. The information can be retrieved from log cache, as blocks are flushed on the primary, or the log file, for LSNs required by the secondary, which are no longer resident in log cache. |
Redo |
The secondary starts a redo thread to processes log blocks (runs redo) that have been received from the log scanner on the primary and hardened on the secondary. |
Progress Response(s) |
The secondary sends progress message to the primary on regular intervals—approximately every 3 messages from the primary or every one second occur, whichever occurs first.
|
At this point the primary and secondary are talking to each other, log blocks are being shipped to the secondary and hardened by the receive worker and the log blocks are being redone by the redo worker. As I mentioned before AlwaysON is using a series of messages and workers to accomplish activity in parallel.
Now let us look at what happens when you issue a COMMIT TRAN.
Step |
Action |
Commit Tran |
The T-SQL COMMIT TRAN triggers the Flush To LSN behavior. SQL Server requires that the log records be flushed (hardened) to stable media to consider the transaction committed. |
Local Flush To LSN |
On the primary a request is made to Flush to LSN level of the commit record. This tells the log writer thread on the Primary database replica to bundle up all log records up to the commit and flush them to stable media.
|
Log Block Message |
The log block message is processed on the secondary.
|
Progress Response |
The progress response contains information such as LSN harden level and the redo LSN level. These can be different as redo is still in progress. These messages are returned approx. every 3 messages or 1 second (think of the database mirroring ping / is alive concept.) |
Commit Complete |
KEY CONCEPT
|
We can see that AlwaysOn uses a series of messages and workers is used to optimize the log block handling and provide high availability. By avoiding the transmission of a specific commit message to the secondary for every commit that occurs on the primary the system is able to avoid a flood of the communications between the primary and secondary(s) that would not scale well.
Bob Dorr - Principal SQL Server Escalation Engineer