CPU affinity is a low-level function on Linux and Windows that enables you to bind one or more processes — or tasks — to specific CPU cores or threads. Administrators can force these processes to run on those cores through completion or equally unbind those processes to free up resources for others. CPU affinity can thus describe the likelihood of dispatching a thread previously used for other tasks back to the CPU for additional use. 

CPU affinity has many names depending on the system or person you're talking to, including processor affinity, cache affinity, CPU binding, and CPU pinning. The concept itself has existed for decades, speculated to have originated in the 1980s to better support early multi-processing systems.

How does CPU affinity work?

Setting CPU affinity is ideal for long running, resource-intensive processes that demand dedicated cycles. It's also great for massive multi-core systems and groups of clustered machines — where engineers can organize and assign thread groups to groups of CPUs. This leads to higher CPU utilization. CPU affinity also allows a given machine to simultaneously run multiple instances of the same process, each on their own respective cores. 

CPU affinity is neither a user space concept nor technically a kernel space concept. A user will configure their CPU affinity settings via GUI (such as in Windows Task Manager) for individual processes, or via command line. These settings are temporary and can be changed at any time, by default. However, it's also possible to implement permanent CPU affinity rules that kick in each time a process initializes, using automations that instruct the OS. Here's where the kernel scheduler comes in — taking these settings and assigning threads or cores accordingly to best suit the workload. 

That said, two types of CPU affinity exist. First is natural affinity, which refers to the default scheduling behavior for the CPU. In this instance, the kernel attempts to run processes on the same CPU for as long as possible. The scheduler shifts a combination of these processes to another CPU or core when capacity issues arise. Second is hard affinity defined by system calls, which rigidly binds tasks to specific CPUs or cores.

What are the benefits of CPU affinity? 

CPU affinity achieves the following in systems of all sizes and complexities: 

  • Higher overall CPU utilization and resource allocation across all active processes

  • Better performance for CPU-intensive workloads and applications

  • Tighter control over the kernel scheduling process with added granularity

  • Easier management of system resources via GUI, making CPU affinity accessible to users who are less technical overall

However, this system management isn't without its drawbacks. Sometimes the CPU scheduling behavior can block threads for regular programs during an I/O operation. This can prevent the CPU from running at peak performance for each task that needs it, when a mixture of apps and tasks are running.

You’ve mastered one topic, but why stop there?

Our blog delivers the expert insights, industry analysis, and helpful tips you need to build resilient, high-performance services.

By clicking "Get new posts first" above, you confirm your agreement for HAProxy to store and processes your personal data in accordance with its updated Privacy Policy, which we encourage you to review.

Thank you! Your submission was successful.

Does HAProxy support CPU affinity?

Yes! HAProxy and HAProxy One support the automatic CPU binding feature, which allows users to take full advantage of their underlying hardware while processing application traffic. HAProxy will enable all available threads in performance mode and organize them into thread groups across available CPUs, and alternatively gives administrators fine-grained control over which CPUs handle which threads for more advanced use cases. This unlocks better performance for intensive tasks while reducing overall CPU usage in a load-balancing context. 

You can quickly enable CPU affinity using a simple directive within your HAProxy configuration. To learn more about CPU affinity and HAProxy, check out our Configuration Manual.