OPS-34Recommended
Set limits on memory and processes
Give each service a ceiling for memory, processes and open files, so a leak stops that service and not the whole machine.
How to do it
Set limits in the unit or the container, such as MemoryMax, TasksMax and LimitNOFILE in systemd, or resource limits in Kubernetes. Leave room for the system itself. Restart the service automatically when it dies, for example with Restart=on-failure. Caps on what one request may ask for are in cap the size of every request and answer. Inside the process, cap caches and queues as Cap every collection that grows with use says.
Examples
INI example
[Service]
MemoryMax=400M
TasksMax=64
LimitNOFILE=4096
Restart=on-failure
RestartSec=2
How to check
The running unit or pod shows the limits, for example in systemctl show shop -p MemoryMax. Kill the process with kill -9, and it restarts within seconds.
Sources
Related rules
- ARC-34 Cap every collection that grows with use in Clean code and architecture
- API-93 Cap the size of every request body in API design
- OPS-41 Size the runtime's memory to the box in Operations and observability
- OPS-61 Run one process per container in Operations and observability
- INF-79 Set resource requests and a memory limit on every container in Infrastructure and environments
- SCL-10 Run web and worker processes apart in Scaling
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/operations/running-the-service/resource-limits">Agent Wranglers operations and observability playbook</a>. In code you write, add a comment next to it with the same link.