N8n Error: “Out of Memory”, slow UI, 502 Bad Gateway

Hi everyone,

I’m having ongoing issues with my self-hosted n8n instance and I’m not sure what the real root cause is. The editor is extremely slow, often stuck on “Loading…” or “Redirecting…”, and many times I end up with a 502 Bad Gateway. The server itself looks totally fine (16GB RAM, low CPU usage), but the n8n container keeps restarting.

In the logs I repeatedly get:d

FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory

and also a lot of:

ValidationError: The ‘X-Forwarded-For’ header is set but trust proxy is false

I’m running n8n in Docker with Traefik, and I’m still using SQLite as the database. I also have many active workflows (webhooks + scheduled workflows), so I’m starting to think SQLite might be overloading n8n during startup and causing the memory spikes.

My idea for fixing this:

I’m thinking of doing the following:

  1. Switch from SQLite to PostgreSQL, because it seems more stable for setups with many active workflows.
  2. Increase the Node.js memory limit (NODE_OPTIONS=--max_old_space_size=8192) so n8n can use more than 4GB RAM.
  3. Enable N8N_TRUST_PROXY=true to stop the Traefik header validation errors.

Before I change the whole setup, I just want to check if this approach makes sense or if you recommend doing something differently.

Any feedback or best-practice advice would be really appreciated!

You can certainly increase the heap size to start to see if that makes any difference, along with N8N_TRUST_PROXY=true, since these are simple changes. Migrating to databases may not be as straightforward as trying the latter first. So, try the simple modfifications. If none of the above helps, then check to your see your big the sqlite db is and how fast the OOM happens. Does the OOM happen a bit later now with the heap size change?

Next, you would want to try to narrow the problem down to see if it’s sqlite related or just a workflow with a bad js code node, for example. if it is sqlite related, I’d imagine the OOM to happen quickly and be very repeatable with the issue happening at around the same time– the logs should show db related activity so check that. If the issue is with a js code node, you will have to address the issue there. If you have a fairly small sqlite db and the logs doesn’t suggest db issues, I wouldn’t even go there yet. I’d try to get n8n to a steady state. I would even stop all workflows from running just to see if I can stand up n8n and reach the editor without issues. If that works, then you can always activate workflows one at at a time.

Khem