{"id":250,"date":"2022-04-04T23:00:00","date_gmt":"2022-04-05T04:00:00","guid":{"rendered":"http:\/\/sycured.127.0.0.1.sslip.io\/?p=250"},"modified":"2024-01-14T11:55:49","modified_gmt":"2024-01-14T16:55:49","slug":"stabilizing-nodejs-container-ram-swap","status":"publish","type":"post","link":"http:\/\/10.42.0.68:8080\/blog\/stabilizing-nodejs-container-ram-swap","title":{"rendered":"Stabilizing NodeJS container about RAM and SWAP"},"content":{"rendered":"\n

Having some NodeJS containers in production isn’t a start-and-forget due to CPU and memory management.<\/p>\n\n\n\n

Let’s see how I managed my last NodeJS containers running on AWS EC2.<\/p>\n\n\n\n

Adding a little of SWAP<\/h2>\n\n\n\n

On a fresh install, you don’t have SWAP.<\/p>\n\n\n\n

I always create a little swapfile to avoid an EC2 being stuck due to being out of memory or your software being killed by the OOM killer.<\/p>\n\n\n\n

Example to create a 4GB (128*32 = 4096) swapfile<\/p>\n\n\n\n

sudo dd if=\/dev\/zero of=\/swapfile bs=128M count=32\nsudo chmod 600 \/swapfile\nsudo mkswap \/swapfile\nsudo swapon \/swapfile\necho \"\/swapfile swap swap defaults 0 0\" >> \/etc\/fstab<\/code><\/pre>\n\n\n\n

Tuning kernel to prioritize RAM<\/h2>\n\n\n\n

I always prefer to prioritize RAM instead of swapfile.<\/p>\n\n\n\n

A little tuning to do it:<\/p>\n\n\n\n

echo \"vm.swappiness=1\" | sudo tee -a \/etc\/sysctl.d\/swap.conf\nsudo sysctl -p \/etc\/sysctl.d\/swap.conf<\/code><\/pre>\n\n\n\n

In my case, the value 1 works well but the best is to begin at 10 and lower it during your tests.<\/p>\n\n\n\n

NodeJS: tuning V8 is the key<\/h2>\n\n\n\n

You can’t tune NodeJS itself, you need to tune V8\u2026 I’m not a NodeJS\/JavaScript fan.<\/p>\n\n\n\n

Before setting a special value, I had a latency of up to 3 seconds\u2026 just impossible to accept for a website and an API.
A lot of memory was used and a very unstable container that restarted too frequently.<\/p>\n\n\n\n

max-old-space-size<\/strong><\/p>\n\n\n\n

It’s the solution, a simple value that you put in an environment variable.<\/p>\n\n\n\n

NODE_OPTIONS=\"--max-old-space-size=1536\"<\/code><\/pre>\n\n\n\n

The EC2 (t3a.medium) has 4GB of memory and 4GB of swap. After setting this environment variable, the latency is between 50 and 200 ms with UptimeRobot.<\/p>\n\n\n\n

Just defining this amount (1.5GB), resolved latency and stability issues so reachability.<\/p>\n\n\n\n

Now, I’ll already set this environment variable because it’s fixing a major issue.<\/p>\n\n\n\n

Like vm.swappiness<\/em>, you need to test and find the right value for your use case.<\/p>\n","protected":false},"excerpt":{"rendered":"

Having some NodeJS containers in production isn’t a start-and-forget due to CPU and memory management. Let’s see how I managed my last NodeJS containers running on AWS EC2. Adding a little of SWAP On a fresh install, you don’t have SWAP. I always create a little swapfile to avoid an EC2 being stuck due to […]<\/p>\n","protected":false},"author":1,"featured_media":102,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"saved_in_kubio":false,"footnotes":""},"categories":[14],"tags":[11],"_links":{"self":[{"href":"http:\/\/10.42.0.68:8080\/wp-json\/wp\/v2\/posts\/250"}],"collection":[{"href":"http:\/\/10.42.0.68:8080\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/10.42.0.68:8080\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/10.42.0.68:8080\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/10.42.0.68:8080\/wp-json\/wp\/v2\/comments?post=250"}],"version-history":[{"count":1,"href":"http:\/\/10.42.0.68:8080\/wp-json\/wp\/v2\/posts\/250\/revisions"}],"predecessor-version":[{"id":251,"href":"http:\/\/10.42.0.68:8080\/wp-json\/wp\/v2\/posts\/250\/revisions\/251"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/10.42.0.68:8080\/wp-json\/wp\/v2\/media\/102"}],"wp:attachment":[{"href":"http:\/\/10.42.0.68:8080\/wp-json\/wp\/v2\/media?parent=250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/10.42.0.68:8080\/wp-json\/wp\/v2\/categories?post=250"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/10.42.0.68:8080\/wp-json\/wp\/v2\/tags?post=250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}