Keeping a small Python service under 128 MiB with systemd

Practical MemoryHigh, MemoryMax, and thread bounds on 1 GB cloud instances.

A 1 GiB micro VM leaves little headroom once the kernel, journald, and a reverse proxy take their share. Rather than guessing whether a standard-library HTTP service will grow under bursty crawler traffic, cap concurrency in code and enforce hard cgroup ceilings in the unit file.

Bound both threads and request bodies

A ThreadingHTTPServer with no semaphore will happily spawn hundreds of worker threads during a slow-client storm, each reserving its own stack and socket buffers. Guard process_request with a BoundedSemaphore(12), set a socket timeout in get_request, and refuse bodies above a fixed byte ceiling before calling read().

Use MemoryHigh before MemoryMax

Setting MemoryHigh=96M and MemoryMax=128M gives the kernel a chance to reclaim page cache before invoking the OOM killer. Pair those directives with TasksMax=32, PrivateTmp=yes, and ProtectSystem=strict, then inspect real peak footprint with systemctl status after a synthetic burst.

Did this work on your setup, or did you spot a mistake? Send the editor a private note or leave a public note for the next reader.

Related edition