Tuesday, May 3, 2016

Limiting MongoDB memory usage with cgroups on Ubuntu 14.04

MongoDB is memory mapping database files, so you Free RAM won't last long. It might be fine if you believe in Linux Memory Management, however I feel quite anxious when mongod takes every single bit of 512Mbytes of RAM of my small DigitalOcean (3 month 1core/512M/20G VPS for 5$, yes 5 bucks) box. Yes I'm chip and I don't want my nginx or nodejs to starve.

Ok, so there is a solution to this - cgroups. Unfortunately there are no config templates with cgroups packages (at least for Ubuntu 14.04), this makes the topic quite obscure. I written some small bash script which will ask you for desired MongoDB memory limit, create configs and init scripts and restart services.
IMPORTANT. This shell script will overwrite all your existing cgroup configuration and init files, please make sure you have nothing to loose.

sudo bash -c 'curl -o- http://brains.by/misc/mongodb_memory_limit_ubuntu1404.sh | bash'

You can find a good write-up on topic here, but it doesn't contain init scripts, so your changes won't last a reboot: https://www.percona.com/blog/2015/07/01/using-cgroups-to-limit-mysql-and-mongodb-memory-usage/

A good page on cgroups with Ubuntu 14.04 is here, but it contains no MongoDB specifics:
http://frank2.net/cgroups-ubuntu-14-04/


No comments:

Post a Comment