How does Microsoft Hyper-V Replica work behind the scenes?
Microsoft modified the code of its Hyper-V hypervisor to include a replication feature that allows the content of a virtual machine (VM) to be copied from a primary Hyper-V server to a Hyper-V replication server (replica server). The Microsoft Hyper-V Replica feature is provided by the Replication Engine, a module injected into the hypervisor's code running on Windows Server 2012 and 2012 R2.
There are two important components running in the Replication Engine module: Replication Manager and Replication Tracker. There are also two separate modules that perform different functions: the Network Module and the Change Tracking Module. These components and modules work together to implement Hyper-V replication.
The following steps summarize what happens when you enable Microsoft Hyper-V Replica for a VM:
When you enable replication on the primary server, you select a destination server to which the VM's contents will be replicated. The Replication Engine invokes the Network Module to perform authentication and authorization operations.
The Network Module makes sure the primary server can authenticate the replica server using either the HTTP or HTTPS protocol. It also checks to make sure the required firewall ports (port 80 for HTTP and port 443 for HTTPS) are enabled on the replica server. The module then checks to make sure the primary server is allowed to replicate to the destination server. Once authenticated and authorized, the primary server hands over control to Replication Manager.
The Replication Manager running on the primary server uses its Initial Replication Handler component to replicate the complete VM, including all associated virtual hard disk (VHD) and configuration files to the replica server. At this stage, the replication is enabled for the VM and you have a replicated VM configured on the replica server, but this VM is turned off.
Once the initial replication is over, the Replication Manager activates the Change Tracking module, which is responsible for tracking the changes to the VM's VHD files. The tracked changes are stored in a Hyper-V Replication Log (HRL) file. HRL files are stored in the same folder as VMs.
Once the replication interval expires -- which is five minutes in Windows Server 2012 and every 30 seconds, five minutes or 15 min in Windows Server 2012 R2 -- the Replication Manager accesses the HRL files. Then the Replication Manager activates its Delta Replication Handler component to ensure the files are converted to Standard Replication and Application-Consistent Replication copies. Once the copies are ready, Replication Manager invokes its Replication Tracker component to send them to the replica server.
Most of these events take place at the primary server. The replica server essentially acts as a "listener" until the reverse replication takes place. It is important to understand that the primary server is still busy tracking changes while the replica server is receiving the replicated copies. When the replica server receives these copies, it applies them to the replicated VM in the reverse order, which ensures that the latest writes are applied.