CPU pinning (also called CPU affinity) is the practice of binding MaxScale threads to specific CPU cores. By default, the operating system scheduler moves threads between cores to balance load. While this is generally efficient, in high-concurrency workloads, context switching and cache invalidation can reduce performance and increase latency.
Why Use CPU Pinning
- Reduce Thread Migration Overhead
Threads pinned to specific cores avoid moving between cores, reducing context switching and improving CPU cache utilization. - Improve Cache Locality
CPU caches (L1/L2/L3) maintain thread-local data. Pinning threads keeps data in the same core’s cache, improving query processing speed. - Predictable Performance Under Load
High-concurrency spikes can trigger uneven scheduling. Pinning ensures each MaxScale thread consistently handles its portion of client connections. - Better Performance Monitoring
When threads are pinned, CPU usage and thread-level metrics are more predictable, making tuning and diagnostics easier.
How to Enable CPU Pinning for MaxScale
MaxScale does not provide a native configuration option to pin threads. CPU pinning must be done at the operating system level.
Linux Example Using taskset
# Pin MaxScale process to CPUs 0-7
taskset -c 0-7 /usr/bin/maxscale -f /etc/maxscale.cnfSystemd Service Example
[Service]
ExecStart=/usr/bin/maxscale -f /etc/maxscale.cnf
CPUAffinity=0 1 2 3 4 5 6 7Notes:
- The
threads=parameter in the[maxscale]section still controls the number of MaxScale worker threads (typically equal to CPU cores). - CPU pinning applies at the process level, ensuring all MaxScale threads are constrained to the specified cores.
- Always monitor CPU usage and performance metrics to verify benefits.
Summary:
CPU pinning is an optional optimization for high-concurrency MaxScale deployments. It improves cache locality, reduces thread migration, and provides more predictable performance, but it must be implemented via the OS, not within MaxScale configuration.
Considerations and Caveats
- Over-pinning: Pinning more threads than CPU cores can reduce performance due to oversubscription.
- Dynamic Workload: CPU pinning is less beneficial if your system runs many other processes competing for CPU.
- Testing Required: Always benchmark with and without pinning to validate improvements.
Bottom line: CPU pinning is optional, but for high-concurrency, low-latency systems, it can improve throughput, reduce latency variance, and make performance more predictable.
