---
title: "Fixing NodeJS image processing memory problems with external tools"
language: en
topics: ["AI", "cloud", "data", "programming", "server", "work"]
full_article_url: https://willem.com/en/2023-05-03_using-ai-to-generate-code/
---
# Fixing NodeJS image processing memory problems with external tools

*Extract the bottleneck instead of feeding it more RAM.*

> Rotating photos inside a single NodeJS process with the lwip module turned my backend into a memory hog as megapixels grew. I fixed it by extracting the work to an external tool that NodeJS calls and awaits.

My content management backend originally rotated uploaded images in-process, using NodeJS with the lwip module (LightWeight ImageProcessor). That worked fine in most situations, but as images grew in size the memory requirements increased significantly. Doing everything in a single NodeJS process meant the entire backend suffered when pushed to its limits processing thousands of photos.
The common strategy for a bottleneck is to extract it from the main program. Instead of processing images inside NodeJS, I call an external auto rotation script built on proven Debian GNU/Linux tools, exiftool and ImageMagick, and wait for its result. Because NodeJS waits asynchronously for the callback, the process stays available for other requests in the meantime.
The rewrite was part of cutting my cloud costs: less computer power for the same results.
