Mahamudul Hasan Rubel
HomeAboutProjectsSkillsExperienceBlogPhotosContact
Mahamudul Hasan Rubel

Senior Software Engineer crafting high-performance web applications and SaaS platforms.

Navigation

  • Home
  • About
  • Projects
  • Skills
  • Experience
  • Blog
  • Photos
  • Contact

Get in Touch

Available for senior/lead roles and consulting.

bd.mhrubel@gmail.comHire Me

© 2026 Mahamudul Hasan Rubel. All rights reserved.

Built with using Next.js 16 & Tailwind v4

Back to Blog
DevOpsJune 21, 20264 min read

Linux Performance: Cgroups v2 and Systemd Slices for VPS

Learn Linux performance tuning using Cgroups v2 and Systemd slices. Stop noisy neighbor syndrome on your VPS with practical, hands-on resource management.

LinuxSystemdDevOpsPerformanceVPSCgroupsDockerCI/CD

I remember the first time a rogue background process crashed my production web server. It was a simple data-processing script that suddenly decided it needed every available CPU cycle, causing my primary API to latency-spike from 40ms to over 3 seconds. That’s the classic "noisy neighbor" syndrome, and on a shared VPS where you don't control the underlying hardware, it’s a constant threat.

If you’re managing production workloads on Linux, relying on default process priority is a recipe for disaster. To solve this, I started moving my critical services into dedicated Systemd slices, leveraging the power of Cgroups v2.

Why Cgroups v2 Matters for Linux Performance

In older versions of Linux, resource management was fragmented across different hierarchies. Cgroups v1 was powerful but notoriously messy to manage. With Cgroups v2, the kernel provides a unified, hierarchical structure that makes resource accounting and throttling much more predictable.

When you're running multiple services on a single VPS, you need a way to say, "This service gets 50% of the CPU, but never more than 70%." If you're coming from a background of container orchestration, you might be familiar with Kubernetes Resource Management: Using VPA Recommendation Mode, but on a bare-metal VPS, you have to build those guardrails yourself.

Implementing Systemd Slice Units

Systemd slices are the cleanest way to organize your processes. Think of them as containers for your system services. Instead of letting every process fight for the same pool of resources, you create a slice, set the limits, and drop your services into it.

I usually start by creating a custom slice file. Let’s say I want to protect my web traffic from my background workers. I’d create a file at /etc/systemd/system/app-workload.slice:

INI
[Unit]
Description=Limited Slice for Background Workers
Before=slices.target

[Slice]
CPUQuota=50%
MemoryMax=2G

After creating the file, reload the daemon: systemctl daemon-reload. Now, any service you want to restrict just needs a single line in its unit file: Slice=app-workload.slice. This is significantly more robust than running background workers with systemd for production reliability without explicit resource boundaries.

The Trade-offs of Hard Limits

When I first set this up, I made the mistake of setting MemoryMax too aggressively. My worker processes started hitting OOM (Out of Memory) kills within about two hours of deployment. I learned quickly that resource management isn't just about limiting the bad actors; it's about giving your healthy processes enough breathing room.

If you set a hard MemoryMax, the kernel will kill the process once it hits that limit. If you use MemoryHigh, the kernel will try to reclaim memory by being more aggressive with page cache and swap, which is often a safer starting point for production apps.

Monitoring and Iteration

You can verify your configuration is actually working by checking the Cgroup stats directly. Run systemd-cgtop to see a real-time view of which slices are consuming the most resources. It's an eye-opener to see how much memory your database or logging sidecars are actually holding onto versus what you think they're using.

For VPS optimization, keep these points in mind:

  1. Start with monitoring: Don't impose limits until you know the baseline.
  2. Use slices for grouping: Don't limit individual services; limit the collective "workload" slice.
  3. Test for failures: If you limit CPU, make sure your app handles the increased latency gracefully.

I’m still experimenting with IO weight settings (IOWeight) to prioritize disk access for my database over my application logs. It’s tricky because disk I/O isn't as easily "bottled" as CPU cycles. My current setup relies on a mix of Systemd slices and occasional nice-level adjustments for legacy scripts that don't play well with Cgroups.

The beauty of this approach is that it’s baked into the kernel. You don't need heavy virtualization or complex orchestration tools to get better Linux performance. You just need to tell the OS how to distribute its attention. It’s not a "set it and forget it" solution, but it’s the most effective way I've found to keep a VPS stable when you're running a mix of high-priority web traffic and unpredictable background tasks.

Frequently Asked Questions

Does using Cgroups v2 add significant overhead? Negligible. The kernel does this accounting anyway; you're just providing the constraints to make the scheduling decisions for you.

Can I use slices for Docker containers? Yes, but Docker has its own way of handling Cgroups. If you're running Docker, you're better off using the default-cgroupns-mode and mapping your container resources directly in your docker-compose.yml.

What happens if I set my limits too low? Your services will experience throttled performance, increased latency, or OOM kills. Always start with generous limits and tighten them iteratively while watching your error rates.

Back to Blog

Similar Posts

DevOpsJune 21, 20263 min read

Docker-in-Docker CI Runners: Orchestrating Ephemeral Linux Environments

Docker-in-Docker CI runners are the key to clean, isolated builds. Learn how to orchestrate ephemeral Linux environments using systemd and Docker containers.

Read more
DevOpsJune 21, 20264 min read

Uptime Kuma Self-Hosted Monitoring: A Simple Guide for VPS Health

Master Uptime Kuma for self-hosted monitoring. Learn to track your VPS health and service uptime using Docker with this straightforward deployment guide.

Read more
A vibrant display of love locks attached to a fence with colorful ribbons.
DevOpsJune 21, 20264 min read

WireGuard Mesh Networking: Secure Your VPS Cluster Communication

WireGuard mesh networking is the most efficient way to secure inter-node communication. Learn to build a private, encrypted VPS cluster network today.

Read more