Setting Resource Limits in Kamal 2
1 min read
One of the most requested features in Kamal 2 is the ability to easily deploy multiple applications on a single server. While Kamal makes this process straightforward, running multiple apps on shared hardware comes with an important consideration: resource management. You need to distribute CPU and memory wisely to ensure each application gets its fair share without starving others.
Understanding Kamal’s Container Architecture
Kamal uses Docker under the hood, specifically the docker run command to manage containers. This means we can leverage Docker’s resource limitation options, but the configuration method differs between:
- Servers or Roles
- Accessory (like Redis, PostgreSQL)
- Proxy (Kamal Proxy)
Setting Resource Limits for Roles and Accessories
For your main application and accessories, add options key to your deploy.yml:
# config/deploy.yml
servers:
host: HOST
options:
cpus: "1" # Use 1 CPU core
memory: 512M # Limit memory to 512MB
accessories:
redis:
options:
cpus: "0.5" # Half a CPU core
memory: 256M # 256MB memory limit
Setting Resource Limits for Proxy
The proxy container (Kamal proxy) requires a different approach. We need to use the Kamal CLI to set Docker options:
# Set CPU and memory limits for proxy
kamal proxy boot_config set --docker-options="cpus=0.5 memory=256M"
# Verify current proxy settings
kamal proxy boot_config get
# Restart the proxy to apply the changes
kamal proxy reboot
Pro Tips 💡
- Monitor Before Setting: Deploy without limits first and monitor actual usage to set appropriate values.
- Safety Buffer: Always add ~20% buffer to your observed memory usage and leave some headroom for the host system.
Need to learn more? Check out: