Back to articles
Cloud VPS & Infra

Enforcing CPU, Memory, and I/O Isolation with systemd cgroups v2 on Linux VPS

Guide to directly limit resources using systemd slice/unit on Linux to ensure backup or build jobs never crash the main Database on the same VPS.

DPTCloud Systems Team7 min read
Enforcing CPU, Memory, and I/O Isolation with systemd cgroups v2 on Linux VPS

Running multiple services on a single Cloud VPS (Web server, Database, backup, build automation) can lead to a "noisy neighbor" problem, where one process consuming excessive CPU, RAM, or I/O can slow down or crash critical services. With systemd cgroups v2, you can set specific resource limits for each service, ensuring stable performance and preventing outages due to resource saturation. This article guides you through using key parameters like MemoryHigh, MemoryMax, CPUQuota, and IOWeight to control CPU, RAM, and I/O resources on Linux VPS.

Setting up systemd cgroups v2 and verifying status

First, ensure your system is using cgroups v2. On modern Linux distributions like Ubuntu 22.04, RHEL 9, and newer releases, cgroups v2 is the default. Check the status with the command:

cat /proc/cgroups

If you see a line containing memory and cpu with 2 in the second column, cgroups v2 is active. Next, verify that systemd is configured to use cgroups v2 by running:

systemctl status systemd-cgroups-agent

A status of active (running) indicates systemd is managing cgroups v2. If not, check your system configuration or distribution.

Configuring resource limits for specific services

To control resources, create a new slice or unit systemd. For example, let's create a slice for the backup-service to limit CPU and RAM.

1. Create the configuration file /etc/systemd/system/backup.slice:

[Slice]
CPUQuota=50%
MemoryHigh=1G
MemoryMax=2G
IOWeight=50

2. Create the file /etc/systemd/system/backup.service:

[Unit]
Description=Backup Service

[Service]
Type=simple
ExecStart=/usr/bin/backup-script.sh
Slice=backup.slice

[Install]
WantedBy=multi-user.target

3. Reload systemd and start the service:

sudo systemctl daemon-reload
sudo systemctl start backup.service

Parameter explanations:

  • CPUQuota=50%: Limits the process to a maximum of 50% CPU usage.
  • MemoryHigh=1G: When RAM usage exceeds 1GB, the system begins to reduce I/O priority to free up memory.
  • MemoryMax=2G: Sets a maximum memory usage limit of 2GB. If exceeded, the process will be killed by the OOM Killer.
  • IOWeight=50: Sets I/O weight, allowing other services to have higher priority when accessing the disk.

Configuring for Database and Web API services

To protect critical services like mysql or nginx, create a separate slice with higher priority:

[Slice]
CPUQuota=80%
MemoryHigh=2G
MemoryMax=4G
IOWeight=100

This setup ensures that the Database and Web API always have sufficient resources to operate stably, even when background processes like backups are running.

Common Errors and Performance Verification

When setting up cgroups, you may encounter common issues:

  • Service fails to start: Check logs with journalctl -u backup.service. Errors are often due to syntax mistakes in the configuration file.
  • No resource limits applied: Ensure the configuration file is in the correct path and systemd has been reloaded.
  • OOM Killer behaves incorrectly: Verify MemoryMax and MemoryHigh. If MemoryMax is too low, the process may be killed prematurely.

To verify effectiveness, use the following commands:

systemctl status backup.service
systemd-cgtop

systemd-cgtop displays resource usage by each slice. You can see the backup process using only 50% CPU and not exceeding 2GB RAM, while the Database remains stable.

Important: In a VPS environment, using cgroups v2 is essential to prevent "noisy neighbor" issues and ensure consistent performance for critical services.

Related infrastructure & engineering guides: Optimizing Linux VPS Network with TCP BBR and Kernel Socket Buffers for Vietnam Players

DPTCloud Tier III Infrastructure

Recommended NVMe Cloud VPS Plans to Deploy This Setup

Enterprise RAID 10 NVMe SSDs, CMC & FPT District 9 uplinks with Anycast Anti-DDoS and 99.99% SLA.

View 22 VPS Plans
ECO · vn-02

VPS-2E

94.600 ₫ / mo

10% VAT included

  • CPU: 2 vCPU
  • RAM: 2 GB
  • SSD: 30 GB NVMe
ECO · vn-02Recommended

VPS-3E

170.500 ₫ / mo

10% VAT included

  • CPU: 2 vCPU
  • RAM: 4 GB
  • SSD: 50 GB NVMe
BASIC · vn-01Recommended

VPS-05

457.600 ₫ / mo

10% VAT included

  • CPU: 4 vCPU
  • RAM: 8 GB
  • SSD: 50 GB NVMe

Related Articles

View all
ZaloZalo SupportDiscord0384 753 478