Affinity, anti-affinity and scheduling
Placement rules: keeping workloads apart, pinning them together, and what is ignored.

Sizing and placement answer different questions. Sizing asks how much CPU and memory a workload may take; placement asks where it runs — which host, which cores, and which NUMA node its memory comes from. They share one surface — libvirt’s cputune carries both a pin and a weight — but this sheet is only about where.
One idea carries the subject. Affinity keeps workloads together and anti-affinity keeps them apart; both are declared constraints on position, not enforced facts. Three layers express them: cluster rules fixing which node a workload may use, balancers that move workloads, and manual pinning of a vCPU and its memory. Each is a constraint the scheduler tries to satisfy, not a promise — and set too tightly, a rule can leave a host unusable or stop a workload failing over.
WHERE, not HOW MUCH — one three-node cluster, three statements
node1 node2 node3
+---------+ +---------+ +---------+
| vm:100 | | vm:200 | | ct:200 |
| web | | db | | replica |
+---------+ +---------+ +---------+
resource-affinity + vm:100 together with vm:200 keep-together
resource-affinity - vm:200 apart from ct:200 keep-separate
node-affinity + vm:100 -> node1 (strict: must, else stopped)
inside one host (CPU / NUMA):
vCPU0..3 --vcpupin--> cores 0-3 --> numatune nodeset=0 --> local RAM
emulator --emulatorpin--> core 4 (I/O threads keep off the vCPU cores)
Affinity and anti-affinity: together or apart
Proxmox VE states the pair plainly: “The affinity positive specifies that HA resources must be kept on the same node, while the affinity negative specifies that HA resources must be kept on separate nodes”. Its documentation groups both under “Affinity/Anti-Affinity constraints”.
Keeping resources together is about traffic: where two VMs talk a lot — the example is “a web server communicating with a database server” — separate nodes add latency and network load. Keeping them apart is about failure or contention: two replicas must not be lost to one host failure.
Rules compose: a positive rule drags its members into each other’s negative and node rules, and two resources cannot be “kept on the same node and separated on different nodes at the same time”. One asymmetry matters: node affinity rules are non-strict by default, but “resource affinity rules are strict by default” — if unmet, the resource moves to recovery or error state.
Node affinity and the order of failover
A node affinity rule places resources on nodes along two axes: direction (positive keeps a resource on the named nodes, negative keeps it off) and hardness (the strict attribute, a soft or a hard constraint). “By default, an HA resource is able to run on any cluster node”, so the rule is what narrows that. Non-strict, it means “if there is none of the specified nodes available, the HA resource can also be moved to other nodes”; strict, there is no escape — “the resource will be stopped”.
Priorities turn the rule into a failover order: the resource runs on the available nodes of the highest priority, dropping a class only when the higher one is gone — node1 first, then node2 and node3 together, then node4. A recovered resource returns to its highest-priority node once it comes back; and a resource depending on a device present on only some nodes must be restricted to those, because a strict rule is what “limits placement, recovery, and migration to the rule’s nodes”.
Automatic balancers move workloads back
Rules are not the only thing deciding position; most hypervisors also move workloads to even out load. In Windows and Azure Local, VM load balancing “identifies over-committed machines and live migrates VMs from those machines to under-committed machines”, weighing memory pressure and “CPU utilization averaged over a five-minute window”; it re-evaluates every 30 minutes and moves “when the machine is more than 80% loaded”.
The balancer does not overrule policy: “Failure policies such as anti-affinity, fault domains (sites), and possible owners are honored”. Two cautions: balancing “can also introduce temporary performance disruptions when VMs are redistributed across hosts”; and an even-average balancer keeps fighting a rule that wants an uneven layout.
Pinning a vCPU, a NUMA node and its memory
The finest placement is manual, in the domain definition. libvirt’s cputune “provides details regarding the CPU tunable parameters for the domain”: vcpupin fixes which host cores each vCPU may use, and emulatorpin fixes where the emulator thread runs. numatune does the same for memory, “how to tune the performance of a NUMA host via controlling NUMA policy for domain process”; its mode is interleave, strict, preferred or restrictive, and it “defaults to ‘strict’”.
The kernel names the same idea: NUMA memory policy chooses “from which node the kernel will allocate memory” and offers four modes — MPOL_BIND, where “memory must come from the set of nodes specified by the policy”; MPOL_PREFERRED, where “the allocation should be attempted from the single node specified in the policy”; and MPOL_INTERLEAVE, which interleaves pages across the named nodes. Proxmox VE exposes the model as its affinity option — “you can specify the physical CPU cores that are used to run the VM” — set through taskset.
Manual pinning is powerful and easy to regret. The documentation warns that “Forcing a CPU affinity can make sense in certain cases but is accompanied by an increase in complexity and maintenance effort”, and that a poor pin “can also easily lead to asynchronous and therefore limited system performance if some CPUs are fully utilized while others are almost idle”. CPU affinity “is not a security feature”: a performance control, never an isolation boundary.
What the scheduler does not guarantee
The honest version: the scheduler is a best-effort allocator under constraints. Proxmox VE’s Cluster Resource Scheduler “is responsible for selecting new node placements for HA resources”, scoring nodes by a mode (basic counts active guests per node; the load modes weigh usage). In every mode it takes “the node with the lowest load according to the used scheduler”, and after a failure it must fit the survivors somewhere: “This increases the service count on those nodes, and can lead to high load, especially on small clusters”.
Rules tighten this, and tight rules fail in documented ways. A negative resource affinity rule cannot name more resources than there are nodes, and a strict node affinity rule will stop a resource rather than place it outside the named nodes. libvirt’s note on memnode is blunt: “This does not guarantee the placement of the node’s memory allocation”. Rules are constraints you declare and the scheduler satisfies when it can; an over-tight anti-affinity rule can render a host unusable, and a strict placement rule can refuse to fail a workload over.
Level and prerequisites
L4 — infrastructure-level: placement as a declared constraint — cluster affinity and anti-affinity rules, failover order, automatic balancing, and host-level CPU and NUMA pinning — and an honest account of what the scheduler does not promise.
This sits above the L3 sheet Hosts, VMs, vCPU, vRAM and virtual devices, which fixes the CPU and memory model but not placement, and beside the L4 siblings Resource allocation and oversubscription, Clusters and quorum, and Capacity planning and host maintenance.
Where to go next
- Server & Virtualization is the area; the sheet’s node is virtualization-and-networking.
- Sibling L4 sheets, in prose: Resource allocation and oversubscription, Clusters and quorum, High availability and failure handling, Live migration and its prerequisites, Capacity planning and host maintenance.
- Read next: the Proxmox VE HA chapter, the libvirt domain XML reference, and the Windows Server VM load-balancing overview.
References
- Proxmox Server Solutions GmbH — Proxmox VE Administration Guide — https://pve.proxmox.com/pve-docs/pve-admin-guide.html
- Proxmox Server Solutions GmbH — High Availability (Proxmox VE wiki) — https://pve.proxmox.com/wiki/High_Availability
- libvirt project — Domain XML format — https://libvirt.org/formatdomain.html
- The Linux Kernel documentation — NUMA Memory Policy — https://www.kernel.org/doc/html/latest/admin-guide/mm/numa_memory_policy.html
- Microsoft — Virtual machine load balancing (Azure Local / Windows Server) — https://learn.microsoft.com/en-us/windows-server/failover-clustering/vm-load-balancing-overview