To isolate resources with systemd and cgroups v2 on a Linux dedicated server, group services into custom .slice units and apply CPUWeight, MemoryHigh, MemoryMax, and IOWeight drop-in directives. Unlike cgroups v1, the unified v2 tree coordinates CPU, memory writeback, and NVMe I/O under one hierarchy. Pair these limits with systemd-oomd and PSI metrics to prevent runaway jobs from starving critical databases.
Deterministic Resource Partitioning with Unified cgroups v2
Co-locating PostgreSQL, worker queues, and web frontends on a single bare-metal Linux host without kernel-level isolation inevitably leads to noisy-neighbor tail-latency spikes during traffic bursts or backup windows. By configuring the unified systemd cgroups v2 hierarchy, operators enforce strict CPUWeight, MemoryHigh, MemoryMax, and IOWeight boundaries directly in kernel space without virtualization overhead.
Core Stack Covered: Linux kernel 6.x unified cgroups v2, systemd slices & drop-ins, Pressure Stall Information (PSI), blk-iocost, and OOMD memory-pressure policies.
Sharing one bare-metal Linux host across databases, background workers, and web endpoints exposes critical services to sudden CPU, memory, and NVMe contention. Because legacy cgroups v1 split controllers across separate trees, buffered writeback often bypassed limits. This guide walks through unified cgroups v2 slice architecture, PSI telemetry, and safe rollback.
Co-located batch jobs, log compaction, and unindexed queries can saturate NVMe queues and evict database pages within seconds.
Unlike v1 trees, cgroups v2 couples memory writeback accounting with block I/O throttling under a single kernel hierarchy.
Confirm cgroup2fs mounts, tune MemoryHigh before MemoryMax, and monitor /proc/pressure stalls to validate deterministic slice isolation.
Kernel, Hardware & Systemd Prerequisites for Unified cgroups v2
Before partitioning production workloads with systemd cgroups v2 linux dedicated server policies, verify that your distribution boots natively into the unified hierarchy rather than hybrid or legacy mode. Modern enterprise distributions—including Debian 12+, Ubuntu 22.04/24.04 LTS, RHEL 9, AlmaLinux 9, and Rocky Linux 9—mount cgroups v2 by default at /sys/fs/cgroup. If you are running an older kernel or a custom GRUB configuration, confirm that systemd.unified_cgroup_hierarchy=0 is absent from /proc/cmdline. You can review supported OS lifecycles in our enterprise Linux OS dedicated server guide.
Hardware & Kernel Requirements
- Linux Kernel 5.15+ or 6.1+ LTS: Required for mature Pressure Stall Information (PSI) metrics, accurate writeback memory accounting, and
blk-iocostNVMe proportional I/O control. - systemd 250+: Provides full support for
ManagedOOMMemoryPressure=,AllowedCPUs=,MemoryZSwapMax=, and per-slice delegation. - NVMe Queue & Scheduler Support: While NVMe devices default to the
nonescheduler for raw throughput, proportionalIOWeight=enforcement requires eitherkyber,bfq, or kernel-levelblk-iocostQoS profiles. - Baseline Hardware Telemetry: Establish your raw CPU, memory bandwidth, and NVMe IOPS ceiling before imposing quotas; see our Linux server benchmarking guide with YABS and FIO for baseline methodology.
# Verify unified cgroups v2 mount and active controllers
stat -fc %T /sys/fs/cgroup/
# Expected output: cgroup2fs
cat /sys/fs/cgroup/cgroup.controllers
# Expected output: cpuset cpu io memory hugetlb pids rdma misc
# Check systemd version and PSI kernel support
systemctl --version | head -n 2
cat /proc/pressure/cpu /proc/pressure/memory /proc/pressure/io
If stat -fc %T /sys/fs/cgroup/ returns tmpfs instead of cgroup2fs, your host is still running legacy cgroups v1 or hybrid mode. On Debian/Ubuntu or RHEL-family systems, update GRUB_CMDLINE_LINUX in /etc/default/grub to include systemd.unified_cgroup_hierarchy=1, regenerate the GRUB configuration, and schedule a controlled reboot before deploying slice policies.
cgroups v1 vs. Unified cgroups v2 & Controller Policy Matrix
Understanding how cgroups v2 differs from legacy v1 prevents common misconfigurations such as applying hard CPUQuota= ceilings where relative CPUWeight= shares are more appropriate, or setting MemoryMax= without a MemoryHigh= throttling buffer. In a dedicated bare-metal environment, you want idle capacity to remain available to bursty workloads while guaranteeing deterministic minimums during contention.
| Resource Dimension | cgroups v1 Behavior | Unified cgroups v2 Directive | Production Bare-Metal Recommendation |
|---|---|---|---|
| CPU Scheduling | Separate cpu and cpuacct trees; shares scale 2–262144 |
CPUWeight= (1–10000) & CPUQuota= (%) |
Use CPUWeight= (e.g., 500 for DB, 100 for batch) so idle cores are never wasted. |
| Memory Throttling | memory.soft_limit_in_bytes only enforced under global pressure |
MemoryHigh= (soft throttle) & MemoryMax= (hard OOM) |
Set MemoryHigh= at 85% of slice budget and MemoryMax= at 95% to trigger reclaim before OOM. |
| Memory Guarantee | Not reliably isolated across writeback caches | MemoryMin= (hard) & MemoryLow= (best-effort) |
Assign MemoryLow= to PostgreSQL/Redis slices to protect hot working sets from batch jobs. |
| Block I/O & Writeback | Buffered writes unattributed to originating cgroup | IOWeight=, IOReadBandwidthMax=, IOWriteIOPSMax= |
Combine unified memory+io controllers with blk-iocost for NVMe fairness. |
| Core Pinning (NUMA) | Manual cpuset hierarchy management |
AllowedCPUs= & AllowedMemoryNodes= |
Pin latency-critical services to a single NUMA socket to eliminate cross-socket QPI/UPI hops. |
Notice the strategic difference between MemoryLow= and MemoryHigh=. MemoryLow= acts as a protective floor: during global memory pressure, the kernel avoids reclaiming pages from a slice whose usage sits below its MemoryLow= threshold unless no other reclaimable memory exists. Conversely, MemoryHigh= acts as a proactive brake: once a slice crosses MemoryHigh=, its allocating threads are throttled and forced to perform direct memory reclaim, slowing down the offending workload before it triggers an abrupt OOM kill.
Step-by-Step Systemd Slice & Drop-In Configuration
To isolate workloads cleanly without editing vendor-supplied unit files in /lib/systemd/system/, create custom .slice definitions under /etc/systemd/system/ and bind individual services via Slice= drop-ins. Suppose a 64-core, 256 GB RAM dedicated server runs three distinct tiers: a primary database tier (tier-database.slice), a web/API tier (tier-web.slice), and an asynchronous analytics/backup tier (tier-batch.slice).
1. Define Custom Resource Slices
Create three top-level slice files so each workload tier receives clear CPU, memory, and I/O priorities. Note that CPUAccounting=true, MemoryAccounting=true, and IOAccounting=true are enabled automatically in modern systemd when any corresponding limit is set, but declaring them explicitly documents intent.
cat <<'EOF' > /etc/systemd/system/tier-database.slice
[Unit]
Description=High-Priority Database Slice
Before=slices.target
[Slice]
CPUAccounting=true
MemoryAccounting=true
IOAccounting=true
TasksAccounting=true
CPUWeight=600
IOWeight=500
MemoryLow=96G
MemoryHigh=120G
MemoryMax=132G
AllowedMemoryNodes=0
EOF
cat <<'EOF' > /etc/systemd/system/tier-web.slice
[Unit]
Description=Frontend Web and API Slice
Before=slices.target
[Slice]
CPUAccounting=true
MemoryAccounting=true
IOAccounting=true
CPUWeight=300
IOWeight=300
MemoryLow=32G
MemoryHigh=56G
MemoryMax=64G
EOF
cat <<'EOF' > /etc/systemd/system/tier-batch.slice
[Unit]
Description=Background Batch, ETL, and Backup Slice
Before=slices.target
[Slice]
CPUAccounting=true
MemoryAccounting=true
IOAccounting=true
CPUWeight=100
CPUQuota=400%
IOWeight=100
MemoryHigh=24G
MemoryMax=32G
IOReadBandwidthMax=/dev/nvme0n1 350M
IOWriteBandwidthMax=/dev/nvme0n1 250M
EOF
2. Bind Services to Slices via Systemd Drop-Ins
Attach your production services to their respective slices using systemctl edit or explicit drop-in directories. Placing services inside slices ensures that even if a service forks worker processes or spawns helper scripts, every child PID inherits the slice’s resource envelope.
mkdir -p /etc/systemd/system/postgresql.service.d
cat <<'EOF' > /etc/systemd/system/postgresql.service.d/10-slice.conf
[Service]
Slice=tier-database.slice
OOMScoreAdjust=-500
EOF
mkdir -p /etc/systemd/system/nginx.service.d
cat <<'EOF' > /etc/systemd/system/nginx.service.d/10-slice.conf
[Service]
Slice=tier-web.slice
EOF
mkdir -p /etc/systemd/system/restic-backup.service.d
cat <<'EOF' > /etc/systemd/system/restic-backup.service.d/10-slice.conf
[Service]
Slice=tier-batch.slice
Nice=10
IOSchedulingClass=best-effort
IOSchedulingPriority=6
EOF
systemctl daemon-reload
systemctl restart postgresql nginx
3. Delegate Sub-Trees for Container Runtimes or Unprivileged Services
If you run Podman, containerd, or custom worker supervisors inside a slice, enable Delegate=yes on the service unit. Delegation hands ownership of the service’s cgroup sub-tree to the process manager so it can create child cgroups without systemd overwriting them during a daemon-reload.
mkdir -p /etc/systemd/system/containerd.service.d
cat <<'EOF' > /etc/systemd/system/containerd.service.d/20-delegate.conf
[Service]
Slice=tier-web.slice
Delegate=yes
EOF
systemctl daemon-reload
PSI Telemetry, blk-iocost Tuning & Runtime Verification
Once slices are active, validate that the kernel is enforcing boundaries and monitor Pressure Stall Information (PSI). Unlike legacy load averages—which conflate runnable threads with uninterruptible disk sleep—PSI measures the exact percentage of wall-clock time that tasks in a cgroup are stalled waiting for CPU, memory, or I/O.
# Inspect live per-slice CPU, Memory, Tasks, and I/O consumption
systemd-cgtop -n 3 -d 2
# Verify effective cgroup properties on a slice
systemctl show tier-database.slice -p CPUWeight,MemoryLow,MemoryHigh,MemoryMax,IOWeight
# Read per-slice PSI stall counters directly from cgroupfs
cat /sys/fs/cgroup/tier-database.slice/cpu.pressure
cat /sys/fs/cgroup/tier-database.slice/memory.pressure
cat /sys/fs/cgroup/tier-database.slice/io.pressure
Interpreting PSI Output
Each PSI file reports two lines: some (at least one task stalled) and full (all non-idle tasks stalled simultaneously), averaged over 10-second, 60-second, and 300-second windows. On a healthy database slice, memory.pressure full avg10 should remain near 0.00. If full avg60 exceeds 5.00 on tier-database.slice while tier-batch.slice is idle, your database working set has outgrown MemoryHigh= and needs either a higher memory allocation or query tuning.
Enabling blk-iocost for NVMe Proportional I/O
By default, Linux NVMe namespaces use the none block scheduler, which bypasses traditional I/O weight queuing. To make IOWeight= effective on high-speed NVMe drives without the lock contention of CFQ/BFQ, configure blk-iocost with a linear latency model or calibrated QoS parameters:
# Check major:minor device ID for your NVMe drive
lsblk -d -o NAME,MAJ:MIN,SCHED /dev/nvme0n1
# Enable blk-iocost with default NVMe linear model (example major:minor 259:0)
echo "259:0 enable=1 ctrl=auto" > /sys/fs/cgroup/io.cost.qos
echo "259:0 ctrl=auto" > /sys/fs/cgroup/io.cost.model
cat /sys/fs/cgroup/io.cost.qos
For ad-hoc emergency throttling during an incident—such as capping a runaway indexing job without editing files or restarting the daemon—use systemctl set-property. Changes take effect immediately in kernel space and persist in /etc/systemd/system.control/.
# Immediately throttle a misbehaving service at runtime
systemctl set-property tier-batch.slice CPUQuota=150% MemoryHigh=12G
Troubleshooting Contention Edge Cases & Safe Rollback
Even well-planned cgroup policies can surface subtle edge cases under production load. Knowing how to diagnose controller mismatches and roll back limits cleanly prevents prolonged outages.
Common Failure Modes
- Missing Controller in Child Cgroup: If
cat /sys/fs/cgroup/tier-database.slice/cgroup.controllersdoes not listioormemory, verify that parent accounting is enabled and no legacy cgroups v1 mount is shadowing the controller. - Premature OOM Kills Inside a Slice: If
dmesg -T | grep -i oomshows processes killed insidetier-web.slicewhile the host still has free RAM, checkmemory.eventsinside the slice directory. A risinghighcounter followed bymaxoroom_killindicatesMemoryMax=is too tight for transient spikes (such as worker prefork reloads). RaiseMemoryMax=or pairMemoryHigh=withsystemd-oomdfor graceful pressure-based termination. - I/O Throttling Stalling Journald or SSH: Never place
system.sliceorsshd.serviceunder restrictive bandwidth caps. Always isolate user/application workloads in dedicated slices so administrative SSH access remains responsive during storage saturation.
Instant Runtime Rollback
If a newly applied limit degrades throughput, you can revert runtime properties or remove a drop-in without rebooting the dedicated server:
# Revert runtime properties set via systemctl set-property
systemctl revert tier-batch.slice
# Remove a specific service drop-in and restore default system.slice placement
rm -f /etc/systemd/system/restic-backup.service.d/10-slice.conf
systemctl daemon-reload
systemctl restart restic-backup.service
Automated Prometheus Telemetry & systemd-oomd Guardrails
Static slice boundaries protect your bare-metal host during normal traffic patterns, yet production workloads evolve over time. Pairing unified cgroups v2 with systemd-oomd and Prometheus node_exporter ensures that slow memory leaks or creeping I/O saturation are caught before they degrade customer-facing p99 latency. Unlike the reactive kernel OOM killer—which only fires when physical RAM and swap are completely exhausted—systemd-oomd continuously polls per-cgroup PSI memory pressure and terminates the lowest-priority offending unit gracefully.
# Enable systemd-oomd pressure monitoring on the batch slice
mkdir -p /etc/systemd/system/tier-batch.slice.d
cat <<'EOF' > /etc/systemd/system/tier-batch.slice.d/30-oomd.conf
[Slice]
ManagedOOMMemoryPressure=kill
ManagedOOMMemoryPressureLimit=60%
EOF
systemctl enable --now systemd-oomd
systemctl daemon-reload
On the observability side, modern node_exporter releases automatically expose node_pressure_cpu_waiting_seconds_total, node_pressure_memory_waiting_seconds_total, and per-cgroup systemd metrics when launched with --collector.systemd. Alerting when tier-database.slice experiences sustained memory.pressure above 2% or when cpu.stat nr_throttled climbs rapidly gives your SRE team early warning to scale vertically or rebalance slices across NUMA nodes.
Frequently Asked Questions
What is the difference between CPUWeight and CPUQuota in systemd cgroups v2?
CPUWeight= (range 1–10000, default 100) is a proportional share that only activates when CPU cores are contended; an idle server lets any slice burst to 100% capacity. CPUQuota= (for example, 200% for two cores) enforces a hard upper ceiling even when other cores sit completely idle.
How do MemoryLow, MemoryHigh, and MemoryMax interact under load?
MemoryLow= protects a slice’s hot pages from kernel reclaim during global pressure. MemoryHigh= acts as a soft throttling boundary that forces direct reclaim and slows allocations smoothly, while MemoryMax= is a hard limit that triggers the cgroup OOM killer if breached.
Does IOWeight work on NVMe SSDs without changing the I/O scheduler?
No. NVMe devices default to the none scheduler, which bypasses weight queuing. To enforce proportional IOWeight= on NVMe namespaces, enable kernel-level blk-iocost via /sys/fs/cgroup/io.cost.qos or switch the device scheduler to kyber or bfq.
Can I apply or modify cgroups v2 limits without rebooting the server?
Yes. As long as the server already booted with cgroups v2 enabled, systemctl set-property <unit> MemoryHigh=16G CPUWeight=400 applies limits immediately at runtime and writes persistent drop-ins to /etc/systemd/system.control/.
Should I isolate Docker or Podman containers inside a dedicated systemd slice?
Yes. Placing container runtimes in a dedicated slice with Delegate=yes prevents container bursts from starving host-level PostgreSQL, monitoring agents, or SSH daemons while preserving per-container cgroup sub-trees.
Provision Isolated Bare-Metal Linux Nodes with Full Kernel Control
Deploy multi-tier databases, workers, and container runtimes on single-tenant NVMe hardware with zero hypervisor overhead, full root access, and 24/7 systems engineering support.
