Lesson 5 of 14 · 20m
Resource limits and what the OOM killer actually does
MemoryMax, CPUQuota, TasksMax, OOMScoreAdjust — bound each service's blast radius before the kernel picks a victim.
The OOM killer doesn't ask which process matters. It walks oom_score, picks the biggest cheapest kill, and fires — and it usually picks the process you were least willing to lose, because the database holds more memory than the leaky worker that caused the problem. The wrong mental model is "OOM means buy RAM." OOM is a scheduling decision the kernel made because nobody told it your priorities. cgroups v2 and a few [Service] directives are how you tell it.
What happens when memory runs out
The kernel's OOM killer scores every process — roughly resident size, adjusted by oom_score_adj (−1000 to 1000) — and kills the top scorer. You see it in the kernel log, not the victim's own log:
journalctl -k -g 'oom|Out of memory'
journalctl -u app --since "-1h" # ends abruptly; the unit records "killed by the OOM killer"A unit whose log just stops, with no error, was usually killed by something external — check journalctl -k for oom-kill and -p err -b around the timestamp before you blame the code.
Bound the blast radius per-service
[Service]
MemoryMax=1G
MemoryHigh=800M
CPUQuota=200%
IOWeight=100
TasksMax=200
OOMScoreAdjust=500MemoryMax=is the hard cap — the cgroup hits it and the service's processes OOM together, inside their own boundary; the rest of the box stays alive.MemoryHigh=starts reclaim pressure earlier, so most services throttle before they die.CPUQuota=200%caps the service at two cores' worth. A runawaynpm cior a forked-away worker can't eat all 32.TasksMax=bounds fork bombs and thread storms — a service that normally holds 30 threads shouldn't get 3,000.OOMScoreAdjust=500biases the kernel killer toward this service; negative values protect it. Put +500 on expendable batch workers, leave databases at 0 or below.IOWeight=throttles a backup or compaction job that would otherwise starve the database's disk.
Set it live without editing the unit: systemctl set-property app.service MemoryMax=1G. Inspect live: systemd-cgtop shows per-cgroup CPU/memory/IO — top for the box's services, which is the granularity ops actually needs.
systemctl status app.service already prints a Memory: 412M line when accounting is on — older builds need DefaultMemoryAccounting=yes in /etc/systemd/system.conf or per-unit MemoryAccounting=yes for systemd-cgtop and the status line to stop guessing. The number you want before writing MemoryMax is the service's honest steady-state plus headroom, not a round number from a diagram.
systemd-oomd and earlyoom
The kernel OOMs late — after minutes of thrashing. systemd-oomd (Fedora ships it enabled; Debian/Ubuntu package it but default off) kills a cgroup when memory pressure (PSI) crosses thresholds, long before the kernel's panic-driven pass. earlyoom is the lighter-weight alternative. Either beats "the box hung for twenty minutes and then killed postgres anyway."
Try this
On a scratch box: systemctl set-property stress.service MemoryMax=200M, run stress-ng --vm 1 --vm-bytes 1G inside it, and watch journalctl -k fire on the cgroup while the box stays healthy. Then run systemd-cgtop during a normal workload — you'll find at least one service quietly using three times what anyone estimated.
Pitfalls
MemoryMaxon a service with a legitimate cache — the kernel kills it for doing its job.MemoryHighplus monitoring beats a hard cap you can't size yet.- An OOM inside the cgroup doesn't always say "oom" in the app log — allocation failures surface as random errors. Correlate with
journalctl -kbefore debugging the wrong layer. OOMScoreAdjustis a bias, not a guarantee — protect the wrong thing and the killer still picks postgres.- cgroup v1 vs v2 naming differs on older distros (
MemoryMaxvsMemoryLimit).stat -fc %T /sys/fs/cgroupprintscgroup2fswhen you're on v2.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
Self-Host OpenClaw: VPS Setup to Production (2026)
Self-host OpenClaw from zero to production. Server provisioning, SSH lockdown, Docker, reverse proxy, Fail2ban, performance tuning, and full backup strategy in one guide.
Zero Trust Everything: The Complete Guide to Self-Hosting Teleport for Secure Infrastructure Access
SudoSecurity runs Teleport internally for everything — admin panel access, secure database connections, Kubernetes, and zero-trust philosophy across our entire team. This is the complete technical guide to installing, configuring, and running your own self-hosted Teleport cluster from scratch.
Red Hat Called This Bug a 5.9. Two Weeks Later It Was a 9.8.
CVE-2026-98432 is a use-after-free in the IPv4 TCP handshake, reachable by anybody who can reach a listening port, sitting in the kernel since 4.4. Red Hat scored it 5.9 moderate and deferred the fix. Then 8.1. Then 9.8.
Self-Hosting Vaultwarden Without Trusting the Defaults
Vaultwarden gives you a Bitwarden-compatible password server in one container and about ten minutes. The defaults that make that possible leave org creation open, audit logging off, accounts on PBKDF2, and a rate limiter that trusts a header anyone can set.