Skip to content

Lesson 8 of 9 · 24m

CPU, Memory, and I/O Isolation with cgroups and systemd

Using cgroup v2 controls to stop noisy neighbors, protect critical services, and understand container resource behavior.

A single host often runs a mixture of critical latency-sensitive work and best-effort background jobs. Without isolation, the batch job wins because it is willing to consume everything. cgroup v2 gives you practical control over CPU, memory, and I/O. systemd exposes much of it without writing shell scripts against /sys/fs/cgroup by hand.

Think in guarantees, not wishes

The controls answer different questions:

  • CPU weight or max: scheduler priority during contention, or a hard ceiling even when idle.
  • Memory high or max: where reclaim pressure starts, and where the OOM killer becomes involved.
  • I/O weight or max: who gets storage bandwidth when contention appears.

A common mistake is setting only limits. Limits prevent worst-case damage, but they do not protect latency during contention. Weights are often the more useful isolation primitive because idle capacity remains available to whoever needs it.

systemd resource controls

# /etc/systemd/system/myservice.service.d/resources.conf
[Service]
CPUWeight=500
MemoryHigh=3G
MemoryMax=4G
IOWeight=500
LimitNOFILE=65536

Apply with:

sudo systemctl daemon-reload
sudo systemctl restart myservice
systemctl show myservice -p CPUWeight -p MemoryHigh -p MemoryMax

Use MemoryHigh when you want pressure and reclaim before disaster; use MemoryMax as the hard boundary. For a JVM, database, or cache, the limit must align with application heap and cache settings. If PostgreSQL thinks it owns 8 GB inside a 4 GB cgroup, the cgroup wins — abruptly.

Read cgroup pressure directly

cat /sys/fs/cgroup/system.slice/myservice.service/cpu.stat
cat /sys/fs/cgroup/system.slice/myservice.service/memory.current
cat /sys/fs/cgroup/system.slice/myservice.service/memory.events

cpu.stat throttling counters are especially important. If nr_throttled is increasing, the service is hitting its CPU quota even when host CPU looks idle. That is the classic Kubernetes mystery: node utilization 30%, application latency terrible, container CPU throttled.

Protect the system from itself

Reserve capacity for the operating system and emergency access. Background jobs belong in low-weight slices:

sudo systemd-run --unit=batch-report --slice=batch.slice   --property=CPUWeight=50 --property=IOWeight=50   /usr/local/bin/generate-report

Interactive SSH, monitoring agents, and critical control-plane processes should not starve because a backup decides to compress the filesystem. Put noisy work in a slice and make that policy visible.

Field notes

  • Throttling can be the right outcome. The goal of a CPU quota is often to degrade one workload so others survive.
  • Memory limits are not performance tuning by themselves; align them with application allocators and cache sizing.
  • cgroup v1 and v2 differ. Check /sys/fs/cgroup before copying instructions from older systems.
  • Isolation shifts contention; it does not create capacity. If everything is critical, the machine is undersized.

Was this lesson helpful?

Sign in to rate