Agent Wranglers

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

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.