Blog · Infrastructure

Self-hosted Supabase: how we cut Kong's memory usage

· updated · 3 min read · by

Originally written in Portuguese. Português

A row of server racks in a data center

3 min read

Running Supabase on your own infrastructure gives you full control over your data, but it requires attention to resource usage. In one of our environments, five Supabase stacks shared the same virtual machine, and memory was always at the limit. Late one night, during an application build, the VM froze from lack of RAM and every service on it went down.

In short

  • Kong, Supabase’s API gateway, ships with nginx_worker_processes = auto: one process per CPU core it can see.
  • On a VM with 24 vCPUs, that meant 24 workers per stack, multiplied by 5 stacks.
  • We pinned KONG_NGINX_WORKER_PROCESSES=2 in each stack. Each Kong dropped from about 1.15 GB to 130 MB.
  • Measuring per container before adding RAM kept the problem from just being postponed.

The diagnosis

The first temptation was to increase the VM’s memory. Before that, we measured each container’s usage:

docker stats --no-stream --format "table {{.Name}}\t{{.MemUsage}}" | sort -k2 -h

One service stood out across all five stacks: Kong. Each instance used more than 1 GB, adding up to about 5.9 GB for gateways alone, on a VM that needed headroom for builds.

The cause

Kong is built on NGINX, and Kong’s default configuration sets the number of workers to auto. In the NGINX documentation, auto means detecting the number of CPU cores and creating one worker for each.

That default makes sense on a server dedicated to Kong. On a VM with 24 vCPUs, shared by several stacks, it creates 24 processes in each gateway, and each process holds its own copy of the configuration, plugins and cache in memory.

The fix

The Kong documentation lets you set any configuration parameter through an environment variable, with the KONG_ prefix. In each stack’s Docker Compose:

services:
  kong:
    environment:
      KONG_NGINX_WORKER_PROCESSES: "2"

For the request volume of these projects, two workers are more than enough: each NGINX worker handles thousands of simultaneous connections. After recreating the containers, you can check the number of processes (the expected value is 2):

docker exec <kong> sh -c "ps aux | grep -c '[n]ginx: worker'"

In our case, each Kong’s usage dropped from about 1.15 GB to 130 MB, with no noticeable loss of performance. Across the five stacks, about 5 GB went back to the VM.

What we learned

  • “auto” defaults do not know your environment. They assume the service is alone on the machine.
  • Measure per container before adding RAM. Just adding memory would have postponed the problem. We also took the VM from 24 to 32 GB, but to give builds headroom, not to feed Kong.
  • Keep headroom for builds. Front-end builds consume a lot of memory for a few minutes, and that peak is when the system freezes.
  • Back up before you change anything. We saved each stack’s compose file and variables before the change, with the way back written down.

References

Need help with Supabase or containers in production? Talk to us.