Last Update: Apr 20, 2023 | Published: Mar 15, 2023
In this article, I will explain what are affinity and anti-affinity rules and how to use them with Azure Stack HCI and Windows Server Hyper-V. If you’ve been using other hypervisors, you may already be familiar with affinity and anti-affinity rules, which are used to define the relationship between virtual machines and hosts.
Affinity and anti-affinity rules describe how virtual machines on a hypervisor are placed next to each other when deployed and in operation. Let’s dive into details about how they work.
When configuring the relationship between two virtual machines with an affinity rule, you want to keep them as close as possible. In the best case, you want to keep them on the same hypervisor host.
Normally, you use affinity rules to keep systems close to each other. That is often required for virtual machines that need very fast and reliable communication with each other.
A good use case could be a high-performance compute cluster where very fast communication for calculations is more important than redundancy. Another example could be frontend and backend systems where the frontend needs very fast communication with the backend system.
Anti-affinity rules are the direct opposite of affinity rules: Those rules are used to ensure that virtual machines are never running on the same hypervisor host.
You may want to use anti-affinity rules for virtual machines that are, for example, running as a cluster or hosting the same service and never should fail at the same time if the host fails. A good example of such a service would be a Windows Server Active Directory domain controller: You should normally have a minimum of two domain controllers, though you don’t want to have them running on the same host to prevent any outages of your directory service.
In any case, clustered systems are always a good use case for using anti-affinity rules.
In more complex environments with larger application systems depending on several frontend and backend application servers, you may want to combine affinity and anti-affinity rules to improve performance while keeping availability as high as possible.
In such a case, you would for example put frontend and backend application services in an affinity group and the backend systems in a separate anti-affinity group.
In Azure Stack HCI, you can create server and site-based affinity and anti-affinity rules. You have the option to create those via PowerShell and the Windows Admin Center. To learn more, Microsoft created a great guide for creating server and site affinity rules for virtual machines.
Normally, an Azure Stack HCI cluster should be perfectly able to place your virtual machines using the affinity rules you defined. However, the following are factors that could negatively impact that placement:
Depending on your virtual machine load balancing configuration, your virtual machines could move around and may not run optimally for your application systems and services. It is normally a good thing to keep load balancing aggressiveness medium to low. That helps to prevent unpredicted moving around of virtual machines.
Affinity and anti-affinity rules are very important tools in your toolset for Windows Server Hyper-V and Azure Stack HCI. It helps you increase your virtual machine performance and availably of critical workloads. In any case, you should always try to use them to optimize your environment.