Skip to content

Instantly share code, notes, and snippets.

@rupeshtiwari
Last active August 11, 2025 19:11
Show Gist options
  • Save rupeshtiwari/a05b108635fe7ea262d5236c1821c5d6 to your computer and use it in GitHub Desktop.
Save rupeshtiwari/a05b108635fe7ea262d5236c1821c5d6 to your computer and use it in GitHub Desktop.
cgroups vs namespaces, linux

Cgroups and Namespaces

Cgroups (control groups) and namespaces are two Linux kernel features that are used to manage and isolate resources and processes.

Cgroups:

Cgroups allow administrators to limit and distribute resources among different groups of processes. They do this by grouping processes together and then applying resource limits to those groups. This can be used to ensure that no one process or group of processes consumes too many resources, which can lead to performance problems or even system instability.

Namespaces:

Namespaces create isolated environments for processes, separating them from the host system and other processes. This can be used to protect processes from each other and to prevent them from interfering with each other's resources.

Cgroups vs Namespaces

The main difference between cgroups and namespaces is that cgroups are used to manage resources, while namespaces are used to isolate processes. Cgroups can be used to limit the amount of CPU, memory, disk I/O, and other resources that a process can use. Namespaces can be used to isolate processes from each other, such as by preventing them from seeing each other's files or network connections.

Where cgropus and namespaces are used?

Cgroups and namespaces are often used together to provide a more comprehensive solution for resource management and isolation. For example, a container runtime like Docker will typically use both cgroups and namespaces to create isolated containers that are limited in their resource usage and that are protected from each other.

Summary

Here is a table summarizing the key differences between cgroups and namespaces:

Feature Cgroups Namespaces
Purpose Resource management Process isolation
Resource types CPU, memory, disk I/O, etc. Files, network connections, etc.
Scope Hierarchical Flat
Isolation No Yes
Use cases Limiting resource usage, creating containers Protecting processes, isolating processes

Analogy for cgroups and namespaces

Cgroups are like the guard who makes sure that each flat gets access to water equally. They control how much resources each process can use, so that no one process hogs all of the resources.

Namespaces are like the flats in the apartment. They create isolated environments for processes, so that they can't see or interact with each other's resources. This analogy can be helpful for understanding how cgroups and namespaces work together to provide resource management and isolation for processes.

Understanding of scope of cgroups vs namespaces

Hierarchical scope of cgroups

Hierarchical cgroups organize resources in a tree-like structure. This means that each cgroup can have child cgroups, and so on. This allows for a more granular control of resources, as you can specify the amount of resources that each cgroup can use.

For example, you could create a cgroup for each application that you run. This would allow you to limit the amount of CPU and memory that each application can use.

Flat namespaces

Flat namespaces organize resources in a flat structure. This means that all of the resources are in the same namespace. This is a simpler way of organizing resources, but it does not offer as much granular control.

For example, if you have a flat namespace for the network, all of the processes in the namespace will share the same network interface.

Analogy for scopr of cgropus vs namespaces

Analogy Hierarchical scope of cgroups

Imagine that you have a company with many different departments. Each department has its own budget, and each department can only spend money on things that are related to its own work. This is similar to hierarchical cgroups, where each cgroup has its own budget, and each cgroup can only use resources that are related to its own work.

Analogy for Flat namespaces

Now imagine that you have a company with no departments. Everyone in the company has access to the same budget, and everyone in the company can use any resource that they want. This is similar to flat namespaces, where all of the resources are in the same namespace, and all of the processes in the namespace have access to all of the resources.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment